表单操作不起作用,用PHP插入数据Ajax

时间:2019-04-09 04:14:07

标签: php ajax

我想使用PHP,MySqli,AJax从数据库中插入数据而不刷新。,但是表单操作无法正常工作。我有一个表名是'tbl_user'和字段'source','source1','source2','adult','child','infant'并且还使用了同名ID ,请分享这个想法

**index.php**

<div class="message_box" style="margin:10px 0px;">//ajax Action Massage for use

    <form action='' name="ContactForm" method='post'>

       <select name="source" id="source"  class="custom-select-Source">

           <option selected="selected" disabled="disabled" value="">Source </option>

           <option value="Online">Online</option>

           <option value="Offline">Offline</option>

       </select>

       <div class="custom-select-area">

            <select name="source1" id="source1"  class="custom-select-Source1">

                 <option selected="selected" disabled="disabled" value=""> Source1 </option>

                 <option value="JD">JD</option>

                 <option value="Direct">Direct</option>

                 <option value="Facebook">Facebook</option>

                 <option value="Instagram">Instagram</option>

                 <option value="Inbound">Inbound</option>

                 <option value="Group Departure">Group Departure</option>

                 <option value="Direct Mail">Direct Mail</option>

                 <option value="B2B">B2B</option>

                 <option value="Yuvatrip">Yuvatrip</option>

         </select>

        <input type="text" name="source2" id="source2"   placeholder="Source2" class="input-group-Source2"> 
        </div>

        <input type="number"  name="adult" id="adult"  placeholder="adult" class="adult">

        <input type="number" name="child" id="child"  placeholder="child" class="child">

        <input type="number" name="infant" id="infant"  placeholder="infant" class="infant">

        <p style="text-align: center; margin-top:20px;">

            <button type="submit" id="submit" class="btn btn-default submit-bt">Submit</button>

php脚本在这里

在没有ajax的情况下php代码工作良好,但我在有aphp的情况下却需要ajax

    <?php
        session_start();

        date_default_timezone_set('Australia/Melbourne');

        if(isset($_POST['submit'])){

           $source = mysqli_real_escape_string($conn, $_POST['source']);

           $source1 = mysqli_real_escape_string($conn, $_POST['source1']);

           $source2 = mysqli_real_escape_string($conn, $_POST['source2']);

           $adult = mysqli_real_escape_string($conn, $_POST['adult']);

           $child = mysqli_real_escape_string($conn, $_POST['child']);

           $infant = mysqli_real_escape_string($conn, $_POST['infant']);

           $reg_id = rand();

           $created = date('y-m-d-h-i-s');

    $query = "INSERT INTO tbl_user(source,source1,source2,adult,child,infant,reg_id,created) 

    VALUES('$source','$source1','$source2','$adult','$child','$infant','$reg_id','$created')";

      if(mysqli_query($conn, $query)){

       $_SESSION['reg_id']=$reg_id;

       header('Location:index.php');

         }  
      }
 ?>

此处的ajax脚本不能正常工作

$(document).ready(function() {

   var delay = 2000;

   $('.btn-default').click(function(e){

   e.preventDefault();

   // youse source field

   var source = $('#source').val();

   if(source == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your source!</span>'
   );

   $('#source').focus();

   return false;
   }

  //use for  source1 field

   var source1 = $('#source1').val();

   if(source1 == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your source1!</span>'
   );
   $('#source1').focus();

   return false;
   }

 //use for  source2 field

   var source2 = $('#source2').val();

   if(source2 == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your source2!</span>'
   );

   $('#source2').focus();

   return false;
   }

   //use for  adult field

   var adult = $('#adult').val();

   if(adult == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your adult!</span>'
   );

   $('#adult').focus();

   return false;
   }

   //use for  child field

   var child = $('#child').val();

   if(child == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your child!</span>'
   );

   $('#child').focus();

   return false;
   }

   //use for  infant field

   var infant = $('#infant').val();

   if(infant == ''){

   $('.message_box').html(

   '<span style="color:red;">Enter Your infant!</span>'
   );

   $('#infant').focus();

   return false;
   }

//use for  ajax Action  

   $.ajax
   ({
   type: "POST",

   url: "index.php",

   data: "source="+source+"source1="+source1+"source2="+source2+"adult="+adult+"child="+child+"infant="+infant,

   beforeSend: function() {

   $('.message_box').html(

   '<img src="Loader.gif" width="25" height="25"/>'
   );

   },        
   success: function(data)
   {
   setTimeout(function() {

   $('.message_box').html(data);

   }, delay);

   }
   });
   });

});

1 个答案:

答案 0 :(得分:0)

您正尝试使用AJAX发出POST请求,但您正在以字符串形式发送数据,这会导致问题,请尝试这种方式,

$.ajax
   ({
   type: "POST",

   url: "index.php",
    dataType: "json",
   data:{
       "source":source,
       "source1":source1,
       "source2":source2,
       "adult":adult,
       "child":child,
       "infant":infant
   }, 

   beforeSend: function() {

   $('.message_box').html(

   '<img src="Loader.gif" width="25" height="25"/>'
   );

   },        
   success: function(data)
   {
   setTimeout(function() {

   $('.message_box').html(data);

   }, delay);

   }
   });
   });