Jump to content

Recommended Posts

Hello,

 

Problem area: radio buttons in a user form.

Initially the radio buttons are unchecked which is fine for what i need

If one of the radio button is selected the process.php scripts recives the value via $_POST, which works fine.

if the radio button isn't selected nothing is sent via $_POST. This is where the problem is....

 

I need:

f the user forgets to select the radio button noting is sent via $_POST. If nothing is sent via $_POST  the process.php

script is unable to verify the wheather the user made a selection or not.

 

print_r($_POST) ==>Array ( [name] => => [birthday] => [formSubmit] => Submit )

radiobutton doesn't appear. how can in include the radio button as part of the $_POST  so that process.php can process the selection weather its empty or not?

 

 

 

form.php

<form name="formprocess" method="POST" action="process.php"> 
    Name: <input type="text" name="name" value="<?php echo $name; ?>" > <br /> 
    eMail: <input type="text" name="email" value="<?php echo $email; ?>" > <br /> 
    Birthday:<input type="text" name="birthday" value="<?php echo $birthday; ?>" /><br /> 
    <br /> 
    <p> <input type="radio" name="radiobutton" value="1" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 1) ?  'checked':''; ?>> OptionOne </p> 
    <p> <input type="radio" name="radiobutton" value="2" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 2) ?  'checked':''; ?>> OptionTwo </p> 
    <p> <input type="radio" name="radiobutton" value="3" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 3) ?  'checked':''; ?>> OptionThree</p> 
     
    <input type="submit" name="formSubmit" value="Submit"> 
</form> 

 

process.php

$allowedFields = array(
	'name',
	'email',
	'birthday',
	'radiobutton',
);

$requiredFields = array(
	'name',
	'email',
	'radiobutton',
);
echo 'radiobutton1: '. $_POST['radiobutton'];
echo '<br />';
 print_r($_POST);
//loop through the POST array

$fieldErrors = array();
foreach($_POST as $key=>$value)
{	// allowed fields
	if(in_array($key, $allowedFields))
	{	$$key = $value;
		// required field?
		if(in_array($key, $requiredFields) && $value == '')
		{	$fieldErrors[] = "The field $key is required."; 
		}
	}	
}

 

Link to comment
https://forums.phpfreaks.com/topic/275428-sending-radiobutton-value-via-_post/
Share on other sites

Another technique is to put a hidden field (with the same name as the radio buttons and zero value) before the button group. If no button is selected the hidden field value is sent.

 

<input type='hidden' name='radiobutton' value='0' />
<p> <input type="radio" name="radiobutton" value="1" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 1) ? 'checked':''; ?>> OptionOne </p>
<p> <input type="radio" name="radiobutton" value="2" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 2) ? 'checked':''; ?>> OptionTwo </p>
<p> <input type="radio" name="radiobutton" value="3" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 3) ? 'checked':''; ?>> OptionThree</p>

Thanks everybody, I greatly appreciate your post replies back.

 

I ended up going with barands answer. I added the hidden input field and it seem to have fixed the issue in my script..

I did change one thing in Brands answer i changed the value='0' to value=''. this gave me what i needed..

 

again thanks for the help.

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.