0

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

1
  • 1
    Have you tried anything so far? if yes then show us code or else first try Commented Sep 6, 2013 at 10:30

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.