Javascript 为什么通过AJAX调用插入PHP MySQL只是偶尔发生?
我有一个带有模态的页面。模态代码位于其自己的名为“tour modal.PHP”的PHP文件中:Javascript 为什么通过AJAX调用插入PHP MySQL只是偶尔发生?,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我有一个带有模态的页面。模态代码位于其自己的名为“tour modal.PHP”的PHP文件中: &时代; 2016年5月25日 2016年5月28日 下午1:00 下午1:30 下午2:00 接近 在同一个文件中,我有一个jQuery/AJAX脚本,如下所示: <script> jQuery("#tour-btn").click(function (e) { var str = "firstNameTourCard=" + jQuery("#firstN
&时代;
2016年5月25日
2016年5月28日
下午1:00
下午1:30
下午2:00
接近
在同一个文件中,我有一个jQuery/AJAX脚本,如下所示:
<script>
jQuery("#tour-btn").click(function (e) {
var str = "firstNameTourCard=" + jQuery("#firstNameTourCard").val()
+ "&lastNameTourCard=" + jQuery("#lastNameTourCard").val()
+ "&emailTourCard=" + jQuery("#emailTourCard").val()
+ "&phoneTourCard=" + jQuery("#phoneTourCard").val()
+ "&tourDate=" + jQuery("#tourDate").val()
+ "&tourTime=" + jQuery("#tourTime").val()
+ "&middleInitialTour=" + jQuery("#middleInitialTour").val();
jQuery.ajax({
type: "post",
url: "/wp-content/themes/myTheme/tour-form.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
console.log ('success');
jQuery('#tourModal').modal('hide');
} else {
console.log ('failure');
jQuery(".errors2").html(result.errors2);
}
}
});
});
</script>
jQuery(“#tour btn”)。单击(函数(e){
var str=“firstNameTourCard=“+jQuery”(#firstNameTourCard”).val()
+“&lastNameTourCard=“+jQuery”(#lastNameTourCard”).val()
+“&emailTourCard=“+jQuery”(#emailTourCard”).val()
+“&phoneTourCard=“+jQuery(#phoneTourCard”).val()
+“&tourDate=“+jQuery(#tourDate”).val()
+“&tourTime=“+jQuery(#tourTime”).val()
+“&middleInitialTour=“+jQuery”(#middleInitialTour”).val();
jQuery.ajax({
类型:“post”,
url:“/wp content/themes/myTheme/tour form.php”,
数据:str,
数据类型:“json”,
成功:功能(结果){
如果(result.success==1){
console.log(“成功”);
jQuery('#tourModal').modal('hide');
}否则{
console.log(“失败”);
jQuery(“.errors2”).html(result.errors2);
}
}
});
});
最后,我有一个在AJAX调用中引用的“tour form.php”文件:
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=MYDBNAME;charset=utf8',
'USERNAME',
'PASSWORD');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $ex) {
echo "did not connect...";
}
$firstNameTourCard = trim(isset($_POST['firstNameTourCard']) ? $_POST['firstNameTourCard'] : '');
$lastNameTourCard = trim(isset($_POST['lastNameTourCard']) ? $_POST['lastNameTourCard'] : '');
$emailTourCard = trim(isset($_POST['emailTourCard']) ? $_POST['emailTourCard'] : '');
$phoneTourCard = trim(isset($_POST['phoneTourCard']) ? $_POST['phoneTourCard'] : '');
$tourDate = trim(isset($_POST['tourDate']) ? $_POST['tourDate'] : '');
$tourTime = trim(isset($_POST['tourTime']) ? $_POST['tourTime'] : '');
if ($_POST['middleInitialTour'] != '') {
$result = array(
"success" => 1);
die(json_encode($result));
}
$errors2 = Array();
if (sizeof($_POST) > 0) {
if ($emailTourCard === '' || !filter_var($emailTourCard, FILTER_VALIDATE_EMAIL)) {
$errors2[] = '<span style="color:red;"> Please enter a valid email address. </span>';
}
}
if (sizeof($errors2) > 0 || sizeof($_POST) == 0) {
$result = array(
"errors2" => implode("<br/>", $errors2),
"success" => 0);
die(json_encode($result));
}
$randID2 = md5(uniqid(rand(), true));
$sql="INSERT INTO tours (id, first_name, last_name, email, phone, tour_date, tour_time, signup_date)
VALUES
('".$randID2."', '".$firstNameTourCard."', '".$lastNameTourCard."', '".$emailTourCard."', '".$phoneTourCard."', '".$tourDate."', '".$tourTime."', NOW())";
$sth = $db->prepare($sql);
$sth->execute();
$result = array(
"success" => 1);
die(json_encode($result));
?>
我使用了几乎所有代码评论中的材料,然后找到了我的答案,只需要做一些调整。谢谢大家
“tour form.php”的结尾:
新脚本:
jQuery("#tour-btn").click(function (e) {
jQuery('#tour-btn').attr("disabled", true);
function joinTour(str) {
jQuery.ajax({
type: "post",
url: "/wp-content/themes/myTheme/tour-form.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
console.log ('success');
jQuery('#tourModal').modal('hide');
jQuery('#tour-btn').attr("disabled", false);
} else {
console.log ('failure');
if (result.errors2) {
jQuery(".errors2").html(result.errors2);
jQuery('#tour-btn').attr("disabled", false);
}
if (!result.errors2) {
setTimeout(function () {
joinTour(str);
}, 500)
}
}
}
});
}
var str = $(form").serialize();
joinTour(str);
});
我使用了几乎所有代码注释中的材料,然后找到了我的答案,只需要做一些调整。谢谢大家
“tour form.php”的结尾:
新脚本:
jQuery("#tour-btn").click(function (e) {
jQuery('#tour-btn').attr("disabled", true);
function joinTour(str) {
jQuery.ajax({
type: "post",
url: "/wp-content/themes/myTheme/tour-form.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
console.log ('success');
jQuery('#tourModal').modal('hide');
jQuery('#tour-btn').attr("disabled", false);
} else {
console.log ('failure');
if (result.errors2) {
jQuery(".errors2").html(result.errors2);
jQuery('#tour-btn').attr("disabled", false);
}
if (!result.errors2) {
setTimeout(function () {
joinTour(str);
}, 500)
}
}
}
});
}
var str = $(form").serialize();
joinTour(str);
});
发生这种情况时,您是否检查了错误日志或查看了浏览器的控制台?只需添加问题代码而不是POROJECT:-)@JayBlanchard我可以在哪里?F12打开浏览器的控制台和开发人员工具。一些错误将在那里报告。您的日志将位于Web服务器上,位置取决于服务器类型和操作系统。@JayBlanchard的做法是正确的。从编程的角度来看,作为旁注,您应该观察PDO的执行,并使用它来返回成功或失败。类似这样的内容:$success=$sth->execute()$结果=数组(“成功”=>$success)代码>-因此结果“成功”是准确的,而不是总是“正确”发生这种情况时,您是否检查了错误日志或查看了浏览器的控制台?只需添加问题代码而不是POROJECT:-)@JayBlanchard哪里可以?F12打开浏览器的控制台和开发工具。一些错误将在那里报告。您的日志将位于Web服务器上,位置取决于服务器类型和操作系统。@JayBlanchard的做法是正确的。从编程的角度来看,作为旁注,您应该观察PDO的执行,并使用它来返回成功或失败。类似这样的内容:$success=$sth->execute()$结果=数组(“成功”=>$success)代码>-因此结果“成功”是准确的,而不是始终“正确”
jQuery("#tour-btn").click(function (e) {
jQuery('#tour-btn').attr("disabled", true);
function joinTour(str) {
jQuery.ajax({
type: "post",
url: "/wp-content/themes/myTheme/tour-form.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
console.log ('success');
jQuery('#tourModal').modal('hide');
jQuery('#tour-btn').attr("disabled", false);
} else {
console.log ('failure');
if (result.errors2) {
jQuery(".errors2").html(result.errors2);
jQuery('#tour-btn').attr("disabled", false);
}
if (!result.errors2) {
setTimeout(function () {
joinTour(str);
}, 500)
}
}
}
});
}
var str = $(form").serialize();
joinTour(str);
});