gpt4 book ai didi

Javascript 警报窗口重定向到另一个网页

转载 作者:可可西里 更新时间:2023-11-01 01:54:52 24 4
gpt4 key购买 nike

    <?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>
alert("Full Name is required!");
window.location.href = "registeruser.php";
</script>
<?php
}

else
{
if ($userName == "")
{
?>
<script>
alert("Invalid username!");
window.location.href = "registeruser.php";
</script>
<?php
}

else
{
if($userName == $result['USERNAME'])
{
?>
<script>
alert("Username already exists!");
window.location.href = "registeruser.php";
</script>
<?php
}

else
{
if ($emailAdd == $result['EMAIL'])
{
?>
<script>
alert("Email address is required!");
window.location.href = "registeruser.php";
</script>
<?php
}

else
{
if ($passWord == "")
{
?>
<script>
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>
alert("Registered Successfully!");
window.location.href = "index.php";
</script>
<?php
}

else
{
?>
<script>
alert("Password and Confirm Password do not match");
window.location.href = "registeruser.php";
</script>
<?php
}
}
}
}
}
}

?>

它工作正常。但问题是:警报窗口显示到另一个网页,而不是在网页内 没关系,但是将用户引导到另一个没有内容的网页看起来很糟糕。如果您能帮助我,请告诉我如何解决此问题并使 javascript 警报窗口仅显示在同一网页上。实际上,我已经尝试过一些这样的解决方案..

if($fullName == "")
{
echo '<script>';
echo 'alert("Full Name is required!")';
echo 'window.location.href = "registeruser.php"';
echo '</script>';
}

最佳答案

您好,您尝试将 ajax 与 jquery 结合使用吗

现在你的脚本只是返回一个 json 对象来说明是否一切正常 //reg.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));

和 //注册.php

<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>

关于Javascript 警报窗口重定向到另一个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com