Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Got an error in xmlhttp.responseText

    Hi..

    It's my first time to use ajax, and now I used ajax to get the value from select option then use that value.

    here is my ajax code:

    Code:
    <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.responseText);
           // alert(xmlhttp.readyState+" "+xmlhttp.status);
           
        document.getElementById("rows").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", "PickingForm.php", true);     
                    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");     
                    xmlhttp.send(parameters);
    //var mydata=xmlhttp.responseText;   
    }
    
    </script>
    nad here is my PickingForm.php where the select option was in.

    Code:
    $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 "<td><select name = 'lot_number' id='lot_number' onChange='select_code();'>";
     
     //echo "<select name='lot_number' id='lot_number' onchange='MM_jumpMenu('parent',this,0)'>";
     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>";  
      
      $lot_number = $_POST['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 "<td><input type='text' name='bin_loc' id='bin_loc' value='$bin_loc'></td>";
      }
    and when I select lot_code in select option:

    I got an error:

    document.getElementById() is null or not an object...

    I don't know how can I solve this error

    Thank you

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by iBall View Post
    Has the element with id equal to rows been loaded into the DOM before you try to access it with getElementById?

    Post the html for the element with id equal to rows.
    here is my whole code:

    Code:
    echo "<td id='rows'><input type='text' value='$bin_loc'></td>";
    Code:
    <?php 
        session_start();                                                                         
       error_reporting(0);
       date_default_timezone_set("Asia/Singapore"); //set the time zone  
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    mysql_select_db("mes", $con);
    $Date_Shelve =date('Y-m-d H:i:s');
    ?>
    <html>
    <title>Picking</title>
    <head>
    <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.responseText);
           // alert(xmlhttp.readyState+" "+xmlhttp.status);
           // alert(xmlhttp.readyState+" "+xmlhttp.status);        
        document.getElementById("rows").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", "PickingForm.php", true);     
                    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");     
                    xmlhttp.send(parameters);
    //var mydata=xmlhttp.responseText;   
    }
    
    </script>
    
    
    
    
    
    </head>
    <body> 
    
    <?php
    $Approved = isset($_POST['priority']);
    if ($Approved)
    {
        $lot_number = $_POST['lot_number'];
        $sr_num_ = $_POST['sr_num_'];
        $SubQty = $_POST['SubQty'];
        $ItemCode = $_POST['ItemCode'];
        $picked_by = $_POST['picked_by'];
        $date_pick = $_POST['date_pick'];
    
    $sql = "SELECT stock_item, qty FROM wms WHERE stock_item = '" . $ItemCode . "' AND lot_number = '" . $lot_number . "'"; 
    $res = mysql_query($sql, $con) or die( "Query : [$sql]<br/>\nError : ". mysql_error($con));
    
    $row = mysql_fetch_assoc($res);
    
    $stock_item = $row['stock_item'];
    $qty = $row['qty'];
    
    if($qty >= $SubQty){
     
    $output = $qty - $SubQty;
    
    $qty_withdraw = '0.00';
    }
    else{
    $output = '0.00';
    $qty_withdraw = $SubQty - $qty;
    }     
    
    
    $sql_up = "UPDATE wms SET qty = '$output', sr_number = '$sr_num_', picked_by = '$picked_by', qty_withdraw = '$qty_withdraw', date_pick = '$date_pick'
    WHERE lot_number = '$lot_number' AND stock_item = '$ItemCode'";
    
    $res_up = mysql_query($sql_up, $con) or die( "Query : [$sql_up]<br/>\nError : ". mysql_error($con));
          
    }
    else{
        
    }
    
    
    ?>
    
      <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>
    
     <div id="Picking">
    <!--<label class="LLabellot">Select SR # :</label>-->
    <?php          
      $sr_num_ = $_POST['sr_num_'];  
      
    $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>$items</td>
            <td></td>
            <td style='text-align:right;'>$demandedqty</td>
            <td>$uom</td>
            </tr>";             
            
    $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></td>
            <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>
            <td>$UoM</td>
            <td>$Description</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>";  
      
      $lot_number = $_POST['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 "<td id='rows'><input type='text' value='$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>";     
      ?>
    </body>
    </html>

    Thank you so much...

    I tried my best to solve it ,still it wont work..

    I only want is to get the value of select option so that I could use it to query the bin loc

    Thank you

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I resolve that error:

    but when I choose on another select option only the first select option value was displayed


    It only gets the first select option value.

    I will attach my sample form:

    and here is my code:

    Code:
    <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.responseText);
           // 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;  
    
    //alert(lot_number);     
    var parameters = "lot_number="                      
                    + lot_number;       
                    xmlhttp.open("POST", "PickingFormTest.php", true);     
                    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");     
                    xmlhttp.send(parameters);
    //var mydata=xmlhttp.responseText;   
    }
    
    </script>
    
    $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); 
    //$res = mysql_fetch_assoc($rows);
    //$bin_loc = $res['bin_loc']; 
     echo "<tr>
            <td></td>
            <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>
            <td>$UoM</td>
            <td>$Description</td>";
                    
    echo "<td><select name = 'lot_number' id='lot_number' onChange='select_code();'>";
     //echo "<td><select name = 'lot_number' id='lot_number' onchange='getText();'>";  
     
     //echo "<select name='lot_number' id='lot_number' onchange='MM_jumpMenu('parent',this,0)'>";
     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>";

    PickingFormTest.php
    Code:
    <?php
          session_start();                                                                         
       error_reporting(0);
       date_default_timezone_set("Asia/Singapore"); //set the time zone  
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    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'>";
    
      }
      
    ?>
    Thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •