【发布时间】:2018-07-28 11:02:18
【问题描述】:
我的网站上有一个注册页面,我想对其实施 PHP 和 AJAX 验证。我将展示我的代码模板(我的代码要大得多)以尝试解释问题。
registration.php
<!DOCUMENT html>
<html>
<head>
<script>
$(document).ready(function(){
$("#signupForm").submit(function(event){
event.preventDefault();
var firstNameInput = $("#firstName").val();
var lastNameInput = $("#firstName").val();
$(".messageForm").load("inculdes/signup.script.php", {
firstName: firstNameInput,
lastName: lastNameInput
});
});
});
</script>
</head>
<body>
<form id="signupForm" action="includes/signup.script.php" method="POST">
<input id="firstName" type="text" name="firstName">
<input id="firstName" type="text" name="firstName">
<button id="submit" name="submit" type="submit">Submit</button>
<div class="messageForm"></div>
</form>
</body>
</html>
signup.script.php
<?php
if (isset($_POST['submit'])) {
include "dbconn.script.php";
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
//Validators for inputs in PHP with various error messages (1-15) and database INSERT and SELECT functions.
...
} else {
echo "<span>Error 16</span>";
exit();
}
}
?>
<script>
// JQuery validator for inputs and CSS style changers based on errors
...
</script>
我的问题是当我单击提交按钮时,我收到错误消息“错误 16”,所以我的函数跳过整个 PHP 脚本并且不会将数据插入数据库。我得出的结论是这个问题是因为变量$_POST['submit']没有设置,因为当我将第一行代码更改为if (!isset($_POST[submit'']))时,它就像魅力一样,但我不想失去阻止用户输入signup.script.php的能力在浏览器的 URL 字段中并运行它。如何纠正这个工作?我看过的教程有这段代码,在这种情况下它可以正常工作。
附:如果需要解决这个问题,我会扩展我的代码,因为我想节省空间,所以我给出了它的简写版本。
【问题讨论】:
-
抱歉,这些是我现在的打字错误,我的原始代码中没有这些错误。我在这里更正了它们。
-
您根本不发送
submit的请求。您可以在此处添加:{ firstName: firstNameInput, lastName: lastNameInput, submit: 1 }- 或检查isset($_POST['firstName']) -
“抱歉,这些是我现在的打字错误” - 请不要为 SO 重写代码。对您的实际代码进行真正的复制/粘贴。
-
inculdes这也只是一个错字吗?这个词拼写为“包括”。 -
^ ...这就是您需要发布实际代码的原因,因此我们不会坐下来调试实际上并不存在的错误。
标签: javascript php jquery ajax forms