Multiple checkbox values in php - php

I have the following code
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value"type1" /><br>
Type 2:<input type="checkbox" name="product[]" value"type2" /><br>
Type 3:<input type="checkbox" name="product[]" value"type3" /><br>
<input type="submit" value="Submit">
</form>
foreach($_POST["product"] as $value)
{
echo $value ;
}
it should return the values user have selected. But it gives only 'on' as output.

Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
An example code:
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="checkbox" name="product[]" value="type 1">
<input type="checkbox" name="product[]" value="type 2">
<input type="checkbox" name="product[]" value="type 3">
<input type="checkbox" name="product[]" value="type 4">
<input type="checkbox" name="product[]" value="type 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['product'])) {
foreach($_POST['product'] as $check)
{
echo $check;
}
}
?>

your value is wrong,you forgot =
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>

It must be value="type1" not value"type1".
Try this
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
foreach($_POST["product"] as $value)
{
echo $value ;
}
?>

try this
<form id="myForm" action="" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST)) {
foreach($_POST["product"] as $value)
{
echo $value ;
}
}

First you forgot the = after value
Remodifying your script becomes this
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
$check = $_POST["product"]
foreach($check as $value)
{
echo $value ;
}

Related

how to add multiple entries having the same text input name and radio buttons (html,php)

<form action="confirm.php" method="post" name="">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
<br>
<button type="submit" class="">Submit</button>
</form>
having problem with the radio buttons.
And on the confirm page I have used foreach loop. How do i also get the values for "f_status" ?
See first of all its an interesting question but unfortunately, the fact is HTML can't understand the field without different names if they are in same form.
so the only way to achieve your goal is to put all of them in three different form tags and then u can name all of the same i.e. f_hobby[]
Also, you need to add a single button to submit all three of them. To achieve this u can use onsubmit() or onclick() function.
<form action="confirm.php" method="post" name="" id="form1">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form2">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form3">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<button type="submit" class="" onclick="submitForms()">Submit</button>
<script>
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
document.getElementById("form3").submit();
alert("gajab");
}
</script>
I have given the forms an id to submit it using a single button, u can also use class instead. I am sure this will solve your problem.

Display a checked checkbox in a form to form POST submission

I have this wizard that needs to display the previous posted value
index.html
<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
in the posted.php i have a similar form only this time i know the value from $_POST
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.
How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.
This would be a way to go:
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
<input type="submit" value="Submit" />
</form>

email signup multiple lists

I have the following code, I want the user to be able to sign up to multiple lists at the same time. It is currently only signing up emails to one list randomly, even if I check all the lists. Is it possible to do? Maybe some sort of php echo?
<form action="" method="post">
<input name="accName" type="hidden" value="companyname">
<input name="listName" type="hidden" value="">
<input name="fullEmailValidationInd" type="hidden" value="Y">
<input name="doubleOptin" type="hidden" value="false">
<input name="successUrl" type="hidden" value="">
<input name="errorUrl" type="hidden" value="">
Email Address <input class="border" name="email" size='50' type="text" value="">
First Name <input class="border" name="First_Name" size='50' type="text" value="">
Last Name <input class="border" name="Last_Name" size='50' type="text" value="">
<label><input name="listName" type="checkbox" value="list1"></label>
<label><input name="listName" type="checkbox" value="list2"></label>
<label><input name="listName" type="checkbox" value="list3"></label>
<label><input name="listName" type="checkbox" value="list4"></label>
<label><input name="listName" type="checkbox" value="list5"></label>
<input type="submit" value="OK">
</form>
Use array for the checkboxes:
<input type="checkbox" NAME="listName[]" VALUE="list1" />
Or use different names...
You will need to grab this using, php side, a loop:
foreach ($_POST['listName'] as $selected)
Just notice that if none are selected this will fail, thus check if the array exists before:
if (isset($_POST['listName'])
{
foreach ($_POST['listName'] as selected)
{
DO YOUR STUFF
}
}

How to pass value on next page from checkbox, which i checked?

I have multiple checkbox and want to pass their value on next page when i checked them.
My code is
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="city" id="city" value="Platinum" />
<input type="checkbox" name="city" id="city" value="Silver" />
<input type="submit" value="Submit" />
</form>
By defining the name attribute as an array name="city[]"
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
print_r($_POST);
/*
Array
(
[city] => Array
(
[0] => Gold
[1] => Platinum
[2] => Silver
)
)
*/
}
?>
<form method="POST">
<input type="checkbox" name="city[]" value="Gold" />
<input type="checkbox" name="city[]" value="Platinum" />
<input type="checkbox" name="city[]" value="Silver" />
<input type="submit" value="Submit" />
</form>
Try this.
<?php
include("config.php");
if($_POST) {
$city = $_POST['card'];
header("location:target_page.php?card=".$city);
}
?>
<form method="POST">
<input type="checkbox" name="card[]" id="card" value="Gold" />
<input type="checkbox" name="card[]" id="card" value="Platinum" />
<input type="checkbox" name="card[]" id="card" value="Silver" />
<input type="submit" value="Submit" />
</form>
This is not working when both fields have the same name.
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="cities[]" id="city" value="Platinum" />
<input type="checkbox" name="cities[]" id="city2" value="Silver" />
<input type="submit" value="Submit" />
</form>
When you write the fieldname like cities[] then you get an array in your PHP-Script.
forearch($_POST['cities'] as $city) {
var_dump($city);
}
And an ID should be unique.

Return hidden field with $_POST out of multiple if file is uploaded

<form class="formp" action="" method="post" enctype="multipart/form-data" id="frm" name="wjd">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"/>
<p>Gerecht:</p><p>Prijs:</p><p>Plaatje:</p><p>Upload:</p>
<?php
$resultmenus = mysqli_query($dbc,"SELECT * FROM prijslijst WHERE menunaam='menu\'s'");
while($rowmenus = mysqli_fetch_array($resultmenus,MYSQLI_ASSOC)) {
echo "<input type=\"hidden\" name=\"id\" value=\"{$rowmenus['id']}\"/>";
echo "<input name=\"gerecht[]\" type=\"text\" size=\"30\" maxlength=\"30\" value=\"{$rowmenus['gerecht']}\"/>";
echo "<input name=\"prijs[]\" type=\"text\" size=\"5\" maxlength=\"5\" value=\"{$rowmenus['prijs']}\"/>";
echo "<img src=\"../{$rowmenus['plaatje']}\" /><input size=\"1\" type=\"file\" name=\"file[]\" id=\"file\"/>";
}
<input type="submit" name="submit" value="verzend" />
</form>
This will output a form with id's 1 to 5 from the db echo'd in the hidden input type field.
My question is: when i upload only 1 file in row 3 with id3 and the rest are empty, is it possible to retrieve only that id?
Because when i try to retrieve it with $_POST['id']; I get the last value which is 5.The is the output to the browser.
<form id="frm" class="formp" name="wjd" enctype="multipart/form-data" method="post" action="">
<input type="hidden" value="2000000" name="MAX_FILE_SIZE">
<p>Gerecht:</p><p>Prijs:</p><p>Plaatje:</p><p>Upload:</p>
<input type="hidden" value="1" name="id">
<input type="text" value="Menu 1" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="3.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu3.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="2" name="id">
<input type="text" value="Menu 2" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="6.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu2.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="3" name="id">
<input type="text" value="Menu 3" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="6.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu1.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="4" name="id">
<input type="text" value="Menu 4" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="7.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu3.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="5" name="id">
<input type="text" value="Menu 5" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="4.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu2.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="submit" value="verzend" name="submit">
</form>
This is the handling script.
if(isset($_POST['submit'])) {
if((array_search('', $_POST["gerecht"])!==false) or (array_search('', $_POST["prijs"])!==false)) { //1 or more field empty
echo "<span class=\"error\">Please fill in all fields</span>";
exit();
}else{//field filled in
foreach ($_FILES['file']['name'] as $i => $name) {
if ($_FILES['file']['error'][$i] == 4) { // no upload continue
continue;
}
if ($_FILES['file']['error'][$i] == 0) {
echo $_POST['id'];
}//end else error 0
}//end foreach
}//end filled in forms
}else{ //submit button clicked
}
if you re-use a form element name (and it's not an array) the last value will overwrite previous values. See how you have
<input type="hidden" value="5" name="id">
try instead
<input type="hidden" value="5" name="id[]">
and iterate through them.
You have
<input type="hidden" value="1" name="id">
<input type="hidden" value="2" name="id">
etc.
You're going to only get number 5 because it is the last one, and is overwriting the one before it.
Same with the input texts, the name attribute should be different.
Do a var_dump($_POST); to see all that is being POST'ed.

Categories