dweezer81 Posted September 28, 2014 Share Posted September 28, 2014 Hi people, I'm new to PHP, and I'm having some real difficulties with trying to re-populate a radio button selection on a form. Basically, I'm working on a WordPress plugin which uses custom fields to create a Bet custom post type. I need to have my form submit the user input, then re-populate it so that if the user wants to edit and update the Bet data, they don't have to fill in the whole form details again, they can just edit what they need to, and update (re-submit) the form. I've managed to get all of the form elements re-populating, apart from a radio button. I've been trawling round the internet for most of yesterday evening and this morning, trying different things, but nothing I've found works for me. I'd really appreciate some help if anyone knows how to do this. Here's my code so far (non working) <?php $win_status = 'checked = "unchecked"'; $ew_status = 'checked = "unchecked"'; if (isset($bet_details_bet_type)) { $selected_radio = $bet_details_bet_type; if ($selected_radio == 'WIN') { $win_satus = 'checked = "checked"'; } elseif ($selected_radio == 'EW') { $ew_status = 'checked = "checked"'; } } echo '<label for="bet_details_bet_type">Bet Type: </label>'; echo '<input type="radio" name="bet_details_bet_type" id="bet_details_bet_type" value="WIN" '. esc_attr( $win_status) .' /> Win '; echo '<input type="radio" name="bet_details_bet_type" id="bet_details_bet_type" value="EW" '. esc_attr( $ew_status) .' /> Each Way </br>'; $bet_details_bet_type is the VARIABLE that gets sent to / pulled from $_POST I have another function which is handling that, which has the following in it. $bet_details_bet_type = $_POST['bet_details_bet_type']; update_post_meta( $post_id, 'bet_details_bet_type', $bet_details_bet_type ); I know that the sending and retrieving is working okay, as the Value is stored and retrieved when I update (submit) the bet, but I just can't seem to get the radio button to be re-checked to the value that was submitted. Again, if any can help with this I would be very grateful. Thanks, Dweezer Quote Link to comment Share on other sites More sharing options...
dweezer81 Posted September 28, 2014 Author Share Posted September 28, 2014 Just noticed the typo on $win_status... but changed it, tried it. Still doesn't work Quote Link to comment Share on other sites More sharing options...
Frank_b Posted September 28, 2014 Share Posted September 28, 2014 try: $win_status = ''; $ew_status = ''; if (isset($_POST['bet_details_bet_type'])) { $selected_radio = $_POST['bet_details_bet_type']; if ($selected_radio == 'WIN') { $win_status = ' checked="checked"'; } elseif ($selected_radio == 'EW') { $ew_status = ' checked="checked"'; } } Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 28, 2014 Share Posted September 28, 2014 Also, check your page source. I could be wrong, but I bet the esc_attr() call is encoding (or possibly just adding a slash to) the quotes around 'checked'. Either way, chances are that'll break it. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 28, 2014 Share Posted September 28, 2014 Looks way too complicated for such a simple thing. Save the value of the selected radio button. When re-building the html code for the radio button, you are using a loop, no? On each pass check what value you are building and if it matches the saved value add the 'checked=' attribute to that output; if not, don't add it it. PS - Why do you have multiple elements with the same id= setting? That is worthless. Quote Link to comment 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.