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");
Related
I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />
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.
I am a beginner in PHP. So I am working on a small task in which I want to make a form in which there are MCQS and there is a countdown timer above it of 10 minutes. When the count down timer is up the form should expire and should show the result automatically.
I have made the form as coded below but I don't know what to do so that I can add timer in this code so that the form expires and shows the result of the right selected answers.
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<div id="page-wrap">
<h1>Final Quiz for Lip building</h1>
<form action="quiz.php" method="post" id="quiz">
<ol>
<li>
<h3>CSS Stands for...</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) None of the above</label>
</div>
</li>
<li>
<h3>Internet Explorer 6 was released in...</h3>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
<label for="question-2-answers-A">A) 2001</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
<label for="question-2-answers-B">B) 1998</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
<label for="question-2-answers-C">C) 2006</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
<label for="question-2-answers-D">D) 2003</label>
</div>
</li>
<li>
<h3>SEO Stand for...</h3>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
<label for="question-3-answers-A">A) Secret Enterprise Organizations</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
<label for="question-3-answers-B">B) Special Endowment Opportunity</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
<label for="question-3-answers-C">C) Search Engine Optimization</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
<label for="question-3-answers-D">D) Seals End Olives</label>
</div>
</li>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</div>
</body>
</html>
You need JavaScript for such behavior, let's say you use the following JS:
window.setTimeout(function() {
document.forms['form_name'].submit();
}, 2000);
form_name should be the name of your form. i.e
<form name = "form_name" ... >
</form>
This will delay the post in milliseconds (2 seconds) and then it should take you to another page where you can show the correct answers.
Fiddle
I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>
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";
}
}
?>