I have a form having multiple checkbox like this.
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
On Form submit I need all checkbox id on my controller whether it is checked or not.
You do not need to post ids here:
here
1) id contains room_list <?php echo $val;?>:
2) name contains room_list[]
3) Value contains <?php echo $val; ?>
Now you id is consist of room_list and $val.
$val you are already getting in post as a value
So in $POST data you can dynamically generate id of all checkbox
ex.
foreach($_POST['room_list'] as $rooms) {
$id = 'room_list'+$rooms; // here $rooms contains the `$val` values
}
Create hidden input fields to get all checkbox values:
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="hidden" name="room_list_hidden[]" value="<?php echo $val; ?>" />
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
So in $_POST['room_list_hidden'] you will get all ids
If check-box is checked then you get that check-box and its value on server and you area using array of check-box so you get in array format.
to get all the ids on server side.
make a query which gives you all the ids on the server directly.
make hidden field for all ids so at the time of form submit you will
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
" />
"id="room_list">
}
Related
<input type="checkbox"name="travel[]" value="bus"/>
<input type="checkbox"name="travel[]" value="train"/>
<input type="checkbox"name="travel[]" value="plane"/>
foreach($_POST['travel']as $selected)
var select[]=$selected;
If the user selects all the three checkboxes, I have to store them in an array and send it to mail as I dont have a data base. So how should I store them in an array?
foreach($_POST['travel']as $selected)
var select[]=$selected;
The above code is returning only last selected check box
And how should I pass it and display it on the mail?
Instead of
foreach($_POST['travel']as $selected)
var select[]=$selected;
update it to
$select = array();
foreach($_POST['travel'] as $key => $selected){
$select[$key]=$selected;
}
Instead of using foreach simply use $select = implode(',',$_POST['travel']);
Because every time you are defining a new array, by var select[]=$selected;.
Change it it $select[]=$selected;
please give different names as below.
after posting data using foreach loop you will get all selected options
<input type="checkbox" name="travel1[]" value="bus"/>
<input type="checkbox" name="travel2[]" value="train"/>
<input type="checkbox" name="travel3[]" value="plane"/>
I've tried to get those checkbox that are unchecked. At this moment i'm just getting those that are checked. Here is my code. That value will be inserted on a table which i don't want to leave it null, that's why i need to get those checkbox unchecked, so that i could insert 1 for checked or 0 for unchecked:
<?php
if (!empty($_POST['menu'])) {
# code...
foreach ($_POST['menu'] as $value) {
# code...
echo "<p>ID Checkbox checked: ".$value;
}
}
?>
One of the reasons for what i need to get both status: checked or unchecked is because i don't want to leave database fields empty.
Unchecked checkboxes don't get POSTed. If you know what fields should be there, you'll probably have to list them out manually.
The wonderful thing about checkboxes is that when you don't have one checked, and you submit a form, nothing is sent to the server. Take this checkbox for example:
<input type="checkbox" name="test"/>
If you left it unchecked and looked for $_POST['test'] it would error out, as it is not set.
So, try doing this:
if(!isset($_POST['name_of_checkbox'])){
// Fill database with some empty value.
} else {
// Do what you need to do for checked value.
}
Hope that gives you some insight!
Say if they are named the same, and you know the number of checkbox you can use a for loop:
if (!empty($_POST['menu'])) {
for ($i=0; $i < 10; $i++) {
if(isset($_POST['menu'][$i])){
echo "Checkbox checked: " . $_POST['menu'][$i];
}else{
echo "Checbox uncheck #" . $i;
}
}
}
You can put the names in an array, then iterate:
$checkboxes = array('cbx1', 'cbx2', 'cbx3');
foreach ($checkboxes as $checkbox) {
if (isset($_POST[$checkbox])) {
echo "<p>ID Checkbox checked: " . $_POST[$checkbox];
} else {
echo "Checbox uncheck :" . $checkbox;
}
}
So yeah many ways to achieve this, it depends on the situation.
Check for #Artur approach as well for client side solution.
<input type="hidden" name="checkbox[8]" value="0">
<input type="checkbox" name="checkbox[8]" value="8">
This would be one way to also get 0's posted. Important part is that the name's are the same. So if checkbox is checked, it's value would override the hidden input value.
Thank you all for your time and be so kind to answer my question. The reason for what i needed to have those checkboxes unchecked and checked is because i have a dynamic menu and so i assign menus to users. Here is the solution i found with a friend of mine at work:
Code for the checkboxes. I query all the menus available on my table Menus so that i will show the administrator all the menus availables so then he could choose which menus he will assign to the users:
´
<table>
<tr>
<td>Menus a asignar:</td>
<td>
<?php
$query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM menus");
while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
# code...
?>
<input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]" value="<?php echo $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?>
<p></p>
<?php
}
?>
</td>
</tr>
</table>
´
Then here is the code to process which checkboxes are checked or not to insert on my table (id_user is not shown but i have taken from another query which is not shown so you'll have to query yourself):
´
$res=mysql_query("select id_menu from menus");
$arid=array();
while($xd=mysql_fetch_assoc($res)){
$arid[]=$xd['id_menu'];
}
for($i=0;$i<count($arid);$i++){
$id_menu=$arid[$i];
$activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
$inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')");
}
´
This gives lists all people in the db and give a drop down for each one of them i want to make it so when i hit one submit button it enters individual values for each person.
so if you make yes for bobby no for mark and yes for dustin you can the pres submit and it will enter that for there values
$results = mysql_query("SELECT * FROM `$tabeluser` WHERE buss='$buss' ORDER BY id DESC LIMIT 1");
while($rows = mysql_fetch_array($results) )
{
fn = $_POST['firstname'];
echo $fn;
?>
<form>
<select name="check">
<option>no</option>
<option>yes</option>
</select>
<?php
<input type="submit" name="submit">
?>
<form>
<?php
}
mysql_query("INSERT INTO `$fn` (buss) VALUES ('$_POST[check]')");
First of all, you create a <form> and a submit button for each of the records you have. That is wrong, since you want to update multiple values at once.
What it should look like would be:
<form>
<?php
while($rows = mysql_fetch_array($results)) {
print '<select name="check[]"> .. </select>';
}
?>
<input type="submit" name="submit" />
</form>
Secondly, your code is formatted as if you are expecting to get $_POST[check] right after sending the code to the browser. That is not how PHP works. You need to separate the logic of having posted values, before printing the actual page contents. PHP is server side, which means that it won't get any data, unless the script is called with it from the beginning. So, that should look something like:
<?php
if (isset($_POST["check"])) {
// handle posted data.
}
else {
// show form
}
// or show form here, without an else, if you want to always show a form,
// even when you have posted values.
?>
Last but not least, you need to find a way to know which posted value belongs to each of your records. The way you have it now (<select name="check">') it will just return you one single value.
If you write it the way I intentionally did above (`) you will get all values, but still you won't be able to easily recognize which value is for each record.
Instead, you may want to do a final result of something like:
<?php
// get mysql records into an array (say $my_array)
if (isset($_POST["submit"])) {
foreach($my_array as $record) {
if (isset($_POST["select_of_id_".$record["id"])) {
// insert additional value into db
}
}
}
print '<form>';
foreach($my_array as $record) {
print '<select name="select_of_id_'.$record["id"].'">';
print '<option value="0">no</option><option value="1">yes</option>';
print '</select>';
}
print '<input type="submit" name="submit"/>';
print '</form>';
?>
Changes required in your code :-
<select name="check[]">
<option value="<?php echo $userId;?>_0">no</option>
<option value="<?php echo $userId;?>_1">yes</option>
</select>
You should make changes in you DB It help to easy maintaing your data.
Create new table where you can save multiple user check data with respective Post
for e.g post_id user_id check
101 111 0
101 112 1
How you can store data from you html
In you post array you will get check array in which you will get multiple check value like this
101_0 if no selected and 102_1 if yes selected. Now you need to explode this value.
$_POST['check'] = array(0 => `101_0`, 1 => 102_1);
Inside loop you can extract userid and respective checked value.
for e.g
$checked = explode('_','101_0');
$userId = $checked[0];
$check = $checked[1];
I'm trying to add submit buttons and hidden input fields so that when a user clicks a submit button I can identify the item they selected. Although I can't figure out how to access the hidden values. I have this code (the value in input type="hidden" are the item's id's). How do I access the values?
foreach($dbh->query("SELECT * FROM beer WHERE country_id = $countryID") as $beer) {
echo "<a href='BeerSummary.php?beerID=$beer[id]'>$beer[2]</a> <br/>";
echo "ABV $beer[3]% - $beer[4] ml - Case Size $beer[5] - Price £$beer[6]";
echo '<input type="submit" value="Add to Cart"> <br/>';
echo '<input type="hidden" name="beer_id[]" value="'.$beer[0].'">';
echo "<br/>";
}
if(isset($_POST["beer_id"])) {
//
}
You have them defined as an input array, so the way to access it/them would be:
foreach($_POST['beer_id'] as $value)
{
echo $value;
}
for each of hidden fields, take id. id = hidden + $beer[i]
then u can easily access the hidden fields with document.getElementById("hidden" + $beer[i])
this will work in javascript.
if u want to so the same in php, #Ben's answer should work.
how can i process all radio buttons from the page?
<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />
<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />
this buttons will be added dynamically so i will not know the radio buttons name(and also the number of the buttons). Is there a way to get all radio values, with a for loop or something like this? thanks
Use a foreach loop
<?php
foreach ( $_POST as $key => $val )
echo "$key -> $val\n";
?>
$key will be the name of the selected option and $val, well, the value.
Since the browser will just change all your input to HTTP-formatted form data, you won't be able to tell what data is from a radio button versus a text box or other input.
If the naming convention is the same as your example, just loop until you don't find a value:
<?
for ($idx = 1; $idx <= 1000; $idx++) {
if (isset($_REQUEST["radio_$idx"])) {
// handle value
}
}
?>
EDIT Alternatively, if your form is generated dynamically, you could write the number of radio buttons it created as a hidden field in the form.
If you are able to alter the form that is being generated, you could write a hidden input that provided a list of all the radio buttons that you want to look for. As you are writing the radio buttons, just make a semi-colon-separated list of all the names that you make. When you are done, write that to a hidden input. Something like this:
On the source form:
<input type="hidden" name="radio_button_list" value="some_value;other_value" />
Then in your handler:
<?
$list = explode(';', $_REQUEST['radio_button_list']);
foreach ($list as $name) {
$value = $_REQUEST[$name];
// handle name & value
}
?>
jheddings' example says it all. However, you will never get the names / values of all buttons - just the selected one from each group. If you need literally all values, you will have to use Javascript.