下面是我的ajax:它显示从php脚本从数据库中获取的数据。我在php页面中var_dump结果来查看数组大小。它表示数组大小(11)。
但是当传递给ajax页面时,它会返回11次数据两次。我使用console.log($ data.length)来查看返回的数据集的数量。它虽然显示了11次。在我的下面的代码中,是什么让循环显示两次?
$("form").on("submit", function () {
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "ajax2.php",
data: data,
success: function (data) {
getData=data;
$("#main_content").slideUp("normal",function(){
$(".the-return").show();
if(data!=""){
console.log(data.length);
for (i = 0; i < data.length; i++) {
$(".the-return").append("<div class='inside_return'><section class='row'><div class='small-12 medium-6 large-10 large-centered columns d_result'><div class='text-center post_id'>" + data[i].id +"</div><div class='small-12 medium-12 large-9 columns subject'>"+ data[i].subject+" Instructor</div><div class='small-12 meidum-12 large-3 columns rate'>" + data[i].rate +"</div><span class='hourly'>hourly</span><div class='small-12 medium-12 large-12 text-center name'>" + data[i].name +"</div><div class='small-12 medium-12 large-4 columns plusneg'><img src='img/happy.png'>" + data[i].plus+"<br/><img src='img/sad.png'>"+ data[i].neg+"</div><div class='small-12 medium-12 large-4 columns gender text-center'>Male<br/>Availabiliy:<br/>Mornings:" + data[i].weekM+"<br/>Afternoons:<br/>"+ data[i].endA+"Evening:"+ data[i].weekE+"</div><div class='small-12 medium-12 large-4 columns score text-center'><h6>Reputation score</h6>"+ data[i].reputation+"</div><div class='row'><div class='small-12 medium-12 large-5 columns shortlist text-center'>Add to Shorlist</div><div class='small-12 medium-12 large-5 columns gettutor text-center'>Get Tutor</div></div><div class='small-12 medium-12 large-12 columns text-center date'>Posted on:<span class='elec'>15 Apr 2015</span></div></div></section></div>");
//alert(data[i].name)
}
}else{
$(".the-return").hide();
$(".the-return-fail").slideDown(1000);
}
});//closes #main_content
}
});
return false;
});
PHP脚本
<?php
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$return = $_POST;
//$return='{"sub":{"1":"","2":""},"postcode":"56000","slider1":"100","action":"test"}';
$return["json"] = json_encode($return);
// echo json_encode($return);
$data = json_decode($return["json"], true);
//var_dump($data);
include('inc/config.php');
$postcode=$data['postcode'];
$rate=$data['slider1'];
$key =array_keys($data['sub']);
//print_r($key);
$pricing2 = $data['slider1'];
$pricing = $pricing2 * 1.90;
$postcode = $data['postcode'];
$bindings = array();
$bindings[] = array(":pricing", $pricing, PDO::PARAM_STR);
$bindings[] = array(":postcode", $postcode, PDO::PARAM_STR);
$key_placeholders = array();
foreach($key as $k => $v) {
$placeholder = ":subid".$k;
$bindings[] = array($placeholder, $v, PDO::PARAM_INT);
$key_placeholders[] = $placeholder;
}
$sql = "SELECT * FROM posts,subjects "
. "WHERE posts.subid IN (". implode(",",$key_placeholders).") "
. "AND posts.pricing <=:pricing "
. "AND posts.Poscode=:postcode "
. "AND posts.subid=subjects.subid "
. "ORDER BY posts.Poscode "
. "DESC LIMIT 60";
$statement = $pdo->prepare($sql);
foreach($bindings as $b) {
$statement->bindValue($b[0],$b[1],$b[2]);
}
$statement->execute();
$json = array();
while( $row = $statement->fetch()) {
array_push($json, array("name" => $row['Name'], "id" => $row['PostUUID'],"rate" => $row['pricing'],"postcode" => $row['Poscode'],"subject" => $row['subname'],"reputation" => $row['Reputation'],"plus" => $row['ReviewPlus'],"neg" => $row['ReviewNeg'],"weekM" => $row['week_morning'],"weekA" => $row['week_afternoon'],"weekE" => $row['week_evening'],"endM" => $row['weekend_morning'],"endA" => $row['weekend_afternoon'],"endE" => $row['weekend_evening'],"postDate" => $row['date']));
}
header('Content-Type: application/json');
echo json_encode($json);
//var_dump($json);
}
?>
答案 0 :(得分:0)
我在提交表单后添加了这一行,以避免双重提交。
$('form').submit(function(){
**$(this).find(':submit').attr('disabled','disabled');**
});