Javascript警报窗口重定向到另一个网页 警报(“用户名已存在!”); window.location.href=“registeruser.php”; 警报(“需要电子邮件地址!”); window.location.href=“registeruser.php”; 警报(“需要用户名!”); window.location.href=“registeruser.php”;
使用基本url,然后将自定义路径粘贴到页面,如:Javascript警报窗口重定向到另一个网页 警报(“用户名已存在!”); window.location.href=“registeruser.php”; 警报(“需要电子邮件地址!”); window.location.href=“registeruser.php”; 警报(“需要用户名!”); window.location.href=“registeruser.php”;,javascript,php,alert,Javascript,Php,Alert,使用基本url,然后将自定义路径粘贴到页面,如: if($fullName == "") { echo '<script>'; echo 'alert("Full Name is required!")'; echo 'window.location.href = "registeruser.php"'; echo '</script>'; } var MyBaseURL= var customURL=MyBaseURL。“/registeruser.
if($fullName == "")
{
echo '<script>';
echo 'alert("Full Name is required!")';
echo 'window.location.href = "registeruser.php"';
echo '</script>';
}
var MyBaseURL=
var customURL=MyBaseURL。“/registeruser.php”;
警报(“需要全名!”);
window.location.href=自定义URL;
您应该避免回显脚本,使用包含php标记的正则脚本块。您应该将警报放在
registeruser.php
页面上。让PHP脚本执行以下操作:
<?php
if($fullName == "")
{
?>
<script>
var MyBaseURL = <?php echo BASE_URL_CONSTANT?>
var customURL = MyBaseURL."/registeruser.php";
alert("Full Name is required!");
window.location.href = customURL;
</script>
<?php
}
?>
然后,registeruser.php
可以执行以下操作:
header("Location: registeruser.php?message=" . urlencode("Invalid username"));
警惕();
可能在会话_start()之后尝试呈现一些html。
e、 g
登录信息
这将为消息提供一个显示页面。
不过,在我看来,更好的是:不要脱离php,在需要的地方使用php header()函数将您带到适当的页面。尝试如下使用:
<html>
<head>
</head>
<body>
<h1>Login Message</h1>
</body>
</html>
if($fullName==“”)
{
回显“警报('需要全名!')”;
echo“window.location.replace('page.php?id=“.crmId.”);”;
}
您可以使用此代码
if($fullName == "")
{
echo "<script language='javascript'>alert('Full Name is required!')</script>";
echo "<script language='javascript'>window.location.replace('page.php?id=".$crmId."'); </script>";
}
如果(警告(“用户名已存在!”)
{
window.location.href=“registeruser.php”;
}
如果(警告(“需要电子邮件地址!”)
{
window.location.href=“registeruser.php”;
}
如果(警告(“需要用户名!”)
{
window.location.href=“registeruser.php”;
}
正如其他人所说,您需要将逻辑移到HTML的末尾。其次,您需要在要加载的页面上实现此逻辑,而不是在重定向的页面上。这是一个两步逻辑。例如:
您需要在当前脚本中实现window.location
然后,就在页面标记之前,您希望重定向用户:
<?php
session_start();
include_once "connect.php";
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "")
{
?>
<script>
if(alert("Full Name is required!"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
else
{
if ($userName == "")
{
?>
<script>
if(alert("Invalid username!"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
else
{
if($userName == $result['USERNAME'])
{
?>
<script>
if(alert("Username already exists!"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
else
{
if ($emailAdd == $result['EMAIL'])
{
?>
<script>
if(alert("Email address is required!"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
else
{
if ($passWord == "")
{
?>
<script>
if(alert("Username is required!"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
else
{
if($_POST['password']==$_POST['confirmpass'])
{
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)
VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
?>
<script>
if(alert("Registered Successfully!"))
{
window.location.href = "index.php";
}
</script>
<?php
}
else
{
?>
<script>
if(alert("Password and Confirm Password do not match"))
{
window.location.href = "registeruser.php";
}
</script>
<?php
}
}
}
}
}
}
?>
您的位置href语法错误。应该是这样的:
<?php
if($fullName == "")
{
echo '<script>';
echo 'alert("Full Name is required!")';
echo '</script>';
}
?>
</body>
if($fullName==“”)
{
回声';
回显“警报”(“需要全名!”);
echo'window.location.href=SITE_URL.“registeruser.php”;
回声';
}
这里SITE_URL是您的项目根路径,类似于此脚本将在同一页面上执行,但您必须使用如下表单
if($fullName == "")
{
echo '<script>';
echo 'alert("Full Name is required!")';
echo 'window.location.href = SITE_URL."registeruser.php"';
echo '</script>';
}
您好,您是否尝试将ajax与jquery结合使用
现在,您的脚本是否只返回一个json对象来说明一切是否正常
//reg.php
<?php
session_start();
include_once "connect.php";
if(isset($_POST[submit])){
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "")
{
header('Location:registration.php?fname=1');
}
else
{
if ($userName == "")
{
header('Location:registration.php?uname=1');
}
else
{
if($userName == $result['USERNAME'])
{
header('Location:registration.php?uname=2');
}
else
{
if ($emailAdd == $result['EMAIL'])
{
header('Location:registration.php?mail=1');
}
else
{
if ($passWord == "")
{
header('Location:registration.php?pwrd=1');
}
else
{
if($_POST['password']==$_POST['confirmpass'])
{
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)
VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
echo ' <script>
alert("Registered Successfully!");
window.location.href = "index.php";
</script>';
}
else
{
header('Location:registration.php?pwrd=2');
}
}
}
}
}
}
}
?>
及
//registration.php
session_start();
header('Content-type: application/json');
include_once "connect.php";
// result we return to the user
$response = ['error' => false, 'message' => ''];
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "") {
$response['message'] = 'Full Name is required!';
$response['error'] = true;
} else {
if ($userName == "") {
$response['message'] = 'Invalid username!';
$response['error'] = true;
} else {
if($userName == $result['USERNAME']) {
$response['message'] = 'Username already exists!';
$response['error'] = true;
} else {
if ($emailAdd == $result['EMAIL']) {
$response['message'] = 'Email address is required!';
$response['error'] = true;
} else {
if ($passWord == "") {
$response['message'] = 'Username is required!';
$response['error'] = true;
} else {
if($_POST['password']==$_POST['confirmpass']) {
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
$response['message'] = 'OK';
$response['error'] = false;
} else {
$response['message'] = 'Password and Confirm Password do not match';
$response['error'] = true;
}
}
}
}
}
}
die(json_encode($response));
$(文档).ready(()=>{
$('#myForm')。关于('submit',函数(e){
e、 预防默认值();
$.post('reg.php',$(this.serialize())
.完成((数据)=>{
如果(数据错误){
警报(数据信息);
}否则{
警报(“注册成功”);
}
}).失败(()=>{
警报(“连接错误!!!”);
});
});
});
尝试将一个/
放在registereduser.php
之前,或将确切路径与域名放在一起。将完整路径添加到echo'window.location.href=“registeruser.php”代码>否。这不是工作。可能的复制,也许你应该考虑在Ajax请求中这样做。顺便说一句,请千万不要做类似于mysql\u查询(“SELECT*FROM users where USERNAME='$USERNAME')
,因为你打开了SQL注入的大门。我想他说的是发生警报时显示的页面,不是警报后重定向到的页面。请添加更多关于您答案的描述。您是否遗漏了什么isset($\u GET['message'
@TarangP我有这个问题,有什么问题吗?我认为正确的语法是isset($\u GET['message'])啊,你为什么不说我打错了,漏掉了]
?
<form method=POST action="">
<!--input goes here-->
</form>
<?php
session_start();
include_once "connect.php";
if(isset($_POST[submit])){
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "")
{
echo ' <script>
alert("Full Name is required!");
window.location.href = "registeruser.php";
</script>';
}
else
{
if ($userName == "")
{
echo ' <script>
alert("Invalid username!");
window.location.href = "registeruser.php";
</script>';
}
else
{
if($userName == $result['USERNAME'])
{
echo ' <script>
alert("Username already exists!");
window.location.href = "registeruser.php";
</script>';
}
else
{
if ($emailAdd == $result['EMAIL'])
{
echo '<script>
alert("Email address is required!");
window.location.href = "registeruser.php";
</script>';
}
else
{
if ($passWord == "")
{
echo ' <script>
alert("Username is required!");
window.location.href = "registeruser.php";
</script>';
}
else
{
if($_POST['password']==$_POST['confirmpass'])
{
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)
VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
echo ' <script>
alert("Registered Successfully!");
window.location.href = "index.php";
</script>';
}
else
{
echo ' <script>
alert("Password and Confirm Password do not match");
window.location.href = "registeruser.php";
</script>';
}
}
}
}
}
}
}
?>
<?php
if(isset($_GET[fname]) && $_GET[fname]==1)
echo ' <script>
alert("Full Name is required!");
</script>';
if(isset($_GET[uname]) && $_GET[uname]==1 )
echo ' <script>
alert("Invalid username!");
</script>';
if(isset($_GET[uname]) && $_GET[uname]==2 )
echo ' <script>
alert("Username already exists!");
</script>';
if(isset($_GET[pwrd]) && $_GET[pwrd]==2 )
echo ' <script>alert("Password and Confirm Password do not match");
</script>';
if(isset($_GET[pwrd]) && $_GET[pwrd]==1 )
echo ' <script>alert("Password is invalid");
</script>';
if(isset($_GET[mail]) && $_GET[mail]==1 )
echo ' <script>alert("invalid mailid");
</script>';
?>
<form method=POST action="reg.php">
<!--input goes here-->
</form>
<?php
session_start();
include_once "connect.php";
if(isset($_POST[submit])){
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "")
{
header('Location:registration.php?fname=1');
}
else
{
if ($userName == "")
{
header('Location:registration.php?uname=1');
}
else
{
if($userName == $result['USERNAME'])
{
header('Location:registration.php?uname=2');
}
else
{
if ($emailAdd == $result['EMAIL'])
{
header('Location:registration.php?mail=1');
}
else
{
if ($passWord == "")
{
header('Location:registration.php?pwrd=1');
}
else
{
if($_POST['password']==$_POST['confirmpass'])
{
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)
VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
echo ' <script>
alert("Registered Successfully!");
window.location.href = "index.php";
</script>';
}
else
{
header('Location:registration.php?pwrd=2');
}
}
}
}
}
}
}
?>
session_start();
header('Content-type: application/json');
include_once "connect.php";
// result we return to the user
$response = ['error' => false, 'message' => ''];
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "") {
$response['message'] = 'Full Name is required!';
$response['error'] = true;
} else {
if ($userName == "") {
$response['message'] = 'Invalid username!';
$response['error'] = true;
} else {
if($userName == $result['USERNAME']) {
$response['message'] = 'Username already exists!';
$response['error'] = true;
} else {
if ($emailAdd == $result['EMAIL']) {
$response['message'] = 'Email address is required!';
$response['error'] = true;
} else {
if ($passWord == "") {
$response['message'] = 'Username is required!';
$response['error'] = true;
} else {
if($_POST['password']==$_POST['confirmpass']) {
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
$response['message'] = 'OK';
$response['error'] = false;
} else {
$response['message'] = 'Password and Confirm Password do not match';
$response['error'] = true;
}
}
}
}
}
}
die(json_encode($response));
<form method=POST action="reg.php" id="myForm">
<!--input goes here-->
</form>
<script src="//path to jquery"></script>
<script>
$(document).ready(() => {
$('#myForm').on('submit', function (e) {
e.preventDefault();
$.post('reg.php', $(this).serialize())
.done((datas) => {
if(datas.erreur) {
alert(datas.message);
} else {
alert('registration succesfully');
}
}).fail(() => {
alert('connect error !!!');
});
});
});
</script>