how to get multiple radio button values in php - php

I have a webpage that contains two set of radio buttons. I want to do; if user select a OS in first fieldset and a language from second fieldset, then user must directed to relevant pages.
Please guide me to complete this page. I am unable to get two radio button values at a time. If I rename the radio buttons like below,
<fieldset id="group1">
<input type="radio" class="radto" name="android"/>
</fieldset>
<fieldset id="group1">
<input type="radio" class="radto" name="english"/>
</fieldset>
It's working, but the problem is users can select more than one OS and language. I need to prevent this. How can I overcome this problem.
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['android']) && isset($_POST['english'])) {
header("location: andro_eng.php");
exit();
}
if (isset($_POST['android']) && isset($_POST['french'])) {
header("location: andro_fre.php");
exit();
}
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<fieldset id="group1">
<li><input type="radio" class="radto" name="a"/> android</li>
<li><input type="radio" class="radto" name="a"/> ios</li>
<li><input type="radio" class="radto" name="a"/> symbian</li>
</fieldset>
<fieldset id="group2">
<li><input type="radio" class="radto" name="b"> english</li>
<li><input type="radio" class="radto" name="b" > french</li>
<li><input type="radio" class="radto" name="b"> spanish</li>
</fieldset>
<input type="submit" value="next" name="submit">
</form>
</body>
</html>

The name attribute has to be the same for the different options:
$( document ).ready(function() {
$('#button').click(function() {
alert($('input[name=os]:checked').val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="group1">
<input type="radio" class="radio" name="os" value="android" id="android" />
<label for="a">Android</label>
</fieldset>
<fieldset id="group1">
<input type="radio" class="radio" name="os" value="iOS" id="iOS" />
<label for="b">iOS</label>
</fieldset>
<button id="button" type="button">Get value</button>
And you need to add a value attribute to the radio inputs, otherwise you won't get any value.
Then you can get the values from PHP: $_POST['os'] will return android, iOS or may be empty if no value is selected.

Related

How to get customized attributes value of a html radio button in php

<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>

HTML and PHP posting in file error

I am making a poll with HTML and PHP and I want to post the poll answers in a text file. I have 6 things to choose from.
The name is answer and the values are a1, a2, a3, a4, a6. How to post a1 or a2 or a3... in the file.
You know when you click on the answer with id a1 to post in a new line a1 in the file.
HTML:
<form action="php/vote.php">
<b><strong>Vote:</strong></b> <br>
<input type="radio" name="answer" value="a1" id="a1">a<br>
<input type="radio" name="answer" value="a2" id="a2">b<br>
<input type="radio" name="answer" value="a3" id="a3">c<br>
<input type="radio" name="answer" value="a4" id="a4">d<br>
<input type="radio" name="answer" value="a5" id="a5">e
<br>
<input type="submit" name="submit" id="submit" value="Vote">
</form>
This should do what you are asking:
<?php
if(isset($_POST['answer'])){
file_put_contents("filename.txt", $_POST['answer']."\n");
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<b><strong>Vote:</strong></b> <br>
<input type="radio" name="answer" value="a1" id="a1">a<br>
<input type="radio" name="answer" value="a2" id="a2">b<br>
<input type="radio" name="answer" value="a3" id="a3">c<br>
<input type="radio" name="answer" value="a4" id="a4">d<br>
<input type="radio" name="answer" value="a5" id="a5">e
<br>
<input type="submit" name="submit" id="submit" value="Vote">
</body>
</form>
</html>
I have added method="POST" to your original HTML so that the receiving PHP can inspect the $_POST variable to see what was sent through from the form. $_POST['answer'] contains the value of the "value" field corresponding to the selected radio button. Once you know this, it is easy to call file_put_contents to write that value to your file. Append a newline "\n" to ensure that each call writes to a separate line of the file.

Radio button value not updating in PHP

I have 4 radio buttons in my form:
<tr><td>Type</td><td>
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D</td></tr>
On page load I set one of the radio buttons using jquery
$("#b").prop("checked", true);
Now I select the value d in my form and submit. In PHP I echo $_POST['type'] , I am always getting the value which was set during page load using jquery i.e. in this case b instead of d.
Why is the value not updating?
Thanks.
UPDATE:Thanks all, it was due to unintentional val() called on radio button. So if radio button value is set using val() it will not change later, strange behavior.
In jQuery 1.6+
$('#b').prop('checked', true);
$('#b').prop('checked', false);
jQuery 1.5 and below
$('#b').attr('checked','checked');
$('#b').removeAttr('checked');
instead of using
$("#b").prop("checked", true);
why dont you write your radio buttons as
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" checked="checked" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D
Works like a charm ;-)).
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
$('#b').attr('checked', 'checked');
});
</script>
</head>
<body>
<?php
if(isset($_POST['sbmt']) && isset($_POST['type'])) {
?>
<h1>Selected type: <?php echo($_POST['type']); ?></h1>
<?php
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<ul>
<li><input type="radio" name="type" id="a" value="a" /> A</li>
<li><input type="radio" name="type" id="b" value="b" /> B</li>
<li><input type="radio" name="type" id="c" value="c" /> C</li>
<li><input type="radio" name="type" id="d" value="d" /> D</li>
</ul>
<input name="sbmt" type="submit" value="Submit" />
</form>
</body>
</html>
Try this code
<?php
if(isset($_REQUEST['sb']))
{
echo $_REQUEST['type'];
}
?>
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(e) {
$('#b').attr('checked','checked');
});
</script>
<tr><td>Type</td><td>
<form name="frm" method="post" action="">
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D</td></tr>
<input type="submit" name="sb" value="submit" />
</form>
</body>
</html>
Try:
$("#b").attr("checked", true);
or
$("#b").attr("checked", "checked");

use multiple Form Action Based On Radio Button Selection

<p><h3 style="font-size:18px;">Call Status:</h3>
<body>
<div>
<form action="immediate.php">
<label><input type="radio" name="colorRadio" <?php if (isset($colorRadio) && $colorRadio=="immediate" ) echo "checked";?>value="IMMEDIATE"> Call Immediate</label>
</form>
<form action="scheduled.php">
<label><input type="radio" name="colorRadio" <?php if (isset($colorRadio) && $colorRadio=="scheduled") echo "checked";?> value="SCHEDULED"> Call Scheduled</label>
</form></div>
<div class="IMMEDIATE box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="SCHEDULED box"><p>
<td>
<input type="Text" id="demo1" name="demo1" maxlength="25" size="25"><a href="javascript:NewCal('demo1','DDMMYYYY',true,24)">
<img src="img/cal.gif" width="16" height="16" border="0"></a>
<span class="descriptions">Pick a Date</span>
</p></div>
Hi Sir this is my code...I just want to add multiple form action on selection of radio button... Call Immediate radio button is selected then send my form at immediate.php page and if Call Scheduled is selected then then page send on scheduled.php page
Its easier with jQuery. Basic version is:
<input type="radio" name="group1" id="basic" value="a.html" onclick="setLocation(this)" checked="checked" />
function setLocation(element) {
document.forms[0].action = element.value
}
here's an example for you
<html>
<head>
<script type="text/javascript">
function select()
{
var1=document.getElementById("radio1");
var2=document.getElementById("radio2");
if(var1.checked==true)
{
document.myform.action="immediate.php";
}
else
{
document.myform.action="Scheduled.php";
}
}
</script>
</head>
<body>
<form action="immediate.php" method="post" name="myform" onsubmit="select()">
<input type="radio" id="radio1" name="colorRadio" value="IMMEDIATE">
<input type="radio" id="radio2" name="colorRadio" value="Call SCHEDULED">
<input type="submit" value="Submit">
</form>
</body>
</html>
It check for selected radio button when submit is clicked and assign action based on selection.
Hope it helps

radio buttons value if statement

I have several groups of radio buttons that I want to use in an IF statement (or if you have a better solution)
Users will come to the site, select the buttons, then select submit. After submitting, I want the user to see instantly if they should "refer patient" or "don't refer patient".
I am not sure of a couple of things:
How do I make the "submit" button cause the input to be calculated (meaning, the user gets the instant response)
Since there are several combinations of inputs that can create a "refer" or "don't refer" response, can I add multiple conditions to the IF statement? Also, how can I include radio buttons in the statement - do I just use the "value" of the button. I only learned the very basic method of using numbers..
Below is my code so far. I tried to start the IF statement with values. Not sure if doing it right.
Any help is greatly appreciated!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Indications for Mohs</title>
<?php
$Patient_status='unchecked';
$Primary_status='unchecked';
$Type_status='unchecked';
$BCCT_status='unchecked';
$SCCT_status='unchecked';
$Size_status='unchecked';
$Area_status='unchecked';
if (isset($_POST['Submit'])) {
$selected_radio=$_POST['REFER'];
if (selected_radio == "Healthy" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H" or "Immunocompromised" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H")
?>
</head>
<body>
<form name="Indications" action="" method="POST">
<p><h2><strong><u>Indications for Mohs Surgery</u></strong></h2>
</p>
<strong>Patient </strong>
<div alighn="center"><br>
<input type="radio" name="Patient" value="Healthy">Healthy<br>
<input type="radio" name="Patient" value="Immunocompromised">Immunocompromised<br>
<input type="radio" name="Patient" value="Genetic">Genetic Syndrome<br>
<hr>
<strong>Primary vs Recurrent</strong>
<div alighn="center"><br>
<input type="radio" name="Primary" value="Primary">Primary<br>
<input type="radio" name="Primary" value="Recurrent">Recurrent<br>
<hr>
<strong>Type</strong>
<div alighn="center"><br>
<input type="radio" name="Type" value="BCC">BCC<br>
<input type="radio" name="Type" value="SCC">SCC<br>
<input type="radio" name="Type" value="LM">LM or MIS<br>
<hr>
<strong>BCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="BCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="BCCT" value="Nodular">Nodular<br>
<input type="radio" name="BCCT" value="Superficial">Superficial<br>
<hr>
<strong>SCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="SCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="SCCT" value="Nonaggressive">Nonaggressive<br>
<input type="radio" name="SCCT" value="Verrucous">Verrucous<br>
<input type="radio" name="SCCT" value="KA">KA - type SCC<br>
<input type="radio" name="SCCT" value="Bowen">In situ SCC/Bowen<br>
<input type="radio" name="SCCT" value="AK">AK<br>
<hr>
<strong>Size (cm)</strong>
<div alighn="center"><br>
<input type="radio" name="Size" value="0.5"><0.6<br>
<input type="radio" name="Size" value="0.6-1">0.6-1<br>
<input type="radio" name="Size" value="1.1-2">1.1-2<br>
<input type="radio" name="Size" value="2">>2<br>
<hr>
<strong>Area</strong>
<div alighn="center"><br>
<input type="radio" name="Area" value="H">H<br>
<input type="radio" name="Area" value="M">M<br>
<input type="radio" name="Area" value="L">L<br>
<hr>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
<p><strong><u>Definitions</u>:</strong><br>
Nonaggressive SCC: <2mm depth without other defining features, Clark level ≤III<br>
Area H: 'Mask Areas' of face (central face, eyelids, eyebrows, nose, lips [cutaneous/mucosal/vermillion], chin, ear, and periauricular skin/sulci, temple), genitalia (including perineal and perianal), hands, feet, nail units, ankles, nipples/areola<br>
Area M: Cheeks, forehead, scalp, neck, jawline, pretibial surface<br>
Area L: Trunk and extremities (excluding pretibial surface, hands, feet, nail units and ankles)</p>
</div>
</form>
</body>
</html>
If you want to have the submit display a response instantly you should use JavaScript as this does not require a form submission/call to a server. You can use the onsubmit event.
Regarding checking for if a radio button is checked, use the .checked property of an element:
document.getElementById('elem').checked //true or false
Almost everything is wrong with that code.
Let's start...
You don't have a REFER element in your form, so $_POST['REFER'] is never set. To access radio button values, you need to access with their relevant name as the index key to the $_POST[] array. E.g. $_POST['Patient'], $_POST['Primary'] ...etc. Those will give you the value of the radio button selected within that group.
Secondly, your conditional statements are wrong in the if statement. To compare conditional statements, you have to specifically compare the variable with different values every time. You'd have to say
if ($selected_radio == "Healthy" && $selected_radio == "Patient") {
// code goes here
}
And also, to check which radio button was selected, you need to access $_POST['<Radio_group_name>'] and this will give you the value of the radio button selected for that group. e.g.
$_POST['Patient']
would give Healthy if user selected that one for the group.

Categories