gpt4 book ai didi

javascript - 带有警报的 javascript 问题未显示

转载 作者:行者123 更新时间:2023-12-02 22:49:43 25 4
gpt4 key购买 nike

嗨,我有这个注册来将新用户注册到数据库,但是注册用户的 JavaScript 警报不起作用,但是例如在同一页面上登录用户正在工作......我的意思是如果你失败了密码它会显示密码错误,但是如果您尝试注册用户输入数据库但没有任何警报说“新记录创建成功”

--------------------------------注册到数据库并发出警报-------- ---------------

<?php
session_start();

$db=mysqli_connect("localhost","root", "","compras");
if(isset($_POST['signup'])) {

$fname = ($_POST['fname']);
$lname = ($_POST['lname']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$phone = ($_POST['phone']);
$sql;
$password=md5($password);

if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Insert into users(fname,lname,phone,email,password) values ('$fname','$lname','$phone','$email','$password')";

if (mysqli_query($db, $sql))
{
echo '<script type="text/javascript">alert("New record created successfully");</script>';

header("Location:http://localhost/Fly With US/index.php");
}
else
{
echo '<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/index.php");

echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}
?>

--------------------------------主页------------------------ --------------------------

    <?php
session_start();
if(isset($_SESSION['username'])){
header("Location: cat_type.php");
}
?>
<html>
<head>
<link rel="stylesheet" href="css/head.css">
<link rel="icon" href="res/flywithus-pt.png" sizes="16x16">
<link href="https://fonts.googleapis.com/css?family=Bgrree+Serif" rel="stylesheet">
<script type="text/javascript" src="scripts/signupForm.js"></script>
<script type="text/javascript" src="scripts/loginForm.js"></script>
<title>Fly With Us | Buy Drones and Computers.</title>
</head>
<body>
<h1><center><img src="res/flywithus-png.png"></center></h1>



<!-- LOGIN -->

<div id="login">
<form name="loginForm" method="post" action="log.php">
<table align="left" width="150%">
<tr>
<td align="center">
<h2>Login</h2>
</td>
</tr>
<tr>
<td>
<input type="text" name="username" id="user" placeholder="Email" required>
</td>
</tr>
<br>
<tr>

<td>
<input type="password" name="password" id="pass" placeholder="Password" required>
</td>
</tr>
<tr>

<td>
<input type="submit" name="login" value="Let me in.." class="click">
</td>
</tr>
<!--
<tr>
<td colspan="2" class="signin">
<input type="image" src="signin_google.png" alt="Login with Google" width="50%" height="50%">

</td>
</tr>
<tr>
<td colspan="2" class="signin">
<input type="image" src="signin_fb.png" alt="Login with FB" width="50%" height="50%">

</td>
</tr>
//-->
</table>
</form>
</div>


<!-- SIGNUP -->

<form name="signupForm" method="post" action="pro.php" id="signup" onsubmit="return ValidateFname() || ValidateLname() || ValidateEmail() || ValidateMobile() ">
<table align="right">
<tr>
<td align="center" colspan="2">
<h2>New Here? Register with us..</h2>
</td>
</tr>
<tr>
<td>
<input type="text" name="fname" placeholder="First Name" required>
</td>
<td>
<input type="text" name="lname" placeholder="Last Name" required>
</td>
</tr>

<tr>
<td colspan="2">
<input type="email" name="email" placeholder="Email" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="text" name="phone" placeholder="Contact Number" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="password" name="password" placeholder="Password" required>
</td>
</tr>

<tr>
<td colspan="2">
<input type="submit" name="signup" value="Sign Up" class="click">
</td>
</tr>

</table>
</form>

</body>
</html>

最佳答案

这是因为 PHP (header) 仍会在 JavaScript (alert) 之后读取。换句话说,警报实际上已显示,但同时加载index.php,因此警报似乎未显示。

不起作用:

echo '<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/index.php");

可以工作:

echo '<script type="text/javascript">
alert("Error Occured");
window.open("index.php","_self");
</script>';

关于javascript - 带有警报的 javascript 问题未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58232608/

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