get option value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

get option value

Post 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>
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: get option value

Post 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.
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

Re: get option value

Post 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
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: get option value

Post 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
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

Re: get option value

Post by matthew0786 »

thanks! that helped a lot
Post Reply