Ajax php mysql - fetch multiple values

647

I want to fetch data from table 'driver' by using sql where (name = '? ') condition and fill the result into three text boxes .

<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>

I am trying to fetch data from other php page

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

so please help I am just new to Ajax and php

46

Answer

Solution:

Encode the output of the PHP file you're fetching with AJAX as JSON using json_encode and then parsehr.responseText as JSON and access the data accordingly.
Have a look here: http://www.json.org/js.html

People are also looking for solutions to the problem: php - Filter 2D array using another 2D array where differently keyed column values intersect

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.