test.php
<form action="test2.php" method="post">
Q1: <br />
<input type="radio" name="q1" value="true" />T<br />(Correct Answer)
<input type="radio" name="q1" value="false" />F<br />
Q2: <br />
<input type="radio" name="q2" value="true" />T<br />
<input type="radio" name="q2" value="false" />F<br />(Correct Answer)
<input type="submit" value="Score" />
</form>
test2.php
<?php
//process code from test.php
?>
I want to get the value of the radio buttons from each question and check whether is true or false. I try this in test2.php:
(1) if($_POST['name']) -> get error (undefined index: name)
(2) if($_POST['submit']) -> get error(undefined index: submit)
(3) if(isset($_POST['name'])-> no error, but nothing happened
How should I solve it?
You have named the radio button as q1 and q2 but trying to access it with a name name. Instead of if($_POST['name']) you have to do like
if($_POST['q1'])
and
if($_POST['q2'])
Also for checking whether the form is submitted or not, you can try this code.
if(isset($_POST))
In case if you are using if($_POST['submit']) for checking whether the form is submitted or not, submit will be the name of the submit button. So you have to set name property to submit button.
<input name="submit" type="submit" value="Score" />
Have you tried this?
$_POST['q1'];
$_POST['q2'];
You need to set the name:
<input name="submit" type="submit" value="Score" />
and to get the value like this:
$_POST['q1'];
$_POST['q2'];
the name attribute is what you send in POST/GET to the php script.
if(isset($_POST['submit']))//don't forget to check using isset()
{
/*other variables*/
$radio_value1 = $_POST['q1'];
$radio_value2 = $_POST['q2'];
}
if($_POST['name'])
to
if($_POST['q1']) {
}
if($_POST['q2']) {
}
if(isset($_POST['name'])
to
if(isset($_POST['q1'])
if(isset($_POST['q2'])
And,
<input type="submit" value="Score" /> to <input type="submit" name="submit" value="Score" />
Related
So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>
I have html checkbox like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br />
<input type="checkbox" name="language[]" value="html" />HTML<br />
<input type="checkbox" name="language[]" value="java" />Java<br />
<input type="checkbox" name="language[]" value="c++" />C++<br />
<input type="submit" value="send" />
</form>
Now I want to detect the checkbox is not checked using this PHP
if($_POST)
{
if(empty($_POST['language']))
{
echo "bla";
}
else
{
foreach($_POST['language'] as $value)
{
echo 'Checked: '.$value.'
';
}
}
}
The output is always show the checbox checked.
My question is, how can I detect the checkbox is not checked?
Example I do not check PHP and Java.
You don't need to validate checkbox by checkbox in order to determine if they are checked or not, you won't get the unchecked checkboxes values at the time you send the form, so, sending the form like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br /> <!-- checked -->
<input type="checkbox" name="language[]" value="html" />HTML<br /><!-- checked -->
<input type="checkbox" name="language[]" value="java" />Java<br /><!-- unchecked -->
<input type="checkbox" name="language[]" value="c++" />C++<br /><!-- unchecked -->
<input type="submit" value="send" />
</form>
In your PHP, you will get an array as follows:
$_POST['languages'] = array("php", "html");
Now, lets say you have an array of all the values in order to check which ones you need to delete, and which ones you need to add, a rough code example would be as follows:
$allValues = array('php', 'html', 'java', 'c++');
$valuesForAdd = $_POST['language'];
$valuesForDeletion = array_diff($allValues, $valuesForAdd);
First you need the selectable items array in the backend:
$items = array('php','html','java','c++');
You have the posted (selected) languages array here:
$_POST['language']
Not selected languages array:
$not_selected_languages = array_diff($items,$_POST['language']);
I hope it helps.
Only 'checked' checkboxes get sent as parameters in a POST request.
If you want to know which aren't checked, you could have the value list stored on PHP side; then once you receive POST data - compare the array on PHP side with POST array.
$all_vals = array('php', 'c++', 'html', 'java');
$post_vals = $_POST['languages'];
foreach ($post_vals as $post_val)
if in_array($post_val, $all_vals)
$checkbox checked
else
$checkbox not checked
I assume this gives you enough liberty to do what you need.
I'm trying to create a temperature converter that allows you to input into a text box, check a radio button and convert based on the radio button you check. However when the submit button is pressed it takes me to a page that says "the resource cannot be found" and it says there's an error finding the /converter which is the action of my form. I'm very new to this and couldn't find anything explaining how to fix it
<?php
#$input = $_GET['degrees'];
#$radio = $_GET['celorfar'];
if($radio == 'far'){
$answer = ($input * 1.8) + 32;}
else {
$answer = ($input - 32) * .56; //.56=5/9
}
echo <<<END_OF_FORM
<form method="GET" action="converter">
<input name="degrees" type="text" />
<br />
<input name="celorfar" type="radio" value="far" /> Convert to Fahrenheit<br />
<input name="celorfar" type="radio" value="cel" /> Convert to Celcius<br />
<input name="Convert" type="submit" value="Convert" /> <br />
<br />
Answer : <input name="Text1" type="text" value="$answer" /></form>
END_OF_FORM
?>
You missed file extension...
<form method="GET" action="converter.php">
I have a list of emails with a checkbox next to it, the user will be able to select which address he/she wants to email. Now i've added another checkbox that when checked will check all the other checkbox. Below is the code i wrote (with help from stackoverflow of course) :
<SCRIPT LANGUAGE="JavaScript">
function selectFunction (checkall,field)
{
if(checkall.checked==true){
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}else{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
}
</script>
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
The "select all" function works fine, but using print_r i saw that when the submit button is clicked, the submited value is the last checkbox that i selected. For example if i click 5-3-1-2, the value in $_POST is "2" and not the rest.
i realized that my code can only register ONE selected checkbox, therefore only the last one is taken into account. So I rewrote the code by adding [] behind the name of the checkbox :
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list[]" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list[]" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list[]" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list[]" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list[]" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
Now, it registers the multiple selections when i checked with print_r.(if i click 5-3-1-2, the value in $_POST is now [0]=>5,[1]=>3,[2]=>1,[3]=>2.)
But the "select all" checkbox doesn't work anymore. I assume is due to the [] which transformed the field into an array. I tried various methods (by replacing "document.myform.list" with "document.myform.list[]" etc. ) None is working so far, i'll continue experimenting but if anybody have a clear idea on how to merge the 2 codes above please help.
Thank You
Change this:
onClick="selectFunction(document.myform.selectallcb,document.myform.list)"
to this:
onClick="selectFunction(document.myform.selectallcb,document.myform['list[]'])"
Here's the fiddle: http://jsfiddle.net/NxfH6/
How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.