Jump to content

Archived

This topic is now archived and is closed to further replies.

niza

passing values from radio buttons to a hyperlink

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.