Jump to content

Problem in getting the value in select option using ajax, php and mysql


Recommended Posts



It's my first time to use ajax and I used it to get the bin_loc per lot_number where I choose in select option per ItemCode.


And now I get the bin_loc but the problem is I only get the bin_loc of the first select option the rest was not.



Here is my code of PickingForm.php


   date_default_timezone_set("Asia/Singapore"); //set the time zone  
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';

mysql_select_db("mes", $con);
$Date_Shelve =date('Y-m-d H:i:s');
<link href="kanban.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
var xmlhttp;  // Give the function a unique name, this is what your HTML will call to run the AJAX 
function select_code() {      // This is all just setting up the variable, ignore it     
if (window.XMLHttpRequest) {          // code for IE7+, Firefox, Chrome, Opera, Safari        
xmlhttp=new XMLHttpRequest();      
} else {          // code for IE6, IE5         
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");      
xmlhttp.onreadystatechange = function() {          
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {              
       // alert(xmlhttp.readyState+" "+xmlhttp.status);
       // alert(xmlhttp.readyState+" "+xmlhttp.status);        
    document.getElementById("bin_loc").innerHTML = xmlhttp.responseText; 
        //var mydata=xmlhttp.responseText;         
var val = document.getElementById("lot_number");      
var lot_number = val.options[val.selectedIndex].text;  

var parameters = "lot_number="                      
                + lot_number;       
                xmlhttp.open("POST", "PickingFormTest.php", true);     
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");     
//var mydata=xmlhttp.responseText;   



  <div id="SR_date">
<label>Date :</label>
<input type="text" name="date_pick" value="<?php echo $Date_Shelve; ?>" size="16" readonly="readonly" style="border: none;">    

<div id="Picking">
  $sr_num_ = $_POST['sr_num_'];

//$sql = "SELECT sr_number "   
$sql = "SELECT sr_number, Items, DemandedQty, uom 
FROM sr_main WHERE sr_number = '$sr_num_'";
$res_sr_main = mysql_query($sql, $con);

$row_num = mysql_num_rows($res_sr_main);

if($row_num > 0){

while($row_sr_main = mysql_fetch_assoc($res_sr_main)){

$sr_number_ = $row_sr_main['sr_number'];
$items = $row_sr_main['Items'];
$demandedqty = $row_sr_main['DemandedQty'];
$uom = $row_sr_main['uom'];  
echo "<label> SR # :</label>";
echo "<input type='text' readonly='readonly' style='border:none;' id='sr_number_' name='sr_number_' value='$sr_number_'>";

echo "<table>";
echo "<th>Items</th>
        <th> Item Code </th>
        <th> Demanded Qty </th>
        <th> UoM </th>
        <th> Description </th>
        <th> Lot Number </th>
        <th> Bin Location </th>
        <th> Picked By </th>
        <th> Approved</th>";
echo "<tr>
        <td style='text-align:right;'>$demandedqty</td>
$sql_sub = "SELECT s.sr_number, s.Items, s.ItemCode, s.SubQty, s.UoM, s.Description 
FROM sr_submain s WHERE s.sr_number = '$sr_num_' and s.Items = '$items' AND NOT EXISTS(SELECT w.stock_item FROM wms w WHERE s.ItemCode = w.stock_item AND w.sr_number = s.sr_number AND (w.qty_withdraw !='0.00' OR w.qty_withdraw = 'NULL'))";

$res_sub = mysql_query($sql_sub, $con);

while($row_sub = mysql_fetch_assoc($res_sub)){

   echo "<form name='picking_lotnum' action='PickingForm.php?sr_num_=".$sr_num_."' method='POST' enctype='multipart/form-data' target='_self'>";

  $sr_num = $row_sub['sr_number'];
  $Items = $row_sub['Items'];
  $ItemCode = $row_sub['ItemCode'];
  $SubQty = $row_sub['SubQty'];
  $UoM = $row_sub['UoM'];
  $Description = $row_sub['Description'];

$query = "SELECT lot_number from wms WHERE (date_shelve IS NOT NULL) AND stock_item = '$ItemCode' AND (qty = orig_qty OR qty != '0.00') AND qty >= '$SubQty' ORDER BY qty";                 
$rows = mysql_query($query, $con); 

echo "<tr>
        <td><input type='text' name='ItemCode' id='ItemCode' value='$ItemCode' readonly='readonly' style='border:none;font-family: Arial, Helvetica, sans-serif;
    font-size: .9em;'></td>
        <td><input type='text' name='SubQty' id='SubQty' value='$SubQty' readonly = 'readonly' style='border:none; text-align:right;font-family: Arial, Helvetica, sans-serif;
    font-size: .9em;'></td>
echo "<td><select name = 'lot_number' id='lot_number' onChange='select_code();'>";
echo "<option></option>";                
  while ($record = mysql_fetch_array($rows))          
      echo "<option value = '{$record['lot_number']}'";             
      if ($lot_number == $record['lot_number'])                 
      echo "selected = 'selected'";             
       echo ">{$record['lot_number']}</option>";        
  echo "</select>";
  echo "</td>";  

  echo "<td id='bin_loc'></td>"; 
  echo "<td> <input type='text' name='picked_by' id='picked_by' size='20' value=''> </td>";  
     echo "<td><input type='submit' name='priority' value='Approved' id='priority'></td>";

     echo "</tr>";
     echo "<input type='hidden' name='date_pick' value='$Date_Shelve' size='16' style='border: none;'>";           
     echo "<input type='hidden' readonly='readonly' style='border:none;' id='sr_num_' name='sr_num_' value='$sr_number_'>"; 
     echo "</form>"; 
  echo "</table>"; 
  echo "</div>";     


and here is the PickingFormTest.php


   date_default_timezone_set("Asia/Singapore"); //set the time zone  
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';

mysql_select_db("mes", $con);

$lot_number = $_POST['lot_number'];

//echo $lot_number;
  // $q=$_GET["lot_number"];    
  $sql_bin = "SELECT bin_loc FROM wms WHERE lot_number = '$lot_number'";
  $res_bin = mysql_query($sql_bin, $con);
  while($row_bin = mysql_fetch_assoc($res_bin)){
      $bin_loc = $row_bin['bin_loc'];
  echo "<input type='text' value='$bin_loc' id='bin_loc'>";



I attach the sample form


Thank you so much


Link to comment
Share on other sites



When you make ajax call, response from server has been sent as soon as you echo or print statement executes.


So following code in PickingFormTest.php, is sending output to your browser and return.


while($row_bin = mysql_fetch_assoc($res_bin)){

      $bin_loc = $row_bin['bin_loc'];


  echo "<input type='text' value='$bin_loc' id='bin_loc'>";


So you can do something like concate your output like $output = "<input value='1'>#<input value='2'>";

echo $output;


and in your javascript code just split this response with '#' and iterate the array to display your input box.


Thanks :shy:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.