Forum Moderators: coopster

Message Too Old, No Replies

how to pass the value of selected dropdown menu to another file?

         

shams

12:27 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



hi,
there is a php-myql script that list the mysql databases in the drop-down menu, use wil select one and the submit button should pass the value or variable $db_name to the test.php, this script list the databases in the drop-down menu but when i select one and then click on submit cannot pass the db_name to the test.php:

<html>
<head><title>List DB</title></head>
<body>
<form method="POST" action="test.php"><div> align="center"><center><p>Select Db
<input type="hidden" name="db_name" value="DB_names">
<SELECT NAME="Select DB">
<?php

$conn = @mysql_connect( 'myserver', 'myuser', 'mypass' )
or die( mysql_errno().': '.mysql_error().
$result = mysql_list_dbs( $conn );

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );

?>
</select>
<input type="submit" value="Submit"></center></div>
</form>
</body>
</html>

Birdman

12:43 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would advise you to stay away from spaces in form fields. Try the change below and see if it will work now.

In your 'test.php' script, you should be able to access the variable like so:

print $_POST['Select_DB'];


<SELECT NAME="Select_DB">
<?php

$conn = @mysql_connect( 'myserver', 'myuser', 'mypass' )
or die( mysql_errno().': '.mysql_error(); // <-semicolon here
$result = mysql_list_dbs( $conn );

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );

?>

shams

1:55 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



thanks for reply, i test your posted code but this even didn't list the databases, the ouput is just a blank page, my posted code show the databases in the drop-down menu, but cannot pass the the select database but the value of value="Db_list", test.php is:
echo 'hello '.$db_name;
and the output of test.php is:
hello Db_list
not the selected database name.

Psychopsia

2:29 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



Hi Shams!

The error is: Parse error: parse error, unexpected ';' in PATH/FILE on line 9

Try adding ) next to mysql_error(), like mysql_error())

After add that to the script works fine for me.

Birdman

3:04 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes Psychopsia is right, there is a parentheses missing. Sorry about that!

<SELECT NAME="Select_DB">
<?php

$conn = @mysql_connect( 'myserver', 'myuser', 'mypass' )
or die( mysql_errno().': '.mysql_error());
$result = mysql_list_dbs( $conn );

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );

?>

[edited by: Birdman at 3:05 pm (utc) on Oct. 19, 2006]

shams

3:38 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



thanks for rpelies but still i get the same ouput with test.php:
hello Db_list
not the selected database name the test.php should print the selected database name instead of Db_list;

Psychopsia

3:48 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



So, in [test.php] is the code posted here or is on another file?

Try using $_POST['Select_DB'] to get the value of the selected item.

Birdman

3:51 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you show us the relevant part of test.php, where you call and display the selected DB?

shams

12:52 am on Oct 20, 2006 (gmt 0)

10+ Year Member



thanks to all, the problem solved with $_POST['list_DB'], the mistake was with $_POST['$db_name'].