I have 2 different queries running on the same page. Query number 1 pulls all of the data (except the order number) from a mysql table into their appropriate fields on the page. Query 2 is an array that pulls the order numbers from the same table and puts them into a drop down box on the same page.
I would like to set it up so when someone selects an order number from the drop down menu it automatically populates the rest of the fields on the page with the corresponding data from that order number.
Is this possible?
Using Vairables
Moderator: General Moderators
Re: Using Vairables
Have the dropdown's onchange event trigger an AJAX call to get the relevant info from the database.
Re: Using Vairables
That's probably not what you mean. Think of it this way: from the user's perspective, they see a page with a dropdown box from which they can select an order number, then you want the order details to appear in a form. This does not mean that the 2 queries are "running on the same page," in fact that would generally not be true.I have 2 different queries running on the same page.
To achieve the result I described above, there are multiple ways you could program it. Perhaps the smoothest way would be to use AJAX, as was suggested by Celauran. But again, that would involve calling a different PHP script to run the query that populates the details (well, technically, it would be possible to do it in the same script, but I don't see much point in doing it that way). You could also just call the same script again, this time with a POST or GET parameter so that the script could send a new page to the browser with the appropriate detail data in the form.
But in any case, you first must clearly understand the relationship between client-side actions (a user selecting an order number) and server-side actions (accessing a database and sending data or HTML to the browser). That's the key. PHP can only perform server-side functions.
Re: Using Vairables
Here is the code I am working with:
The problem I am having is that the variable $republicordernumber is coming through blank when I echo it on screen. The variable contains the data from a drop down menu. Is there another way to reference a variable name in PHP?
Code: Select all
SELECT * FROM orderstest WHERE republicordernumber=$republicordernumber";Re: Using Vairables
Does this mean you're posting a form and mapping the dropdown's name from the $_POST array to $republicordernumber? If so, it could just be a typo either in the form or where you're setting $republicordernumber. If not, how is the variable being defined?ehauser wrote:The problem I am having is that the variable $republicordernumber is coming through blank when I echo it on screen. The variable contains the data from a drop down menu.
Re: Using Vairables
Here is the code where I am calling the republicordernumber
Besides that, I have not defined the variable anywhere else because I thought I had defined it in the code above. Do I need to still do a $republicordernumber = $_POST['republicordernumber'] ?
Code: Select all
<?
$query2="SELECT republicordernumber FROM orderstest ORDER BY republicordernumber asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
$result2 = mysql_query ($query2);
echo "<select name=republicordernumber class=c44 title=Order Number id=republicordernumber onchange=SQL()>Republic Order Number</option>";
// printing the list box select command
while($nt=mysql_fetch_array($result2)){//Array of records stored in $nt
echo "<option value=$nt[republicordernumber]>$nt[republicordernumber]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>Re: Using Vairables
Is your drop-down list part of a form? I ask because it doesn't appear to be in that code snippet you posted. If it is and if you're posting the form to auto-populate the other fields (or another form) as discussed in the OP, then you'll definitely need to assign $foo = $_POST['foo'].