How to insert data without knowing no off columns in php - php

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

Related

How to separate checkbox variable results outputted in a while loop in PHP?

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>";

HTML/PHP Checkbox Foreach Loop

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>";

Checkbox Values stored in DB and want to display

Please see this link
http://thedesigningworld.com/bea
Here's a Small form contains 8-9 fields + a group of checkboxes
I want to save all details in DB + want to display in a table in proper manner, but it not works properly
Here's the code which i used
for($i=0;$i<count($_POST[wert1]);$i++)
{
if($_POST[wert1][$i]!= "")
{
$check1[] =$_POST['wert1'][$i]; } }
$new1=implode(',', $check1);
$result = "INSERT into table1(check1) values($new1)";
$result = mysqli_query($con, $result);
So i've one doubt that for each checkbox row, should i need to define same array name or different like here i used array name as wert1[] for first row
Checkbox values are not transmitted if the box is not checked.
If you have influence, you could put a hidden input field of the same name before the checkbox and the value "0", like:
<input type="hidden" name="checkbox_name" value="0" />
<input type="checkbox" name="checkbox_name" value="1">Some Text</input>
In you example site, you're using array notation, which is basically a good thing. However, you have not given an index so you might not recognize missing elements.

PHP Array Variable Being Passed Incorrectly

I have a query which is doing something strange. This checkbox is in a while loop and it correctly lists out everything it needs to:
<input type='checkbox' name='rep[]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[]' value='$billableTotal'>
When the form is submitted the values are inserted into the database using:
foreach ($_POST['rep'] as $index => $id) {
$sql2="INSERT into b_sale_basket (QUANTITY,LID,NAME)
VALUES
('".$_POST['billablehours'][$index]."','s1','".$_POST['rep'][$index]."')";
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}
}
It inserts everything as it should do except billablehours. I have outputted the value of $billableTotal on each checkbox on the form page and the value is correct. For example it might equal 25 but when the button is pressed it inputs 37.5 which is another value of a checkbox.
Strange. Can anybody identify an issue?
Problem is that if checkbox is not checked it's not passed to server, so if u have 3 checkboxes and select first and third, on server you got them with 0, 1 index.
in your case just use specific identifiers:
<input type='checkbox' name='rep[$id]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[$id]' value='$billableTotal'>
Assuming you know this $id is connected corresponding $invoiceID and $billableTotal,
it may be entry id from database.
And when using: $_POST['billablehours'][$index] if checkbox is not checked it gots empty...
Note: my exmplanation is just to understand the point, not 100% working example maybe, because I've got no full code what you do.
Just remember: It's the key when working with checkboxes to correctly reference the data passed from server to client and vs.
Only checked checkboxes are submitted. So if you only check one, then rep array will only have one element 0 but thebillablehours array will have as many as are defined on the page.
$i = 0;
//loop
echo "<input type='checkbox' name='rep[$i]' value='$invoiceID'>Reference Number: $invoiceID";
echo "<input type='hidden' name='billablehours[$i]' value='$billableTotal'>";
$i++;
//end loop
So the rep and billablehours indexes will match regardless of how many checkboxes were checked.
You are wide open to SQL injection attacks with your script. You either need to use prepared statements (preferable) or run your $_POST values through mysqli_real_escape_string;
Leaving my incorrect answer below, but not deleting this answer because I think the SQL injection vulnerability needs to be addressed.
That being said, your issue is that you aren't echoing the value:
value='<?php echo $billableTotal; ?>'>
Make these changes:
name="billablehours[$invoiceID]"
$_POST['billablehours'][$_POST['rep'][$index]]

How can I retrieve checkbox array values when the checkbox name is dynamically created?

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]";

Categories