我有一个带模态的页面。模态代码在其自己的PHP文件中,名为" tour-modal.php":
<div class="modal fade" id="tourModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="errors2"></div>
<form class="tour-card" action="insert.php" method="post">
<div class="form-group col-sm-12">
<input type="text" class="form-control" id="firstNameTourCard" placeholder="First Name"/>
<input id="middleInitialTour" type="hidden" value="">
<input type="text" class="form-control" id="lastNameTourCard" placeholder="Last Name"/>
<input type="email" class="form-control" id="emailTourCard"/>
<input type="tel" class="form-control" id="phoneTourCard"/>
<select class="form-control" id="tourDate">
<option>May 25 2016</option>
<option>May 28 2016</option>
</select>
<select class="form-control" id="tourTime">
<option>1:00 PM</option>
<option>1:30 PM</option>
<option>2:00 PM</option>
</select>
</div>
<input id="tour-btn" type="button" value="SUBMIT" class="btn btn-default tour-button"/>
<button type="button" class="btn btn-default tour-button" data-dismiss="modal">CLOSE</button>
</form>
</div>
</div>
</div>
</div>
在同一个文件中,我有一个像这样的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>
最后,我有&#34; tour-form.php&#34;在AJAX调用中引用的文件:
<?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));
?>
问题是我尝试这次SQL查询实际执行的次数只有5次。调用AJAX调用,并且PHP文件成功结束,模式关闭,但INSERT
语句很少实际工作。任何人都可以看到任何错误吗?
答案 0 :(得分:0)
使用我的代码几乎所有评论的材料,我继续找到我的答案,只需要进行一些调整。谢谢大家!
&#34; tour-form.php&#34;:
的结束$sth = $db->prepare($sql);
#success = $sth->execute();
$result = array(
"success" => $success);
die(json_encode($result));
新脚本:
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);
});