I am trying to check whether any of the radio buttons on a web page have been selected when using PHP. I have searched around a lot and tried multiple things but nothing has seemed to work. I am trying to return a value of false to the variable $check so that later on after multiple of these checks I can stop the form from submitting.
HTML:
<form id="Quiz" method="post" action="markquiz.php" novalidate="novalidate">
<fieldset>
<legend>What does the 'S' in OWASP stand for?</legend>
<p><label for="Superman"><input type="radio" name="Q1" value="Superman" required="required"/>Superman</label>
<label for="Segregation"><input type="radio" name="Q1" value="Segregation"/>Segregation</label>
<label for="Security"><input type="radio" name="Q1" value="Security"/>Security</label>
<label for="Simple"><input type="radio" name="Q1" value="Simple"/>Simple</label>
</p>
</fieldset>
<p>
<input id ="submit" type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</p>
</form>
PHP:
//Q1
if (isset($_POST["Q1"])){
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 == "Security") {
$Score = $Score + 1;
}
else {
$check = false;
}
Thanks!
Try this and do add name="submit" in your button
if (isset($_POST["submit"])){
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 != "") {
$Score = $Score + 1;
}
elseif($AnsQ=="") {
$check = false;
}
if(isset($_POST['Submit'])) {
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 == "Security") {
$Score = $Score + 1;
} else {
$check = false;
}
}
Related
I have got the php, html code.
And I want to post the multiple checkbox values, but this does not work dunno why, I can print count or array, it prints(0), does not matter
the array values is always empty
<form action = 'main.php?w=creatNewTemplate2' method = 'post'>
<input type = 'text' name = 'templateName' maxlength = '30'/><br />
<input type= 'checkbox' name= 'exercises[]' value='A' />A<br />
<input type= 'checkbox' name= 'exercises[]' value='B' />B<br />
<input type = 'submit' value = 'Sukurti'/>
</form>
if($w == "creatNewTemplate2")
{
$d = $_POST['exercises'];
$ddd = count($d);
print_r($_POST);
}
I think this could work for you:
if($w == "creatNewTemplate2")
{
$d = $_POST['exercises'];
$ddd = count($d);
for ($x = 0; $x < $ddd; $x++) {
echo $_POST['exercises'][$x].'<br>';
}
}
If you use same name you need to loop them
This should be work
Or you can change the two checkbox to select multiple
<form action="main.php?w=creatNewTemplate2" method="post">
<input type="text" name="templateName" maxlength="30"/><br/>
<select multiple name="exercices[]">
<option>A</option>
<option>B</option>
</select>
<input type="submit" value="Sukurti"/>
</form>
<?php
if($_GET["w"] == "creatNewTemplate2")
{
foreach ($_POST["exercises"] as $ex) {
echo $ex . '<br>';
}
}
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
I got this form:
<form name="formx" id="formx" action="var.php" method="POST">
<input type="checkbox" name="f_check1"> Check 1
<input type="checkbox" name="f_check2">Check 2
<input name="f_register" value="Register" type="submit">
</form>
And on the var.php file I have:
<?php
if($_POST['f_register'] == "Register") {
$check1 = $_POST['f_check1'];
$check2 = $_POST['f_check2'];
}
echo $check1. "<br>" ;
echo $check2;
?>
And when i fill the form and go to the var.php, i only get results if the checkbox is on, and i want it to say "true" if checked and "false" if not.
P.S: I'm using XAMPP to run the website.
Use this block:
<?php
if($_POST['f_register'] == "Register") {
$check1 = isset($_POST['f_check1']);
$check2 = isset($_POST['f_check2']);
}
if($check1) echo '<br>check1 true';
else echo 'check1 false';
if($check2) echo '<br>check2 true';
else echo '<br>check2 false';
?>
$check1 = isset($_POST['f_check1']);
$check2 = isset($_POST['f_check2']);
Unchecked checkboxes aren't sent to the server. So you can account for that with:
$check1 = isset($_POST['f_check1']) ? true:false;
$check2 = isset($_POST['f_check2']) ? true:false;
Try this?
<form name="formx" id="formx" action="var.php" method="POST">
<input type="checkbox" name="f_check1"> Check 1
<input type="hidden" name="f_check1" value="0" />
<input type="checkbox" name="f_check2">Check 2
<input type="hidden" name="f_check2" value="0" />
<input name="f_register" value="Register" type="submit">
</form>
Edited as requested:
The hidden field with the same name will be passed if the checkbox is not checked.
I have an error that I can't figure out...
Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?
HTML:
<form method="post" action="newRequest.php">
<input type="text" name="title" />
<input type="hidden" name="fname" value="0" />
<input type="checkbox" name="fname" value="1"/>
<input type="hidden" name="ename" value="0" />
<input type="checkbox" name="ename" value="1" />
<input type="hidden" name="seat" value="0" />
<input type="checkbox" name="seat" value="1" />
<input type="hidden" name="fields" value="0" />
<input type="text" id="fields" name="fields" />
<input type="submit" />
</form>
PHP:
if (strlen($_POST[title]) > 2) {
$toDb[title] = $_POST[title];
} else {
error('title');
}
$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];
if ($_POST[fields] > 0) {
$i = 0;
while ($i < $_POST[fields]) {
$toDb[optional][$i] = $_POST[optional-$i];
$i++;
}
$toDb[optional] = serialize($toDb[optional]);
} else {
$toDb[optional] = 0;
}
newEvent($toDb,$dbh);
JQuery that is adding dynamical fields:
$(document).ready(function() {
$('#fields').focusout(function(){
var fields = $('#fields').val();
var i = 0;
while(i < fields) {
$('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
i++;
}
})
})
You should quote array indexes. It should be
$toDb['optional'][$i] = $_POST['optional'.$i];
You are missing commas in $_POST
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
Here is your modified code
if (strlen($_POST['title']) > 2) {
$toDb['title'] = $_POST['title'];
} else {
error('title');
}
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
if (count($_POST) > 0) {
$i = 0;
while ($i < count($_POST)) {
$toDb['optional'][$i] = $_POST['optional-'.$i];
$i++;
}
$toDb['optional'] = serialize($toDb['optional']);
} else {
$toDb['optional'] = 0;
}
newEvent($toDb,$dbh);
Also use count() to check if $_POST has values > 0.
I faced the same problem and I solved it using Javascript, like this :
add a new text field every time a button is pressed
Am new to the world of development and am just starting to pick up PHP. I have basic form that attempts to validate the checkboxes the user has selected or checked. My code is below. The question I have is why is that when I have the order of my form as follows, the form does not pass the value NET, PHP or RUBY and the values that are costantly passed are no.
--- Form code that does not work ---
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="hidden" name="ch3" value="no">
<input type="submit" name="submit" value="submit">
However if my code is as follows;
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch3" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
The boxes appear checked. The entire code below.
<?php
$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
if($_POST["ch1"] == "net") {
$ch1status = "checked";
}
}
if(isset($_POST["ch2"])) {
if($_POST["ch2"] == "php") {
$ch2status = "checked";
}
}
if(isset($_POST["ch3"])) {
if($_POST["ch3"] == "ruby") {
$ch3status = "checked";
}
}
if ($_POST["ch1"] == "no" && $_POST["ch2"] == "no" && $_POST["ch3"] == "no") {
print "There is no such choice";
}
}
?>
<html>
<head>
<title>Sample form checkbxoes</title>
</head>
<body>
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch3" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
print $_POST["ch1"];
print $ch1status;
}
if(isset($_POST["ch2"])) {
print $_POST["ch2"];
print $ch2status;
}
if(isset($_POST["ch3"])) {
print $_POST["ch3"];
print $ch3status;
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
</body>
</html>
</form>
Also is there any other way of validating if the user has not selected any checkboxes as opposed to using hidden form fields.
Its just a browser-issue and its quite simple: The elements have the same name and the later element overwrites the first one.
Another way of validating, if a checkbox is not checked is to check, if its set in the $POST-array. If its missing, its treated like "not checked".
UNDEFINED INDEXES:
This is because checkboxes are only sent if they are checked. One thing you can do is always check the variable with isset (e.g. isset($_POST['ch1'])) before using them; another is to name your checkboxes the same thing with a [] following the name (e.g. name="languages[]") and then do something like this:
// Create a list of languages that are OK (remember, some users are malicious)
$languages = array('net','php','ruby');
// Compile a list of the answers the user picked; force it to be an
// array by either explicitly casting to an array, or using an empty array
// if none chosen
$picked = isset($_POST['languages']) ? (array)$_POST['languages'] : array();
// first, use array_intersect to remove entries present in one and not the other
// i.e. invalid entries from the client or entries not picked from the whole list
// then, "flip" the array so that the values become keys,
// because isset is faster than in_array
$valid_langs = array_flip(array_intersect($languages, $picked));
// check on languages
if (isset($valid_langs['php'])) { /* PHP picked */ }
if (isset($valid_langs['net'])) { /* NET picked */ }
if (isset($valid_langs['ruby'])) { /* Ruby picked */ }
Simpler Solution:
<form>
<input type="checkbox" name="php" value="yes" />
<input type="checkbox" name="net" value="yes" />
<input type="checkbox" name="ruby" value="yes" />
</form>
<?php
$php = $net = $ruby = 'unchecked';
if (!isset($_POST['php'],$_POST['net'],$_POST['ruby'])) {
echo 'There is no such choice';
}
else {
if (isset($_POST['php']) && $_POST['php'] == 'yes') {
$php = 'checked';
}
if (isset($_POST['net']) && $_POST['new'] == 'yes') {
$net = 'checked';
}
if (isset($_POST['ruby']) && $_POST['ruby'] == 'yes') {
$ruby = 'checked';
}
}
// ... snip ...
There are a great many ways to do this. Hopefully you will be interested in learning many of them.
Php is all server-side, so in order to keep them from submitting you'll need client-side validation. Easiest client-side validation is with javascript, or jQuery's Validation Plugin if you're already using jQuery (which you should be if you plan on using AJAX at any point).
And yes, you can get rid of those hidden inputs.
You do not need those hidden fields. Remove them and it should work.
EDIT:
Check out this modification
$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";
if(isset($_POST["submit"])) {
if(#$_POST["ch1"] != "") {
$ch1status = "checked";
}
if(#$_POST["ch2"] != "") {
$ch2status = "checked";
}
if(#$_POST["ch3"] != "") {
$ch3status = "checked";
}
if (#$_POST["ch1"] . #$_POST["ch2"] . #$_POST["ch3"] == "") {
print "There is no such choice";
}
}
?>
<html>
<head>
<title>Sample form checkbxoes</title>
</head>
<body>
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="checkbox" name="ch1" value="net" <?php echo $ch1status; ?>>.NET
<input type="checkbox" name="ch2" value="php" <?php echo $ch2status; ?>>PHP
<input type="checkbox" name="ch3" value="ruby" <?php echo $ch3status; ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
print $_POST["ch1"];
print $ch1status;
}
if(isset($_POST["ch2"])) {
print $_POST["ch2"];
print $ch2status;
}
if(isset($_POST["ch3"])) {
print $_POST["ch3"];
print $ch3status;
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
</body>
</html>