0

我想通过使用 sql where (name = '?') 条件从表“驱动程序”中获取数据并将结果填充到三个文本框中。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <script language="JavaScript" type="text/javascript">
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "my_parse_file.php";
    var fn = document.getElementById("driver_name").value;
  //  var ln = document.getElementById("last_name").value;
    var vars = "driver="+fn;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
            document.getElementById("contact_no").value = return_data;
            $("#wait").css("display","none");

        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
    $("#wait").css("display","block");

}
</script>

</head>

<body>


<form action="my_parse_file.php"  method="post" id="form2">

  <fieldset >   <legend>Dispatch</legend>
    <table width="50%" align="center">
      <tr valign="baseline">
        <td align="left">Dispatch_date:</td>
        <td><input type="date" name="dispatch_date" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left">Driver_name:</td>
        <td><select name="driver_name" id="driver_name" onchange="javascript:ajax_post();">
          <?php
do {  
?>
          <option value="<?php echo $row_drivers['Driver_Name']?>"><?php echo $row_drivers['Driver_Name']?></option>
          <?php
} while ($row_drivers = mysql_fetch_assoc($drivers));
  $rows = mysql_num_rows($drivers);
  if($rows > 0) {
      mysql_data_seek($drivers, 0);
      $row_drivers = mysql_fetch_assoc($drivers);
  }
?>
        </select></td>
      </tr>
      <tr valign="baseline">
        <td align="left">Driver_id:</td>
        <td><input type="text" name="driver_id" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left">Contact No.</td>
        <td><input type="text" name="contact_no" id="contact_no" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left">Truck_id:</td>
        <td><input type="text" name="truck_id" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left">Nums. of skids</td>
        <td><input type="number" min="0" max="30"  id="skids"  required="required" />
          &nbsp;</td>
      </tr>
      <tr valign="baseline">
        <td align="left">&nbsp;</td>
        <td><input type="submit" value="Insert record" />
          <?php ?></td>
      </tr>
    </table>
  </fieldset>
  <input type="hidden" name="MM_insert" value="form2" />
</form>

我正在尝试从其他 php 页面获取数据

<?php echo ' Driver Name' ?>
<?php echo ' Driver ID' ?>
<?php echo ' Contact no' ?>

所以请帮助我只是 Ajax 和 php 的新手

4

1 回答 1

1

使用json_encode将您使用 AJAX 获取的 PHP 文件的输出编码为 JSON ,然后解析hr.responseText为 JSON 并相应地访问数据。
看看这里: http: //www.json.org/js.html

于 2013-09-06T13:55:25.540 回答