Forum Moderators: coopster

Message Too Old, No Replies

Updating select box with value from 2 different tables

Html Form select box display numbers

         

Atomic_Guy

11:27 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



I have a form with just one select box. Currently I can pull numbers from the table and get it in to the select box but the trouble I am having at the moment is to get 2 different set of numbers into one select box from 2 different table. Table1 and table2. So when I select Table1 button or Table2 button the select box should update it's self with appropriate table value. For example, If I press Table1 button the select box should pull only data in table1.

How do I achieve this? I have the code for the both tables. How do I connect this to the button?

Table1:
<select name ="ID_Number">

<? php

$connection = mysql_connect($_host, $db_user, $db_password) or die("Unable to connect");
mysql_select_db($db_name, $connection);

$query = "SELECT * FROM num_Table1";
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result))
{
echo "<OPTION VALUE = '".$record["num"]."'>".$record["num"];
}
?>
</select>

Table2:

<? php

$connection = mysql_connect($_host, $db_user, $db_password) or die("Unable to connect");
mysql_select_db($db_name, $connection);

$query = "SELECT * FROM num_Table2";
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result))
{
echo "<OPTION VALUE = '".$record["num"]."'>".$record["num"];
}
?>

Thanks to everyone for their inputs.

barns101

12:05 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



I'm not sure that I quite understand what you're doing. You start off with a page where the user selects a button either "Table 1" or "Table 2" and then the next page displays a select box based on that choice?

If that's the case you can use an if() [php.net] statement to check which button was pressed and use your database access code appropriately.


<?php
if($_POST[table_1])
{
// Use first code sample
}
elseif($_POST[table_2]
{
// use second code sample
}
else
{
// Display the two buttons for the first page
}
?>

This assumes that your buttons are named "table_1" and "table_2" and you use a form that posts to itself.

Atomic_Guy

8:05 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



HI barns101....

Sorry for any confusion. What I was trying to do is to get the select box to have numbers. The select box is already defined in the form and just needed to populate with numbers from the table. I have other fields in the form, after I entered data in these fields I then want to select a number from the select box depending on the table button I pressed.

Does that make sense?

The suggestion you gave me will work but that means the whole form has to be refreshed, instead of refreshing the whole form just want to populate the select box with either numbers from table1 or table2.

Any suggestions?

mcibor

8:17 pm on Nov 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use javascript table 1: -onclick="populate_select(this)". There's no other option.

If you don't want to pull both data, then use AJAX.

Hope this shows you right direction
Michal

Atomic_Guy

10:55 am on Nov 25, 2006 (gmt 0)

10+ Year Member



Yes I realise the only way to pull the data out of Table1 or Table2 live is to use Java script. The code you typed mcibor....how do I use this code? I dont have a clue about java script and if you can kindly expand a little on this code would be appreciated.

How do I link Java script to a button and
On click Table1 button how to populate the select tag?

<select name ="Table1"> </Select>

thanks

[edited by: Atomic_Guy at 11:01 am (utc) on Nov. 25, 2006]

mcibor

7:50 pm on Nov 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here you've got some resources:

[quirksmode.org...]

it's a step by step explanation of how to do that.

If it didn't help you, then write, and we'll try to find sth out.

Michal