<form action="#" method="post">
<input type="checkbox" name="box[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="box[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="box[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
I am using php code to fetch checked box values,
how can I save each checked values in different variable out of loop?
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['box'])){
foreach($_POST['box'] as $selected){
echo $selected."</br>";
}
}
}
?>
Related
I have several checkboxes that contain names (ids as referrence to database) - see code below. How can I select all checked values and add them to database (via MySQL) each row for each checked?
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
After clicking "send" with requiered checked names, the result in database should look like this (I selected John and Mike):
Id
1
3
(only selected ones)
How can I achieve that?
You need to wrap your inputs in a <form> element and give this form a method of post:
<form method="post">
<input type="checkbox" value="1" name="names[]" />John
<input type="checkbox" value="2" name="names[]" />Peter
<input type="checkbox" value="3" name="names[]" />Mike
<input type="checkbox" value="4" name="names[]" />Kattie
<input type="submit" value="Send" name="send" />
</form>
This will allow you to post submitted data from your form inputs to your PHP.
Note: If your HTML is in a different file (ie not in the same file as your form) you can add the action attribute to your form (eg: action="fileWithPHP.php")
Now you can access all checked checkboxes in your PHP using $_POST['names']. This will allow you to get your array of checked values. You can then use a foreach loop to loop through every value in your array:
<?php
if(isset($_POST['names'])) {
$names = $_POST['names'];
foreach($names as $name) {
echo "Add " . $name . " to db here<br />"; // add $name to db
}
}
?>
You can wrap the inputs around a <form> and send it to php and retrieve using $_GET or $_POST and update the database.
I have used POST method here.
HTML:
<form action="test.php" method="post">
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
</form>
PHP:
if(!empty($_POST['names'])) {
foreach($_POST['names'] as $check) {
echo $check; //instead of echo you can insert it to the database
}
}
How to get by POST multiple checked checkbox IDs and VALUEs at the same time? Code bellow shows html to send data.
<form action="" method="post">
first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" />
second<input type="checkbox" name="first[]" id="second'<?php echo $data2; ?>'" value="second" />
third<input type="checkbox" name="first[]" id="third'<?php echo $data3; ?>'" value="third" />
<input type="submit" value="submit">
</form>
After send by post I get values, but ID is missing.
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value.'<br/>';
}
How can I send ID and VALUE and get them by post without explode them? For sure i can split them after, but there should be another way.
You could do something like:
<form action="" method="post">
first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
<input type="hidden" name="first[0][id]" value="first[]">
second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
<input type="hidden" name="first[1][id]" value="second[]">
third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
<input type="hidden" name="first[2][id]" value="third[]">
<input type="submit" value="submit">
</form>
And on the back-end:
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value['value'].'<br/>';
echo 'ID: '.$value['id'].'<br/>';
}
If you want to get an id value from an input, used the id as key in your name array
<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>
or
<input type="checkbox" name="first[1]" .../>
<input type="checkbox" name="first[2]" .../>
<input type="checkbox" name="first[3]" .../>
then when you loop over your posted inputs, include the key in the key=>value
foreach($_POST['first'] as $id => $value){
echo 'ID: '.$id.' => VALUE: '.$value.'<br/>';
}
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 some buttons in a first_page.php, and some checkboxes in a second_page.php.
I need to select the corresponding checkbox with a query string to get this:
When "first value button" is pressed --> "second_page.php" with "my first value" checkbox already selected.
first_page.php :
<form action="second_page.php">
<input class="btn" type="submit" value="first value button">
<input class="btn" type="submit" value="second value button">
<input class="btn" type="submit" value="third value button">
</form>
second_page.php :
<form name="name" method="post" action="#">
<input type="checkbox" name="mybox[]" value="my first value"/>
<span>my first box</span><br />
<input type="checkbox" name="mybox[]" value="my second value"/>
<span>my second box</span><br />
<input type="checkbox" name="mybox[]" value="my third value"/>
<span>my third box</span><br />
</form>
You have to specify a name for each input to connect it to $_POST then in second_page.php you have to fetch the form value.
In first_page.php:
<form action="second_page.php" method="post">
<input name="first_value_btn" class="btn" type="submit" value="first value button">
<input name="second_value_btn" class="btn" type="submit" value="second value button">
<input name="third_value_btn" class="btn" type="submit" value="third value button">
</form>
In second_page.php :
<input type="checkbox" name="prodotti[]" value="my first value" <?php echo ( isset($_POST['first_value_btn']) ? 'checked="checked"' : '');?> />
Read more
I've used POST as method in my example above, you could use GET instead and then replacing $_POST with $_GET instead.
I would give the Submit buttons names and then do a check on them on the next page:
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit1'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit2'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit3'])) { echo 'checked'; ?> />
I'm a beginner.I have displayed student id and student name of 10 students on a table. Against each student id, there should be a checkbox(dynamic). When I click the ADD button, all the checked students details (id,name) must be inserted into another database table. What should i do?
Use checkbox name as an array,
example :
<form method="post" action="" id="frm_id">
<input type="checkbox" name="chkid[]" value="10,Anu" />Anu
<input type="checkbox" name="chkid[]" value="11,Raj" />Raj
<input type="checkbox" name="chkid[]" value="12,Ram" />Ram
<input type="checkbox" name="chkid[]" value="13,xxx" />xxx
<input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
<input type="checkbox" name="chkid[]" value="15,zzz" />zzz
<input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
<input type="submit" value="Insert" name="sub"/>
</form>
<?php
if(isset($_POST['sub']))
{
$id=$_POST['chkid'];
for($i=0;$i<count($id);$i++)
{
$exp=explode(',',$id[$i]);//Explode id and name
echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
}
}
?>
<form method="post" action="pageurl">
<input type="checkbox" name="studentid[]" value="1,Student1" />Student1
<input type="checkbox" name="studentid[]" value="2,Student2" />Student2
<input type="checkbox" name="studentid[]" value="3,Student3" />Student3
<input type="checkbox" name="studentid[]" value="4,Student4" />Student4
<input type="submit" />
</form>
<?php
$id=$_POST['studentid'];
foreach($id as $student)
{
$extract = explode(',',$student);
$query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')";
}
?>
try using the array of checkbox element like this:
<input type="checkbox" name="months[]" value="feb">February<br>
<input type="checkbox" name="months[]" value="mar">March<br>
<input type="checkbox" name="months[]" value="apr">April<br>