Jump to content


Photo

Radio button value on PHP Post


  • Please log in to reply
8 replies to this topic

#1 jonw118

jonw118

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts

Posted 27 July 2009 - 11:59 PM

Hi there- I'm writing a script right now and not sure how I should handle this problem.

Basically it's a form that uses post and when you click submit does simple calculations.

For form fields to get it to keep the values upon submit is no problem, I'm using this:
<input id="variable_a" type=text name="variable_a" wrap=virtual value="<?php echo $_POST["variable_a"]; ?>" size=3 maxlength=10>

If I didn't echo it, it would lose the value on submit.

BUT - I can't figure out on radio button how to do it (so it will show what was selected after submit). I need one radio button to have a value of "1" and the other to have a value of "2".

Can anyone advise me how to do this?

Thanks!



#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,811 posts
  • LocationCanada

Posted 28 July 2009 - 12:10 AM

Put the options into an array (or pull from a database):
$options = array(
    '0' => 'Option 1',
    '1' => 'Option 2',
    '2' => 'Option 3',
    '3' => 'Option 4',
);

foreach($options as $value => $label)
{
  $checked = ($option == $_POST['group_name'])? ' checked="checked"' : '';
  echo "<input type=\"radio\" name=\"group_name\" value=\"{$value}\"{$checked} /> $value<br /> ";
}

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 mmarif4u

mmarif4u

    Advanced Member

  • Members
  • PipPipPip
  • 1,244 posts
  • Location/dev/swat

Posted 28 July 2009 - 12:12 AM

The simple way is to use JavaScript.

<form id="yourform" method="POST" action="">
<input id="variable_a" type=text name="variable_a" wrap=virtual value="<?php echo $_POST["variable_a"]; ?>" size=3 maxlength=10 onclick="document.getElementById('yourform').submit();">
</form>

OR in PHP:

$v1 = 'unchecked';
$v2 = 'unchecked';

if (isset($_POST['Submit'])) {

$selected_radio = $_POST['variable_a'];

if ($selected_radio = = 'v1') {
$v1 = 'checked';
}
else if ($selected_radio = = 'v2') {
$v2 = 'checked';
}
}

V-1 <Input type = 'Radio' Name ='variable_a' value= '1'
<?PHP echo $v1; ?>

V-2 <Input type = 'Radio' Name ='variable_a' value= '2'
<?PHP echo $v2; ?>


#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,811 posts
  • LocationCanada

Posted 28 July 2009 - 12:36 AM

The OP asked

I can't figure out on radio button how to do it (so it will show what was selected after submit).


Unless this page is merely for learning purposes and there is no reason for the user to actually submit the page I don't see how JavaScript is an answer. Even if you wanted to have the JavaScript determine some value before submission it still couldn't be used for the only solution since the same logic would need to be copied on the server-side to take care of users without JavaScript enabled.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 jonw118

jonw118

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts

Posted 28 July 2009 - 12:39 AM

First- thanks so much for the help!


Mjdamato: I tried your solution, pasted your example in exactly, and when I click submit, the radio buttons didn't stay checked (None of them were selected).


Mmarif4u: Can't quite get this one to work either. First it gave an error:

Parse error: syntax error, unexpected '=' in

So I removed the "=" on the two lines where you had two of them.

That got rid of the error.

But - it didn't keep the settings on the submit. Here's the exact code I used:

<?php
$v1 = 'unchecked';
$v2 = 'unchecked';

if (isset($_POST['Submit'])) {

$selected_radio = $_POST['variable_g'];

if ($selected_radio = 'v1') {
$v1 = 'checked';
}
else if ($selected_radio = 'v2') {
$v2 = 'checked';
}
}
?>

And
<Input type = 'Radio' Name ='variable_g' value= '1' <?PHP echo $v1; ?>>
<Input type = 'Radio' Name ='variable_g' value= '2' <?PHP echo $v2; ?>>

It keeps the first one checked after every submit.

Thanks again for the help!

#6 jonw118

jonw118

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts

Posted 28 July 2009 - 12:43 AM

The OP asked

I can't figure out on radio button how to do it (so it will show what was selected after submit).


Unless this page is merely for learning purposes and there is no reason for the user to actually submit the page I don't see how JavaScript is an answer. Even if you wanted to have the JavaScript determine some value before submission it still couldn't be used for the only solution since the same logic would need to be copied on the server-side to take care of users without JavaScript enabled.



Yeah, this is for an online calculator - so someone can input variables, click submit and see the updated price. The problem is the user will get confused if they can't remembered what was selected and what wasn't.

#7 mmarif4u

mmarif4u

    Advanced Member

  • Members
  • PipPipPip
  • 1,244 posts
  • Location/dev/swat

Posted 28 July 2009 - 12:47 AM

<?php
$v1 = 'unchecked';
$v2 = 'unchecked';

if (isset($_POST['Submit'])) {

$selected_radio = $_POST['variable_g'];

if ($selected_radio == '1') {
$v1 = 'checked';
}
else if ($selected_radio == '2') {
$v2 = 'checked';
}
}
?>


#8 jonw118

jonw118

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts

Posted 28 July 2009 - 12:54 AM

Awesome! That did the trick. Thanks so much!

#9 mmarif4u

mmarif4u

    Advanced Member

  • Members
  • PipPipPip
  • 1,244 posts
  • Location/dev/swat

Posted 28 July 2009 - 12:56 AM

You are most welcome.
Mark the thread as solved.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com