I have check box which will be dynamically added along with the textfields.
<input type="checkbox" name="namechkbox[]">
<input type="text" name="nametxt[]">
I will need to map the checkbox value with the text field. I found from other questions that after adding the hidden element over the input element checkbox.
<input type="hidden" name=namechkbox[]" value=0>
Since it's dynamic, it will add the index because of name[] in the name.
What is the way to handle checkbox with value submit for the dynamic elements?
if user checks any checkbox change the values off to on and on to off viceversa.
for ex:
<input type="checkbox" name="namechkbox[0]" value="on" />
<input type="hidden" name="nametxt[0]" value="This my data string 1" />
<input type="checkbox" name="namechkbox[1]" value="off" />
<input type="hidden" name="nametxt[1]" value="This my data string 2" />
now you submits the from then check loop through array to check
if namechkbox[0] value is on
then take the value of nametxt[0]
Try this:
<?php
if(isset($_POST)){
$invite = $_POST;
echo '<pre>';
print_r($invite);
}
?>
<form method="post" action="">
<?php for($i=0;$i<4;$i++)
{
?>
<input value="chkbob1" name="invite['id<?php echo $i;?>']" type="checkbox">
<input value="" name="name['id<?php echo $i;?>']" type="text">
<?php
}
?> <input type="submit">
</form>
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
}
}
I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)
I need some help saving the value of the checkbox that user has selected.
I have two checkboxes in the form:
<form action="process.php" method="POST">
Option One: <input type="checkbox" name="check1" value="1"/>
Option Two: <input type="checkbox" name="check2" value="2"/>
<br>
<input type="submit" value="Submit" />
</form>
<?php
if(isset($_POST['check1'])){
//execute function A
}
elseif(isset($_POST['check2'])){
//execute function B
}
?>
Now if the user ticks "Option One" and clicks submit, the checkbox remains checked and the php code executes some function otherwise if the user ticks "Option two" another function is executed and the checkbox 2 remains checked.
Please someone help me sort this out.
Thankyou!
If you want to keep them checked after submitting:
Option One: <input type="checkbox" name="check1" value="1" <?php if(isset($_POST['check1'])) echo 'checked="checked"'; ?> />
Option Two: <input type="checkbox" name="check2" value="2" <?php if(isset($_POST['check2'])) echo 'checked="checked"'; ?> />
I need the value of submit button when form is posted in existing text field. and below is the code.
I trying out but it is creating a new text field.
<?php
$var1 = $_POST['hello'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo "<input type='text' name='text1' value='$var1'>";
?>
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>
It would create a new text field, because that's what you're echo is doing.
Just change it do:
<?php
$submitbutton = $_POST['hello'];
$textbox = $_POST['text1'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo $submitbutton;
echo $textbox;
?>
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>
Edit in reply to comment
Use this:
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="<?php (isset($_POST['hello'])?$_POST['hello']:null); ?>">
</form>
How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>