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
matthew0786
Forum Newbie
Posts: 9 Joined: Sat Feb 28, 2009 8:01 am
Post
by matthew0786 » Thu Mar 26, 2009 8:41 pm
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
Post
by tech603 » Thu Mar 26, 2009 9:17 pm
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
Post
by matthew0786 » Fri Mar 27, 2009 12:20 am
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
N1gel
Forum Commoner
Posts: 95 Joined: Sun Apr 30, 2006 12:01 pm
Post
by N1gel » Fri Mar 27, 2009 7:54 am
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:document.getElementById(\"myForm\").submit();'>delete</a>";
(The : is actually a : character)
Then use $_POST["user"] in the test.php
Hope this helps
matthew0786
Forum Newbie
Posts: 9 Joined: Sat Feb 28, 2009 8:01 am
Post
by matthew0786 » Sat Mar 28, 2009 12:52 am
thanks! that helped a lot