I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value.
I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:
$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}
Thanks
It's pretty simple, take a look at the code below:
The form:
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
PHP code:
<?php
$answer = $_POST['ans'];
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
A very more efficient way to do this in php:
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
and for check boxes multiple choice:
<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>
Just simply use isset($_POST['radio']) so that whenever i click any of the radio button, the one that is clicked is set to the post.
<form method="post" action="sample.php">
select sex:
<input type="radio" name="radio" value="male">
<input type="radio" name="radio" value="female">
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['radio'])){
$Sex = $_POST['radio'];
}
?>
<?php
if (isset($_POST['submit']) and ! empty($_POST['submit'])) {
if (isset($_POST['radio'])) {
$radio_input = $_POST['radio'];
echo $radio_input;
}
} else {
}
?>
<form action="radio.php" method="post">
<input type="radio" name="radio" value="v1"/>
<input type="radio" name="radio" value="v2"/>
<input type="radio" name="radio" value="v3"/>
<input type="radio" name="radio" value="v4"/>
<input type="radio" name="radio" value="v5"/>
<input type= "submit" name="submit"value="submit"/>
</form>
take a look at this code
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
php
<?php
if(isset($_POST['submit'])){
if(isset( $_POST['ans'])){
echo "This is the value you are selected".$_POST['ans'];
}
}
?>
I suggest you do it through the GET request:
for example, index.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="button" value="submit" onclick="sendPost()" />
</form>
<script type="text/javascript">
function sendPost(){
var value = $('input[name="ans"]:checked').val();
window.location.href = "sendpost.php?ans="+value;
};
</script>
this is sendpost.php:
<?php
if(isset($_GET["ans"]) AND !empty($_GET["ans"])){
echo $_GET["ans"];
}
?>
my form:
<form method="post" action="radio.php">
select your gender:
<input type="radio" name="radioGender" value="female">
<input type="radio" name="radioGender" value="male">
<input type="submit" name="btnSubmit" value="submit">
</form>
my php:
<?php
if (isset($_POST["btnSubmit"])) {
if (isset($_POST["radioGender"])) {
$answer = $_POST['radioGender'];
if ($answer == "female") {
echo "female";
} else {
echo "male";
}
}else{
echo "please select your gender";
}
}
?>
Related
How can I send a radio or checkbox value to the $_POST array even when the field is empty?
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender">
<input type="submit">
</form>
Here is what I get from the page if I hit submit without even filling out data.
Array
(
[name] =>
[email] =>
)
As you see, without touching the input type text, their value was pushed to the $_POST array. How can I do this with the radio box? I essentially want to "set" it, although it is blank just like the text inputs.
I know I could always call
<?php
if(isset($_POST['gender'])) {
//Code
}
?>
But that's not necessarily what I'm looking for. I need it to set automatically. Thanks in advance!
Try this it will work :
Unchecked radio elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="Gender" value="1"/>Male
<input type="submit" name="submit" value="submit"/>
</form>
Should be :
HTML :
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" value="male"/>Test
<input type="submit" name="submit" value="submit"/>
</form>
PHP Code :
if(isset($_POST['submit']))
{
echo $radio_value = $_POST["radio"];
}
Consider this example:
// Set a default value as male
<input type="radio" name="gender" checked value="male"> Male
<input type="radio" name="gender" value="femail"> Female
and you will get the value
Array
(
[name] =>
[email] =>
[gender]=>male
)
You only get the checked radio in the $_POST array
If you want to send blank value automatically then
By default checked that radio button and set blank value:
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" value='' checked>
<input type="submit">
</form>
HTML :
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" checked value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
<input type="submit" name="submit" value="submit"/>
</form>
PHP :
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
<form action="" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" checked value="male"> Male
<input type="radio" name="gender" value="femail"> Female
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.
Here is my code,
$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}
<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>
Please help me regarding this. Thanks in advance.
You need to apply condition on checked HTML attribute.
Try this:
<form>
Address : <br>
<textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
Gender : <br>
<input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : ''; ?> >Male
<input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : ''; ?>>Female <br>
<input type="submit" value="Save">
</form>
Value has to be placed between the openning and closing tags :
<texterea name="whatever">Textarea value goes here</textarea>
To set a radio/checkbox as selected/checked, you need to add to it a "checkded" attribute :
<!-- HTML4 -->
<input type="radio" name="whatever" value="value1" checked="checked" /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
<!-- HTML5 -->
<input type="radio" name="whatever" value="value1" checked /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
How to echo 0, When not check checkbox ?
When press submit button, It's will echo 11
But i want to echo 101 (echo 0 for not checked ckeckbox).
How can i do that ?
<form id="form02" name="form02" method="post">
<input name="h_type[]" type="checkbox" id="h_type[]" value="0" checked/><input type="hidden" name="more[]" value="1"> <br/>
<input name="h_type[]" type="checkbox" id="h_type[]" value="1" /><input type="hidden" name="more[]" value="1"> <br/>
<input name="h_type[]" type="checkbox" id="h_type[]" value="2" checked/><input type="hidden" name="more[]" value="1"> <br/>
<input type="submit" name="button" id="button" value="Submit" />
</form>
<?php
$data_h_type=$_POST['h_type'];
$data_more=$_POST['more'];
if(count($data_h_type)>0){
foreach($data_h_type as $key=>$value){
echo $data_more[$value];
echo "<br>";
}
}
?>
echo isset($data_more[$value]) ? $data_more[$value] : 0;
Hi do anyone know how to Assign a value to a "Submit" type button on HTML to the PHP process?
Currently the user needs to click a button which would then assign a global variable a value. etc. button 1 = 10, button 2 = 30... Attached are the codes that I have typed in so far. Please help! Thanks!
<html>
<head>
<title>Test </title>
</head>
<body>
<h2>
Test Generator</h2>
<p>
<br />
<input type="submit" name="action" value="2 Weeks"/>
<p>
<input type="submit" name="action" value="1 Month"/></p>
<p>
<input type="submit" name="action" value="3 Months" /></p>
<p>
<input type="submit" name="action" value="6 Months" /></p>
<p>
<input type="submit" name="action" value="1 Year" /></p>
<?php
$dateSelection;
switch ($_POST['action'])
{
case '2 Weeks':
$dateSelection==1;
break;
case '1 Month':
$dateSelection==2;
break;
case '3 Months':
$dateSelection==3;
break;
case '6 Months':
$dateSelection==4;
break;
case '1 Year':
$dateSelection==5;
break;
default:
echo "Something is wrong...";
break;
}
?>
</body>
Thanks!
First of all make a form.
<form id="someform" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ; ?>">
<input type="submit" name="action" value="1 year">
<input type="submit" name="action" value="2 year">
</form>
Then add php code on the same page at the bottom:
<?php
echo $_POST['action'];
?>
Now if you load the page (it will show an error for now that's it not assigned) now click one of your buttons. It should show 1 or 2 years depending on what button you clicked.
If this is what you mean with your question i will help you further and remove errors and help you send it to another page if you want.
You could try putting the submit buttons inside a label for a radio button...
<?php
if(!empty($_POST['action']) {
echo $_POST['action'],' weeks selected';
}
?>
<style>
.hidden-radio{display:none;}
</style>
<form action="" method="post">
<input type="radio" name="action" value="2" id="2_weeks" class="hidden-radio" />
<label for="2_weeks"><input type="submit" value="2 Weeks"/></label>
<input type="radio" name="action" value="4" id="4_weeks" class="hidden-radio" />
<label for="4_weeks"><input type="submit" value="1 Month"/></label>
<input type="radio" name="action" value="13" id="13_weeks" class="hidden-radio" />
<label for="13_weeks"><input type="submit" value="3 Months"/></label>
<input type="radio" name="action" value="26" id="26_weeks" class="hidden-radio" />
<label for="26_weeks"><input type="submit" value="6 Months"/></label>
<input type="radio" name="action" value="52" id="52_weeks" class="hidden-radio" />
<label for="52_weeks"><input type="submit" value="1 Year"/></label>
</form>
This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>