i have 5 checkboex. How can I get the value of them if any of them is checked and submited into the second page.
here is my html code.
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1">y
<input type="CHECKBOX" name="CHECKBOX_2" value="2">o
<input type="CHECKBOX" name="CHECKBOX_3" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
here is Javascript code
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
return true;
}
}
</script>
My dear first you need to change the names of your checkboxes to any array like this
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX[]" value="1">y
<input type="CHECKBOX" name="CHECKBOX[]" value="2">o
<input type="CHECKBOX" name="CHECKBOX[]" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
On your test.php just traverse the whole array of CHECKBOX like this
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo('check box is checked and do some thing with '.$value);
else
echo('check box is not checked');
}
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if(theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false )
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
var val=''; var arr = new Array();
if(theForm.CHECKBOX_1.checked == true){
val += theForm.CHECKBOX_1.value; arr.push(/*push val*/);
}
if(theForm.CHECKBOX_2.checked == true){
val += theForm.CHECKBOX_2.value;
}
if(theForm.CHECKBOX_3.checked == true){
val += theForm.CHECKBOX_3.value;
}
alert(val); //echo out array elements
}
}
</script>
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1"/>y
<input type="CHECKBOX" name="CHECKBOX_2" value="2"/>o
<input type="CHECKBOX" name="CHECKBOX_3" value="3"/>t
<input type="SUBMIT" value="Submit!">
</form>
[UPDATE]::
<?php
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo "var val[".++$count."] = '".$value."'";
}
?>
<script type="text/javascript">
for(var i=0; i<val.length; i++)
alert(val[i]);
</script>
Related
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;
}
}
I want to show the output of polldemo1.php on poll.php page. But it is not happening. It is showing the poll.php page's form again as output.
Please correct it.Also if anyone can explain me php with ajax, it would be really nice
poll.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
</head>
<body>
<h3>SIG Polls</h3>
<form method="POST" id="myform" class="myform" >
C/C++
<input type="checkbox" name="vote[]" value="0" />
<br />
Java
<input type="checkbox" name="vote[]" value="1" />
<br />
Matlab
<input type="checkbox" name="vote[]" value="2" />
<br />
C#/.NET
<input type="checkbox" name="vote[]" value="3" />
<br />
PHOTOSHOP
<input type="checkbox" name="vote[]" value="4" />
<br />
CCNA
<input type="checkbox" name="vote[]" value="5" />
<br />
BASIC ELECTRONICS
<input type="checkbox" name="vote[]" value="6" />
<br />
<input type="submit" name="submit" value="vote" onclick="return submitForm()" />
</form>
<div id="myResponse"></div>
<script type="text/javascript">
function submitForm() {
var form = document.myform;
var dataString = $(form).serialize();
$.ajax({
type:'POST',
url:'polldemo1.php',
data: dataString,
success: function(data){
$('#myResponse').text(data);
}
});
return false;
}
</script>
</body>
</html>
polldemo1.php
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
</head>
<?php
echo "hello";
//error_reporting(0);
if(isset($_POST['submit']))
{
/*if (isset($_COOKIE["ieeepoll"]))
{
echo "<script>alert('You have already voted'); location.href='poll.php';</script>";
} */
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$c = $array[0];
$java = $array[1];
$matlab = $array[2];
$net = $array[3];
$ph = $array[4];
$ccna = $array[5];
$be = $array[6];
foreach($_POST["vote"] as $vote)
{
if ($vote == 0)
{
$c = $c + 1;
}
if ($vote == 1)
{
$java = $java + 1;
}
if ($vote == 2)
{
$matlab = $matlab + 1;
}
if ($vote == 3)
{
$net = $net + 1;
}
if ($vote == 4)
{
$ph = $ph + 1;
}
if ($vote == 5)
{
$ccna = $ccna + 1;
}
if ($vote == 6)
{
$be = $be + 1;
}
}
//insert votes to txt file
$insert = $c."||".$java."||".$matlab."||".$net."||".$ph."||".$ccna."||".$be;
file_put_contents($filename,$insert);
echo $be;
}
?>
Add onsubmit event in for html form attribute as following
<form method="POST" id="myform" class="myform" onsubmit="return false" >
You can manage above thing by jQuery also, just need to modify your javascript code something like this.
$("#myform").on('submit', function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'polldemo1.php',
data: $("#myform").serialize(),
success: function (data, status) {
//do whatever you want to do
},
error: function (err) {
//do wahtever you want to do
}
});
});
//OR
$("#myform").on('submit', function (event) {
event.preventDefault();
submitForm();
});
Also you don't need to keep any html attribute in you php script. Remove all html tags from that script.
try this it should help you.it will display the content of the file on poll.php on submit of form.
poll.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
</head>
<body>
<h3>SIG Polls</h3>
<form method="POST" id="myform" class="myform" >
C/C++
<input type="checkbox" name="vote[]" value="0" />
<br />
Java
<input type="checkbox" name="vote[]" value="1" />
<br />
Matlab
<input type="checkbox" name="vote[]" value="2" />
<br />
C#/.NET
<input type="checkbox" name="vote[]" value="3" />
<br />
PHOTOSHOP
<input type="checkbox" name="vote[]" value="4" />
<br />
CCNA
<input type="checkbox" name="vote[]" value="5" />
<br />
BASIC ELECTRONICS
<input type="checkbox" name="vote[]" value="6" />
<br />
<input type="submit" name="submit" value="vote" onclick="return submitForm()" />
</form>
<div id="myResponse"></div>
<script type="text/javascript">
function submitForm() {
var dataString = $("#myform").serialize();
$.ajax({
type:'POST',
url:'index.php',
data: dataString,
success: function(data){
$('#myResponse').text(data);
}
});
return false;
}
</script>
</body>
</html>
polldemo1.php
<?php
echo "hello";
//error_reporting(0);
if(isset($_POST))// do not check for submit here it won't go with post
{
/*if (isset($_COOKIE["ieeepoll"]))
{
echo "<script>alert('You have already voted'); location.href='poll.php';</script>";
} */
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$c = $array[0];
$java = $array[1];
$matlab = $array[2];
$net = $array[3];
$ph = $array[4];
$ccna = $array[5];
$be = $array[6];
foreach($_POST["vote"] as $vote)
{
if ($vote == 0)
{
$c = $c + 1;
}
if ($vote == 1)
{
$java = $java + 1;
}
if ($vote == 2)
{
$matlab = $matlab + 1;
}
if ($vote == 3)
{
$net = $net + 1;
}
if ($vote == 4)
{
$ph = $ph + 1;
}
if ($vote == 5)
{
$ccna = $ccna + 1;
}
if ($vote == 6)
{
$be = $be + 1;
}
}
//insert votes to txt file
$insert = $c."||".$java."||".$matlab."||".$net."||".$ph."||".$ccna."||".$be;
file_put_contents($filename,$insert);
echo $insert;
}
?>
The value of the button should change in 1 or 0, but the echo $_POST["ordina"] give always 1 and I don't understand because the code seems correct.
<script> function order() {
if (document.ordination.ordina.value == "1") {
document.ordination.ordina.value = "0";
} else {
document.ordination.ordina.value = "1";
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" onclick="order();return true;">Ordina</button>
The alert(document.ordination.ordina.value) give always 1.
Some can help me?
Check it now..
<script> function order() {
if (document.ordination.ordina.value == "1") {
alert(document.ordination.ordina.value); // this one shows 1
document.ordination.ordina.value = "0";
} else {
alert(document.ordination.ordina.value); // this one shows 0
document.ordination.ordina.value = "1";
} }</script>
<?php echo $_POST['ordina'];?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="<?php if(isset($_POST['ordina'])){echo $_POST['ordina'];}else{ echo '1';}?>" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
after you submit this form the <?php echo $_POST['ordina'];?> is 0...
and set to button value as 0 and again submit the value can be changed to 1.
and its consequently changed 0 to 1 to 0 to 1 but you have first time load this page means the alert shows 1 only..
This always return 1, because your button is a "submit" button, so the body is reloaded each time you click on the button.
<script>
function order() {
if (document.ordination.ordina.value == "1") {
document.ordination.ordina.value = "0";
} else {
document.ordination.ordina.value = "1";
}
alert(document.ordination.ordina.value);
}
</script>
<form id="ordination" name="ordination" method="POST" action="">
<button type="button" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
</form>
I change type "submit" for "button", then it works.
<script> function order() {
if ($("#ordina").val() == "1") {
$("#ordina").val(0);
} else {
$("#ordina").val(1);
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
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>