jonw118 Posted July 28, 2009 Share Posted July 28, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/ Share on other sites More sharing options...
Psycho Posted July 28, 2009 Share Posted July 28, 2009 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 /> "; } Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884609 Share on other sites More sharing options...
mmarif4u Posted July 28, 2009 Share Posted July 28, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884610 Share on other sites More sharing options...
Psycho Posted July 28, 2009 Share Posted July 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884618 Share on other sites More sharing options...
jonw118 Posted July 28, 2009 Author Share Posted July 28, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884624 Share on other sites More sharing options...
jonw118 Posted July 28, 2009 Author Share Posted July 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884627 Share on other sites More sharing options...
mmarif4u Posted July 28, 2009 Share Posted July 28, 2009 <?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'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884629 Share on other sites More sharing options...
jonw118 Posted July 28, 2009 Author Share Posted July 28, 2009 Awesome! That did the trick. Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884634 Share on other sites More sharing options...
mmarif4u Posted July 28, 2009 Share Posted July 28, 2009 You are most welcome. Mark the thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/167750-radio-button-value-on-php-post/#findComment-884636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.