Jump to content

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/
Share on other sites

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 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; ?>

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.

 

 

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!

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.

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.