所以我想提交一些数据(从表单)到服务器数据库使用AJAX,但由于某种原因,它接缝,AJAX脚本根本不工作。当我提交表单时,PHP脚本执行没有问题。我到处寻找答案,但迄今为止没有任何工作。这可能是愚蠢的,但我需要一些帮助。AJAX/PHP没有工作
的Jquery/AJAX脚本:
$(document).ready(function() {
$('#InputForm').submit(function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value){
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
async: true,
type: type,
data: data,
success: function(response){
$(#Suc_Sub).fadeIn(800);
}
});
return false;
});
});
PHP和HTML脚本:
$url = "";
$email = "";
$comment = "";
$stage = "";
$url_error = "";
$email_error = "";
$comment_error = "";
if(!empty($_POST['websiteURL']))
{
$url = $_POST['websiteURL'];
$regex = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
if (!preg_match($regex, $url))
{
$url_error = "Invalid Url";
}
}
else
{
$url_error = "Url is blank";
}
if(!empty($_POST['userEmail']))
{
$email = $_POST['userEmail'];
$email = fix_input($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$email_error = "Invalid email";
}
}
else
{
$email_error = "Email is blank";
}
if(!empty($_POST['userComment']))
{
$comment = $_POST['userComment'];
$comment = fix_input($comment);
$regex = "/^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/";
if (!preg_match($regex, $comment))
{
$comment_error = "Invalid Comment";
}
}
else
{
$comment_error = "Comment is blank";
}
$stage = $_POST['websiteStage'];
if(empty($email_error) && empty($url_error) && empty($comment_error))
{
$date_parts = getdate(date("U"));
$post_date = "$date_parts[mday]-$date_parts[mon]-$date_parts[year] - $date_parts[hours]:$date_parts[minutes]:$date_parts[seconds]";
mysqli_query($con,"INSERT INTO submit (URL,EMAIL,COMMENT,STAGE,Date)
VALUES ('$url','$email','$comment','$stage','$post_date')");
die();
}
else
{
die();
}
<form method="post" id="InputForm" action="..\Scripts\sbt.php">
<fieldset style="border:none">
<ul style="list-style-type: none;">
<li>
<label for="websiteURL"> <img src="..\Images\blank tick.png" id="Website_Image" height="50" width="60"> </label> <!--Dont forget about the label pics-->
<input size="25" autocomplete="off" title="Type your URL" type="url" id="websiteURL_Box" class="TextInput" name="websiteURL" required="required" placeholder="Your Website URL..." autofocus="autofocus" maxlength="100"/>
<div id="flyout_hidden_url" hidden></div>
</li><br>
<li>
<label for="userEmail"> <img src="..\Images\blank tick.png" id="Email_Image" height="50" width="60"> </label> <!--Dont forget about the label pics-->
<input size="25" autocomplete="off" title="Your Email plz" type="email" id="userEmail_Box" class="TextInput" name="userEmail" required="required"required="required" placeholder="Your Email..." autofocus="autofocus" maxlength="100"/>
<div id="flyout_hidden_email" hidden></div>
</li><br>
<li>
<label for="userComment"> <img src="..\Images\blank tick.png" id="Comment_Image" height="50" width="60"> </label> <!--Dont forget about the label pics-->
<input size="25" autocomplete="off" title="Your Comment" type="text" id="userComment_Box" class="TextInput" name="userComment" placeholder="Any comments...?" autofocus="autofocus" maxlength="100"/>
<div id="flyout_hidden_comment" hidden></div>
</li><br>
<li>
<label for="websiteStage"> </label>
<select name="websiteStage" class="custom">
<option value="Alpha">Alpha Version</option>
<option value="Beta">Beta Version</option>
<option value="Finished">Finished</option>
</select>
</li><br>
<li>
<label id="botTest_Label" for="botQuestion">I am not a cyborg!</label> <!--Dont forget about the label pics-->
<input type="checkbox" required="required" value="botQ" id="botTest_Box" title="For Bot testing!">
</li><br>
<input id="SubmitButton" type="submit" disabled value="Submit">
</ul>
</fieldset>
</form>
任何形式的帮助表示赞赏。非常感谢。
什么“不起作用”?任何错误消息?你打开SQL注入,使用准备好的语句.. – chris85
我们无法为你调试。当你调试它,特别是它失败?当你逐步浏览JavaScript代码时,它是否符合你的期望?是否发出POST请求?它是否包含您期望的数据?服务器的回应是什么? – David
当我提交表单时,它只是执行PHP脚本。而当我检查控制台日志,看看是否有什么错误,它不说什么。它像它不存在。 – GeorgeS