Javascript 在sql语句成功后尝试重定向到页面时出现白色屏幕
一旦数据成功添加到sql表中,我将尝试重定向到新页面,但是当我输入Javascript 在sql语句成功后尝试重定向到页面时出现白色屏幕,javascript,php,mysql,sql,Javascript,Php,Mysql,Sql,一旦数据成功添加到sql表中,我将尝试重定向到新页面,但是当我输入 header("location: success.php"); exit; 代码,我只是在加载页面时看到一个白色屏幕 $('#submit').click(function () { /* when the submit button in the modal is clicked, submit the form */ <?php $
header("location: success.php");
exit;
代码,我只是在加载页面时看到一个白色屏幕
$('#submit').click(function () {
/* when the submit button in the modal is clicked, submit the form */
<?php
$sql = "INSERT INTO APPOINTMENT(Weekstart, Day, APTime) VALUES ('01-01-2020', 'T', '9:00')";
if (mysqli_query($conn, $sql)) {
header("location: success.php");
exit;
} else {
header("location: no.php");
exit;
}
?>
});
$('#提交')。单击(函数(){
/*单击模式中的“提交”按钮后,提交表单*/
您正在javascript代码中使用php代码
它将在页面加载时执行,而不是在单击按钮时执行
创建一个sperate php文件,并将php代码放入其中。假设test.php并在模式中使用blewo表单,无需javascipt
<form action="test.php" method="post">
/* your input parameters*/
<input type="submit">
</form>
/*您的输入参数*/
你试过写“Location”而不是“Location”?你有没有像“Header ready sent”这样的错误?使用大写不起作用,我在Chrome中没有收到任何错误当我删除exit语句时,我得到了一个Header ready sent error如果你的代码与写的完全一样(即PHP生成页面,与生成的JS混合),它将不起作用,因为在header()语句之前,您已经将内容发送到浏览器(HTML和JS)。您应该将处理表单提交的PHP代码移到代码的顶部,或者更好地移到它自己的文件中,并且retryJavascript在客户端运行,PHP在服务器端运行。对于这一点,您必须使用ajax或表单提交。有什么办法解决吗?