Jump to content

passing values from radio buttons to a hyperlink


niza

Recommended Posts

Here's my code:

[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>";        [/code]

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

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

But no matter what radio button I choose, when I click the Edit link, the ID doesn't change. Any sugestions? Thanks.
Link to comment
Share on other sites

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

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

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:

[code]<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>[/code]
In this example, you would add the ID of "ulink" to the link in question so Javascript can interact with it.
Link to comment
Share on other sites

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...
[code]<?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);
?>[/code]
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.