在表格PHP AJAX SQL的一个字段中输入数据时完成剩余的表单字段

时间:2017-12-07 07:56:43

标签: php jquery ajax

Javascript代码: 这里当在票号字段中输入值时,我想调用ajax并从fill.php文件中检索数据。

        <script type="text/javascript">
          $("#tktNum").change(function() {
            var ticket = $('#tktNum').val();
        alert(ticket);
            $.ajax({
              type: "POST",
              dataType: 'json',
              data:"ticket="+ticket,
              url: "fill.php", 
                 success: function(json) {
                 $('#bookNum').val(json.booking);
                 $("#email").val(json.email);
                 $("#mobile").val(json.mobile);
                 $("#fName").val(json.fname);
                 $("#lName").val(json.lname);
                 $("#mName").val(json.mname);
                 $("#airline").val(json.airline);
               },
               error : function(){
                 alert('Some error occurred!');
               }
             });
          });
        </script>

    FORM: This is the form where data should appear dynamically upon entering the value in ticket number field. The field with id tktNum is where the key change option will work.

    <div class="form-group col-lg-6">
                    <input name="bookNum" value="" id="bookNum"   class="form-control" aria-required="true" aria-invalid="false" required placeholder="Booking Number/PNR*" type="text">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="tktNum" id="tktNum" value="" class="form-control" aria-required="true" aria-invalid="false" required placeholder="Ticket Number*" type="text">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="fName" id="fName" value="" class="form-control" aria-required="true" aria-invalid="false" style="text-transform: capitalize;" required placeholder="First Name*" type="text">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="mName" id="mName" value="" class="form-control" aria-required="true" aria-invalid="false" style="text-transform: capitalize;" placeholder="Middle Name (if applicable)" type="text">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="lName" id="lName" value="" class="form-control validates-as-required" aria-required="true"style="text-transform: capitalize;" required aria-invalid="false" placeholder="Last Name*" type="text">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="mobile" id="mobile" value="" class="form-control validates-as-required" aria-required="true" required aria-invalid="false" placeholder="Mobile*" type="tel">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="email" value="" class="form-control validates-as-required" aria-required="true" required aria-invalid="false" placeholder="Email*" type="email">
                  </div>

                  <div class="form-group col-lg-6">
                    <input name="airline" id="airline" value="" class="form-control validates-as-required" aria-required="true" required aria-invalid="false" placeholder="Airline*" type="text">      
                  </div>

FILL.PHP //连接工作正常 连接部分工作正常。

$server = array('127.0.0.1','::1');
if(!in_array($_SERVER['REMOTE_ADDR'], $server)){
$conn = new mysqli("***","***","******","***_com_au");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$selected = mysqli_select_db($conn,"***_com_au") 
  or die("Could not select everestpnr");
}
else
{
  $conn = new mysqli("localhost","root","","everestpnr");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$selected = mysqli_select_db($conn,"everestpnr") 
  or die("Could not select everestpnr");
}    
 $results="SELECT * FROM `booking` WHERE `ticket`='".$_POST['ticket']."'";
        $result = $conn->query($results);
        $row = mysql_fetch_assoc($query); 

                // send the data encoded as JSON
                $json = array('booking' => $row['booking'], 
                            'fname' => $row['fName'],
                            'mname' => $row['mname'],
                            'lname' => $row['lname'],
                            'mobile' => $row['mobile'],
                            'email' => $row['email'],
                            'airplane' => $row['airplane']);                      
                echo json_encode($json );

        exit;
        }

如果表单的故障单编号字段中的密钥更改,则不会动态更新字段。我需要表单动态从数据库中提取数据,并在输入表单的票号字段中的票号时填写所有输入。

1 个答案:

答案 0 :(得分:0)

如果你想通过java脚本改变那个问题,你应该这样做 使用此代码

    <script type="text/javascript">
    $("#tktNum").change(function() {
        var ticket = $('#tktNum').val();
        //alert(ticket);
        $.ajax({
            type: "POST",
            dataType: 'json',
            data: "ticket=" + ticket,
            url: "fill.php",
            success: function(response) {
                json = JSON.parse(response);
                $('#bookNum').val(json.booking);
                $("#email").val(json.email);
                $("#mobile").val(json.mobile);
                $("#fName").val(json.fname);
                $("#lName").val(json.lname);
                $("#mName").val(json.mname);
                $("#airline").val(json.airline);
            },
            error: function() {
                alert('Some error occurred!');
            }
        });
    });
</script>

如果您想解决问题,可以使用此功能, 请提及标题类型标题('Content-type:application / json');用json格式表示响应

<?php
header('Content-type: application/json');

//Connection is working fine
//The connection part is working fine.

    $server = array('127.0.0.1','::1');
    if(!in_array($_SERVER['REMOTE_ADDR'], $server)){
    $conn = new mysqli("***","***","******","***_com_au");
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $selected = mysqli_select_db($conn,"***_com_au") 
      or die("Could not select everestpnr");
    }
    else
    {
      $conn = new mysqli("localhost","root","","everestpnr");
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $selected = mysqli_select_db($conn,"everestpnr") 
      or die("Could not select everestpnr");
    }    
     $results="SELECT * FROM `booking` WHERE `ticket`='".$_POST['ticket']."'";
            $result = $conn->query($results);
            $row = mysql_fetch_assoc($result); 

                    // send the data encoded as JSON
                    $json = array('booking' => $row['booking'], 
                                'fname' => $row['fName'],
                                'mname' => $row['mname'],
                                'lname' => $row['lname'],
                                'mobile' => $row['mobile'],
                                'email' => $row['email'],
                                'airplane' => $row['airplane']);                      
                    echo json_encode($json );

            exit;