Page 1 of 1

get option value

Posted: Thu Mar 26, 2009 8:41 pm
by matthew0786
this is what im trying to do, i have successfully retrieved values from my DB.

now i have a link next to the option box which will pass the selected value into the next page. How do i get the selected value ??

Code: Select all

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="Users"; // Database name
$tbl_name="members"; // Table name
 
// Connect to server and select databse.--------------------------------------------------------------------------------------------------------------
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
 
<?php 
//Generate OPTION box--------------------------------------------------------------------------------------------------------------------------------------
$name= mysql_query("select * from $tbl_name"); ?>
<select name="user" id="user">
<?php while($res= mysql_fetch_assoc($name)) { ?>
<option>
<?php echo $res['username']?>
</option>
 <?php } ?>
</select>
 
<a href="<?php echo "test.php";?>">delete</a>
Here is my link code to link to the page, how do i get the selected option value?

Code: Select all

<a href="<?php echo "test.php";?>">delete</a>

Re: get option value

Posted: Thu Mar 26, 2009 9:17 pm
by tech603
You can pass the value into the link.

echo out the link
<? echo "<a href="test.php?value=" . $yourvalue . ">Click Here</a>"; ?>

This will make the link test.php?value=yourvalue so the when you click it your url will be that and you can get the value on the next page by doing this.

<?php

if(isset($_GET['value'])){
$myvalue = $_GET['value'];
}
?>

hope that helps.

Re: get option value

Posted: Fri Mar 27, 2009 12:20 am
by matthew0786
sorry, what i meant to imply was that i wanted to insert the value of the OPTION index into the link, hope that clears it up

Re: get option value

Posted: Fri Mar 27, 2009 7:54 am
by N1gel
I don't think you want to put the delete option in the link there is always the chance a user could bookmark the page or it could get stored by a search engine bot.

I find its always best to post delete options. I would do it the following way

Code: Select all

 
echo "<form id='myForm' name='myForm' method='post' action='test.php'>";
echo  "<select name='user' id='user'>";
while($res= mysql_fetch_assoc($name)) 
{
    echo "<option value='$res['userID']'>";
    echo $res['username'];
    echo "</option>";
 }
echo "</select>";
echo "</form>";
 
echo "<a href='javascript&#058;document.getElementById(\"myForm\").submit();'>delete</a>";
 
(The &#058; is actually a : character)

Then use $_POST["user"] in the test.php

Hope this helps :D

Re: get option value

Posted: Sat Mar 28, 2009 12:52 am
by matthew0786
thanks! that helped a lot