Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:25:04+00:00 2026-06-01T04:25:04+00:00

I have a javascript that is executed with the onchange event. onchange=showUser(1, this.value) The

  • 0

I have a javascript that is executed with the onchange event.

onchange="showUser(1, this.value)"

The Javascript calls a php page which executes a mysql query and displays the results on the current page.

The javascript works perfectly however I want to duplicate the javascript to run a different query and display the results on the existing page.

I have a table for orders, when I insert an order in the first column, I want the onchange even to trigger two scripts, one to pull and display product details in the second column and the other to pull and show the selling units in the third column.

my javascript is:

<script type="text/javascript"> 
  function showWhse(userNumber, str) 
  { 
    if (str=="") 
    { 
      document.getElementById("whse" + userNumber).innerHTML=""; 
      return; 
    }   
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
    } 

    xmlhttp.onreadystatechange=function() 
    { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
        document.getElementById("whse" + userNumber).innerHTML=xmlhttp.responseText; 
      } 
    } 
    xmlhttp.open("GET","getdata1.php?q="+str,true); 
    xmlhttp.send(); 
  }
</script>

the complete PHP Page is:

<html>
<head>
<title>Sales Portal</title>
<script type="text/javascript"> 
  function showUser(userNumber, str) 
  { 
    if (str=="") 
    { 
      document.getElementById("txtHint" + userNumber).innerHTML=""; 
      return; 
    }   
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
    } 

    xmlhttp.onreadystatechange=function() 
    { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
        document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText; 
      } 
    } 
    xmlhttp.open("GET","getdata1.php?q="+str,true); 
    xmlhttp.send(); 
  }
</script>

</head>
<body topmargin=0>

<form name="orderform" id="orderform" action="newsale.php" method="post">

<table border=1>    

<tr>
<td>Product Code</td>
<td>Description</td>
<td>Warehouse</td>
<td>Selling Units</td>
</tr>
<tr id="r1">  
    <td width=100>
        <input size=10  type=number id=sku1 name=sku1 onchange="showUser(1, this.value)">
    </td>
    <td width=280>
        <div align="left" id="txtHint1">&nbsp;</div>
    </td>
    <td width=100>
        <div align="left" id="whse1">&nbsp;</div>
    </td>
    <td width=100>
        <div align="left" id="su1">&nbsp;</div>
    </td>
</tr>
<tr id="r2">  
    <td>
        <input size=10  type=number id=sku2 name=sku2 onchange="showUser(2, this.value)">
    </td>
    <td>
        <div align="left" id="txtHint2">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="whse2">&nbsp;</div>
    </td>
    <td width=100>
        <div align="left" id="su2">&nbsp;</div>
    </td>
</tr>
<tr id="r3">  
    <td>
        <input size=10  type=number id=sku3 name=sku3 onchange="showUser(3, this.value)">
    </td>
    <td>
        <div align="left" id="txtHint3">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="whse3">&nbsp;</div>
    </td>
    <td width=100>
        <div align="left" id="su3">&nbsp;</div>
    </td>
</tr>
<tr id="r4">  
    <td>
        <input size=10  type=number id=sku4 name=sku4 onchange="showUser(4, this.value)">
    </td>
    <td>
        <div align="left" id="txtHint4">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="whse4">&nbsp;</div>
    </td>
    <td width=100>
        <div align="left" id="su4">&nbsp;</div>
    </td>
</tr>
</table>
</form>

</body>
</html>

and the php file that executes the mysql query is:

<?php
 $q=$_GET["q"];

$con = mysql_connect('localhost', 'unilekxy_UL', 'Unilever2011');
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("unilekxy_unilever", $con);


$sql="SELECT Category, Description,SellingUnits,Grouping,CasesPerPallet,ShrinksPerPallet FROM skudata WHERE packcode = '".$q."'";
$result = mysql_query($sql);
$rows=mysql_num_rows($result);

if($rows==0){echo "<font color=red><b>NOT A VALID PRODUCT CODE</b></font>";} else {
while($row = mysql_fetch_array($result))
   {
   echo "<font color=red>".$row['Description']."</font>, ";
   if($row['SellingUnits']=="CS"){echo "<font color=red>".$row['CasesPerPallet']."</font> ";} elseif($row['SellingUnits']=="SHR") {echo "<font color=red>".$row['ShrinksPerPallet']."</font> ";}
   }}

mysql_close($con);
 ?> 

ideally I can use this script to fetch and display the product details, selling units and warehouse information once the user inputs the product code, as per below image:
enter image description here

How can I edit or duplicate the javascript in order for 1 onchange event to action this? I find that if I duplicate the entire sript, only the last instance is executed, I assume because the variables being used are the same with the unchanged script?

Thanks in advance for the help,
Ryan

ADDITION

I have made the changes to the javascript as below:

<script type="text/javascript"> 
  function showUser(userNumber, str) 
  { 
    if (str=="") 
    { 
      document.getElementById("txtHint" + userNumber).innerHTML=""; 
      return; 
    }   
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
    } 

    xmlhttp.onreadystatechange=function() 
    { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
        document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText; 
      } 
    } 
    xmlhttp.open("GET","getdata1.php?q="+str,true); 
    xmlhttp.send(); 

var responseText = xmlhttp.responseText; 
var description = responseText.substring(13, responseText.indexOf(",Warehouse:")); 
var warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:")); 
var sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14); 

document.getElementById("whse" + userNumber).innerHTML = warehouse; 
document.getElementById("txtHint" + userNumber).innerHTML = description; 
document.getElementById("su" + userNumber).innerHTML = sellingUnits;
  }
</script>

How do I call these values to display in seperate columns? warehouse, description and sellingunits?

I presume I need to call these values from within my table html, how do I do this?

Thanks again, Ryan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:25:06+00:00Added an answer on June 1, 2026 at 4:25 am

    First, edit your php file that executes the sql query to return all three values and remove any html formatting. The following does it by putting all values into a single string. If you know JSON for php, then you should use that instead.

    <?php
     $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'unilekxy_UL', 'Unilever2011');
     if (!$con)
       {
       die('Could not connect: ' . mysql_error());
       }
    
    mysql_select_db("unilekxy_unilever", $con);
    
    
    $sql="SELECT Category, Description,SellingUnits,Grouping,CasesPerPallet,ShrinksPerPallet FROM skudata WHERE packcode = '".$q."'";
    $result = mysql_query($sql);
    $rows=mysql_num_rows($result);
    
    if($rows==0){echo "<font color=red><b>NOT A VALID PRODUCT CODE</b></font>";} else {
    while($row = mysql_fetch_array($result))
     {
       echo "Description:" . $row['Description'] . ",Warehouse:" . $row['Warehouse'] . ",SellingUnits:" . $row['SellingUnits'];
     }
    }
    
    mysql_close($con);
     ?> 
    

    Next, edit the showUser function to parse the xmlhttp.responseText to separate each value and set them to the desired div locations.

    <script type="text/javascript"> 
      function showUser(userNumber, str) 
      { 
        if (str=="") 
        { 
          document.getElementById("txtHint" + userNumber).innerHTML=""; 
          return; 
        }   
        if (window.XMLHttpRequest) 
        {// code for IE7+, Firefox, Chrome, Opera, Safari 
          xmlhttp=new XMLHttpRequest(); 
        } 
    
        xmlhttp.onreadystatechange=function() 
        { 
          if (xmlhttp.readyState==4 && xmlhttp.status==200) 
          { 
            //document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText;
            var responseText = xmlhttp.responseText;
            var description = responseText;
            var warehouse = "";
            var sellingUnits = "";
            if (responseText.indexOf("NOT A VALID") == -1)
            {
              description = responseText.substring(13, responseText.indexOf(",Warehouse:")); 
              warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:")); 
              sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14); 
            }
    
            document.getElementById("whse" + userNumber).innerHTML = warehouse; 
            document.getElementById("txtHint" + userNumber).innerHTML = description; 
            document.getElementById("su" + userNumber).innerHTML = sellingUnits;
    
          } 
        } 
        xmlhttp.open("GET","getdata1.php?q="+str,true); 
        xmlhttp.send();
      }
    </script>
    

    At this point, you should not need to make any other changes as the single call to showUser(1, this.value) will update all three fields.

    Keep in my mind that it is always good to separate your user interface formatting code, <font>, from your SQL select statements as I have done above. In addition, it is always good to execute as few SQL queries as possible.

    For more information about the indexOf and substring methods, see JavaScript String Object.

    Please note that I did not test any of this code, so there may be spelling mistakes or incorrect syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript that runs using the onchange event when a user enteres
In this page: http://www.depositosalto.com.br/lojas.php , I have a javascript to resize a div and
I have a javascript that calls a function each 1 minute. That function sends
I have a javascript that does this (http is your XMLHttpRequest object) var r
I have a piece of javascript that grabs JSON data. When executed locally everything
I have this javascript modal which I want to use for error reporting. Everything
I have following Javascript objects: function Alfa() { this.status=''; this.setStatus = function(value) { this.status=value
I have 2 textboxes that I'm trying to capture the onchange event for. These
I have an input field on my PHP page. When a value is entered
I have a gridview inside an updatepanel and I have a javascript that calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.