Page 1 of 1

Using Vairables

Posted: Tue Sep 27, 2011 3:40 pm
by ehauser
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?

Re: Using Vairables

Posted: Tue Sep 27, 2011 3:45 pm
by Celauran
Have the dropdown's onchange event trigger an AJAX call to get the relevant info from the database.

Re: Using Vairables

Posted: Tue Sep 27, 2011 8:01 pm
by califdon
I have 2 different queries running on the same page.
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.

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

Posted: Wed Sep 28, 2011 9:23 am
by ehauser
Here is the code I am working with:

Code: Select all

SELECT * FROM orderstest WHERE republicordernumber=$republicordernumber";
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?

Re: Using Vairables

Posted: Wed Sep 28, 2011 9:48 am
by Celauran
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.
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?

Re: Using Vairables

Posted: Wed Sep 28, 2011 10:07 am
by ehauser
Here is the code where I am calling the 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 
?>
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'] ?

Re: Using Vairables

Posted: Wed Sep 28, 2011 10:12 am
by Celauran
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'].