Jump to content

Re-populating a checked Radio Button on a form - Please Help.


dweezer81

Recommended Posts

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 :)

Link to comment
Share on other sites

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"';
	}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.