I'm creating a control panel and having check boxes like the following
The value of $myboxid is the checkbox id, example: cb1 and the name is just the value eg Name of place: London. It grabs this information from my database
<input id='".$myboxid."' name='cplace[]' checked type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>
What I'm trying to do is check which box is selected out of the multi box selection. I can get which boxes are selected and for that to output the value, my problem is I also need it to tell me which boxes are not selected.
My Form method is POST, my php back end is the following
$lname=$_POST['cplace'];
if(isset($_POST['cplace'])) {
foreach($lname as $place){
echo $place." CHECKED <BR>";
}
}
I'm trying to get this to output the checkbed boxes and the ones which are not selected.
Thanks for the help!
Well you can check the value of the checkbox in the POST array.
Simply print all the checkbox from the db and then check the value
In one line you can do like this
Inside your db loop
$isChecked=(in_array($_POST['cplace'], $box)) ? "checked" : "";
echo "<input id='".$myboxid."' name='cplace[]' ".$isChecked." type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>";
Related
I'm trying to write an attendance system that, when a user is present at a class, the staff will check a box and the program will then add one to the present count of the relevant customers. The problem is to output the register, taken from phpMyAdmin, it uses a while loop so all the checkboxes have the same variable name. This is the code I have so far...
echo "<form 'action=badminreg.inc.php' method='post'>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row['bookingID']."</td><td>".$row['firstName']."</td><td>".$row['surname']."</td><td><input type='checkbox' name='present' value='present'</td><td><input type='checkbox' name='incident' value='incident'</td></tr>";
}
echo "<input type='submit' name='reg-submit'>";
echo "</form>";
isset($_POST['reg-submit']);
$pres = $_POST['present'];
I need to separate the check box inputs so that the program will be able to mark individual users differently to others. I'm sort of new to PHP as this is for my A-level coursework so is there a way to get around this? Any help would be great. Thanks
You can define the name for the checkbox elements as array and pass the bookingID as value.
Example (untested):
$output = "<tr><td>{$row['bookingID']}</td><td>{$row['firstName']}</td><td>{$row['surname']}</td><td><input type='checkbox' name='present[]' value='{$row['bookingID']}'</td><td><input type='checkbox' name='incident[]' value='{$row['bookingID']}'</td></tr>";
The variables $_POST['present'] and $_POST['incident'] contains then an array with the IDs of the selected check boxes.
If you change it to name='present[]' then when you submit the data again, you'll get an array of values inside $_POST["present"] instead of a single one. (You'll get one item in the array for each checkbox with that name which was actually checked - a quirk of HTML forms is that if the checkbox wasn't checked, its value is not submitted at all).
You'll also want to change the value of the checkbox to be the ID of the customer (or booking, maybe), so you can identify which box was checked.
Same for the "incident" checkbox as well, of course.
So you're aiming for something like this, I think:
echo "<tr>
<td>".$row['bookingID']."</td>
<td>".$row['firstName']."</td>
<td>".$row['surname']."</td>
<td><input type='checkbox' name='present[]' value='".$row['bookingID']."'</td>
<td><input type='checkbox' name='incident[]' value='".$row['bookingID']."'</td>
</tr>";
I have a variable number of fields in a table,where the number of column headings are directly fetched from database,so the number of heading increases as the data from database increase(eg-demo,demo1,demo2,..).For each column there is a selectbox,here i have given name for check box as txtcheck".$i."[] where $i is set as an incriminating value.
<input type='checkbox' name='txtcheck".$i."[]' value='1'>
My doubt is how can i fetch the checkbox post value when form submission.I dont know whether this is correct way nor find a way to efficiently do this. Please advise.
The name of your checkboxes shouldn't be incremented, but only be txtcheck[]. In this case the selected boxes will be transfered as array.
<input type='checkbox' name='txtcheck[]' value='1' />
In your PHP code you can access this array with $_POST['txtcheck'].
<?php
$checkedBoxes = $_POST['txtcheck'];
foreach ($checkedBoxes as $checkdBox) {
// Here you can handle each box that was checked in your form, e.g. echo it's value
echo $checkedBox . ' ';
}
?>
So if you checked e.g. boxes with values 1, 5 and 10 your output will be 1 5 10
dropmenu is <select name="dropbox"> with 3 options - admin, activate, delete. The below snippet of code shows if the activate <option> is selected and submitted by the submit button then echo etc. I have a <select name="dropbox"> on every row for each user. My code only works if i change the last drop box.
if(isset($_POST['submit']))
{
if(isset($_POST['dropmenu']) && $_POST['dropmenu'] == 'activate')
{
echo 'is activated';
}
else{
echo 'fail';
}
}
Is there a way i can use foreach drop box with a value selected?
One of the possible ways is to connect the name of the select element with user id like
<select name="dropbox_[userId]">
In this case you simply know how to build an input name for checking it's value in $_POST table.
If you don't interate by users on submit code then you can use a regular expression to get an inforamtion abut user connected with element.
Please help.
I am trying to dynamically name my checkboxes using php. I am using POST.
The problem I am running in to is $element is not working. The results of $_POST
does not show any checkboxes.
Thanks in advance for the help.
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '{$element}' />";
}
But something like
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '$element}' />";
}
works just fine. Notice the missing { near $element}. This code would show which checkboxes are
on!! The printed array would have an extra "}"
Array
(
[Tomato_and_Cheese_small] => on
[Tomato_and_Cheese_small}] => 1
[Tomato_and_Cheese_large] => on
[Tomato_and_Cheese_large}] => 1
)
ps. there are other inputs like text that get posted to $_POST just fine.
The print_r($cart_array) works fine too.
The browser sends the value of radio buttons only when they are checked.
Also, each radio button must have the same name (if you want to user to be able to check only one of them). Only the value changes:
print '<input type=checkbox checked value="'.htmlspecialchars($element).'" name=checked_items />';
POST this and check the value of $_POST['checked_items']
If a checkbox is not checked it will not be present in the post parameters.
What happened was that he had checkboxes with the name $element and text fields with the same name too after that. So the checkbox name $element in $_POST got overrided by the text field $element
I am dynamically creating forms based on values in a database. Each form element corresponds to a specific database entry, which includes the name, label, type, and (depending upon the type) possible values to be displayed with the form element.
The name value for each form element is set to the identity column value of the corresponding database entry.
I'm running into a problem when I am working with checkboxes, though. I'm trying to retrieve the array of selected values, but I am unable to retrieve more than one. I believe that this is because I am not properly setting the checkbox names to an array, but I am not certain.
Here is where I am generating the checkbox tags:
$answers = explode(',',$answerKey);
for($i=0; $i < count($answers); $i++) {
$questionTag .= "<INPUT TYPE='checkbox' name='$id' value='$answers[$i]' />$answers[$i]";
}
Yet when I post back my results, the results of $_POST["$id"] only returns the last value in the checkbox list.
Any suggestions would be appreciated!
$questionTag .= "<INPUT TYPE='checkbox' name='".$id."[]' value='$answers[$i]' />$answers[$i]";