试图通过ajax调用php页面

时间:2014-05-03 10:13:20

标签: php jquery ajax

提交提醒警报正在运行,但在ajax代码中无效。 在var中的值存储并转到ajax页面,我试图使用mysql插入查询,但它无法正常工作。我在ajax成功结果后不显示,一秒ajax文件名显示在NET(错误控制台)

JQuery代码:

$(document).ready(function(){   
$('#submit').click(function(){
var project_ID = $('#project_name').val();
var accommodation=$('#accommodation').val();
var no_of_floors=$('#no_of_floors').val();
var no=$('#no').val();
 if(project_ID==''){
    alert('Select Project Name');
}
if(accommodation==''){
    alert('Select Accomodation Scale');
}
 if($('#typeI').prop('checked')){
    var type1=$('#typeI').val();
    }
if($('#typeII').prop('checked')){
    var type2=$('#typeII').val();
    }
if($('#typeIII').prop('checked')){
    var type3=$('#typeIII').val();
    }
if($('#typeIV').prop('checked')){
    var type4=$('#typeIV').val();
    }
    if(project_ID!="" && accommodation!='') 
   {
    $.ajax({url:"villa_Accomo_type.php?project_ID="+project_ID
   +"&accommodation="+accommodation+"&type1="+type1+"&type2="+type2
   +"&type3="+type3+"&type4="+type4+"&no_of_floors="+no_of_floors+"&no="+no,
      success:function(result){
          res=result;
        alert(res);

     }

}); 
   }
 });
 });

AJAX PAGE

    <?php
$project_ID    = $_GET['project_ID'];
$accommodation = $_GET['accommodation'];
$type1         = $_GET['type1'];
$type2         = $_GET['type2'];
$type3         = $_GET['type3'];
$type4         = $_GET['type4'];
$no_of_floors  = $_GET['no_of_floors'];
$no            = $_GET['no'];

echo $insert = "insert into villas_scale (project_id,scale,type,
  no_of_floors,no)    values('$project_ID',
'$accommodation','$type1','$no_of_floors','$no')";
mysql_query($insert);

echo $insert2 = "insert into villas_scale (project_id,scale,
 type,no_of_floors,no) values('$project_ID',
'$accommodation','$type2','$no_of_floors','$no')";
mysql_query($insert2);

echo $insert3 = "insert into villas_scale (project_id,scale,
  type,no_of_floors,no) values('$project_ID',
'$accommodation','$type3','$no_of_floors','$no')";
mysql_query($insert3);
echo $insert4 = "insert into villas_scale (project_id,scale,
 type,no_of_floors,no) values('$project_ID',
'$accommodation','$type4','$no_of_floors','$no')";
mysql_query($insert4);

?> 

1 个答案:

答案 0 :(得分:1)

我必须在代码中编辑这么多错误。jsFiddle

var no = $('#no').val();
if (project_ID === '') {
    alert('Select Project Name');
}
if (accommodation === '') {
    alert('Select Accomodation Scale');
}
if ($('#typeI').prop('checked')) {
    var type1 = $('#typeI').val();
}
if ($('#typeII').prop('checked')) {
    var type2 = $('#typeII').val();
}
if ($('#typeIII').prop('checked')) {
    var type3 = $('#typeIII').val();
}
if ($('#typeIV').prop('checked')) {
    var type4 = $('#typeIV').val();
}
if (project_ID !== "" && accommodation !== '') {
    $.ajax({
        url: "villa_Accomo_type.php",//This should the url where you send data
        type: 'GET',
        data: project_ID = "project_ID" + project_ID + " & accommodation = " + accommodation + " & type1 = " + type1 + " & type2 = " + type2 + " & type3 = " + type3 + " & type4 = " + type4 + " & no_of_floors = " + no_of_floors + " & no = " + no, // here you need the specify your serialize object 
        success: function (result) {
            alert(result);

        }

    });
}