Jump to content


Photo

passing values from radio buttons to a hyperlink


  • Please log in to reply
3 replies to this topic

#1 niza

niza
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 08 April 2006 - 09:00 PM

Here's my code:

echo "<table cellspacing='10' border='0' cellpadding='0' width='100%'>";
echo "<tr><td width='20'></td>
<td>Ad Name</td></tr>";

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))  
{
extract($row);
echo "<tr><td>[b]<input name='vAd' type='radio' value='$adID'>[/b]</td>
<td>{$row['adName']}</td></tr>";
} 
echo "<tr><td colspan='2' width='10'>
<table cellspacing='10' border='0' cellpadding='0' width='100%'>
<tr><td>[b]<a href='edit_add.php?id=$adID'>[/b]Edit</a></td></tr></table>
</td></tr></table>";        

I would like to pass the value $adID from the radio buttons
<input name='vAd' type='radio' value='$adID'>

to this link here:
<a href='edit_add.php?id=$adID'>Edit</a>

But no matter what radio button I choose, when I click the Edit link, the ID doesn't change. Any sugestions? Thanks.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 April 2006 - 04:58 PM

I do not understand your question. Do you want the value of your checkbox to go to your link when selected? If so this is not possible with PHP.

#3 Vorotaev

Vorotaev
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationCanada

Posted 09 April 2006 - 05:08 PM

[!--quoteo(post=362860:date=Apr 8 2006, 05:30 PM:name=niza)--][div class=\'quotetop\']QUOTE(niza @ Apr 8 2006, 05:30 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]I would like to pass the value $adID from the radio buttons
<input name='vAd' type='radio' value='$adID'>

to this link here:
<a href='edit_add.php?id=$adID'>Edit</a>

But no matter what radio button I choose, when I click the Edit link, the ID doesn't change. Any sugestions? Thanks.
[/quote]
Unfortunately, what wildteen88 said is correct. PHP parses the page before ever sending it, and has no way to change the ID thereafter. You should use Javascript instead:

<script type="text/javascript">

function uplink() {
    document.getElementById('ulink').href = 'edit_add.php?' + document.getElementByName('vAd').value;
}

</script>

<input name='vAd' type='radio' value='$adID'>

...

<a id="ulink" href='edit_add.php?id=$adID'>Edit</a>
In this example, you would add the ID of "ulink" to the link in question so Javascript can interact with it.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 05:11 PM

Values of checkboxes or any form input are only passed to your PHP script when the form is submitted.

In your code snippet I don't see a form start or end "<form></form>" or a submit button.

Here's how I would write this. I've left out all of the table stuff since that is only formatting and really should be done with CSS...
<?php
$tmp = array();
$tmp[] = '<form method="POST" action="edit_add.php">';
$tmp[] = 'Ad Name: ';
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
    '<input  name="vAd" type="radio" value='" . $row['adID'] . '"> ' . $row['adName'] . '<br>';
$tmp[] = '<input type="submit" name="submit" value="Edit">';
$tmp[] = '</form>';
echo implode("\n",$tmp);
?>
When you choose a button and click the "Edit" button, the value of $_POST['vAd'] in your script edit_add.php will contain value you selected.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users