I know this should be an easy one, but I'm failing to make it work.
I have the following form:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
</form>
After submit I want to display the form again and to check the checkboxes that the user has selected before submission. My language of choice is PHP.
Thanks.
Something like this should work:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" <?=(in_array("2", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" <?=(in_array("5", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" <?=(in_array("10", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" <?=(in_array("1", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" <?=(in_array("66", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade PR-11 <br />
</form>
If I select the first three checkboxes, then $_POST will be:
Array ( [blades] => Array ( [0] => 2 [1] => 5 [2] => 10 ) )
If I select the first, third and fifth checkboxes:
Array ( [blades] => Array ( [0] => 2 [1] => 10 [2] => 66 ) )
This was done with:
print_r($_POST);
As you can see, $_POST['blades'] is an array with each selected value.
My full testing code was:
<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
<input type="submit">
</form>
<?php print_r($_POST); ?>
<?php $blades = $_POST["blades"]?>
<form action="" method="post">
<input type="checkbox" name="blades[]" <?php if($blades[0]==2):?>checked="checked"<?php endif?> value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" <?php if($blades[1]==5):?>checked="checked"<?php endif?> value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" <?php if($blades[2]==10):?>checked="checked"<?php endif?> value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" <?php if($blades[3]==1):?>checked="checked"<?php endif?> value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" <?php if($blades[4]==66):?>checked="checked"<?php endif?> value="66" />Blade PR-11 <br />
</form>
Related
Hi and thanks for reading my question. I am using a simple form to get some input :
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries" value="USA" /> USA<br />
<input type="checkbox" name="countries" value="Canada" /> Canada<br />
<input type="checkbox" name="countries" value="Japan" /> Japan<br />
<input type="checkbox" name="countries" value="China" /> China<br />
<input type="checkbox" name="countries" value="France" /> France<br />
<input type="submit" value="Order">
</form>
I want to make sure order.php is geting all of the choices selected, so order.php only contains the following code :
<pre>
<?php var_dump($_POST);?>
</pre>
Unfortunately, it is only outputting whatevre is the bottom-most checkbox that is checked.
The output is like this :
array(1) {
["countries"]=>
string(6) "Canada"
}
If i try the following code for output :
<?php
foreach($_POST as $key=>$post_data){
echo "You posted:" . $key . " = " . $post_data . "<br>";
}
?>
I get this output :
You posted:countries = Canada
Can anyone tell me where i am going wrong and how i can retrieve all of the data, for every box that is ticked ?
Thank you.
You gave the same name to your checkboxes, and PHP will overwrite previously parsed name submissions with the current value. You need to use the array-notation hack:
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
^^
which then makes $_POST['countries'] an array of all the values submitted.
echo "You posted: " . implode(',', $_POST['countries']);
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Order">
</form>
Change it to above, this will store all your checkboxes results for you!
I want to make a timetable with, Codeigniter
but I have a problem with looping.
My view
Class
`<input type="checkbox" name="class[]" value="1A" id="1a" /> 1A
<input type="checkbox" name="class[]" value="1B" id="1b" /> 1B
<input type="checkbox" name="class[]" value="1C" id="1c" /> 1C `
Hours
<input type="checkbox" name="hours[]" value="1" id="h1" /> hours 1
<input type="checkbox" name="hours[]" value="2" id="h2" /> hours 2
<input type="checkbox" name="hours[]" value="3" id="h3" /> hours 3
<input type="checkbox" name="hours[]" value="4" id="h4" /> hours 4
<input type="checkbox" name="hours[]" value="5" id="h5" /> hours 5
<input type="checkbox" name="hours[]" value="6" id="h6" /> hours 6
Day
`<input type="checkbox" name="day[]" value="1" id="Monday" /> Monday
<input type="checkbox" name="day[]" value="2" id="Tuesday" /> Tuesday
<input type="checkbox" name="day[]" value="3" id="Wednesday" /> Wednesday
<input type="checkbox" name="day[]" value="4" id="Thursday" /> Thursday
<input type="checkbox" name="day[]" value="5" id="Friday" /> Friday
<input type="checkbox" name="day[]" value="6" id="Saturday" /> Saturday `
room
` A
<input type="checkbox" name="room[]" value="B" id="b" /> B
<input type="checkbox" name="room[]" value="C" id="c" /> C
<input type="checkbox" name="room[]" value="C" id="d" /> D `
my model
function add(){
$day= $this->input->post('day');
$hours= $this->input->post('hours');
$class= $this->input->post('class');
$room= $this->input->post('room');
foreach ($hoursas $hr){
$data = array('day'=>$day,'hours'=>$hr,'class'=>$class,'room'=>$room);
$this->db->insert('t_jadual', $data);
}
}
how do I loop $class,$room and $day?
I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="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.
How would i get the value of a radio button and use that in my session so for example;
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
</div>
as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?
#Xavier: Try --
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
</div>