I have been trying to make a system for my school and some of the code is giving me a problem. i have a teachers page on which i input some details from a database and give a radiobutton with each record. when the user clicks submit it is supposed to go to a different page and put only the records into a differnt table for which the radiobutton was checked.
The code for making the radiobutton is
while($row=mysql_fetch_array($sqlquery))
{
echo "<tr> <td>$row[Name]</td><td> $row[Number]</td><td>$row[Form]</td><td> $row[Outings_left]</td><td>$row[Night_outings_left]</td> <td><input type='radio' id='$c' name=' $c' value='checked' /></td><td><input type='date' id='$sd' name='$sd' /></td><td><input type='date' id=' $ed ' name=' $ed' /></td></tr>";
$arr= Array($n=> Array(1=>'row[name]', 2=> 'row[number]') );
$c++;
$sd++;
$ed++;
}
Note- there are also two date fields getting the values in a similar manner
on the other page i am trying to check whether the radiobutton is checked in teh following manner. but this is showing that all the radoibuttons are unchecked
$cb1=$_POST['$c'];
echo $cb1;
if($cb1=='checked')
{
echo "entered";
}
EDIT: Also, does every variable start at 1? If so, $c = $sd = $ed. So you are having multiple radio inputs with the same id and name.
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
to all i have a weird problem when i am trying to update a table of my database with checkboxes it takes only one value and all the rest just ingnores them here is my php code so far
foreach ($_POST['choice'] as $id){
$price=$_POST['price'][$id];
$availability=$_POST['availability'][$id];
$result1=mysql_query("UPDATE store SET storeid='".$id."',availability='".$availability."', price='".$price."' WHERE productid='".$par1."'");
}
Yes you are right and i am deeply sorry for the lack of information.Here is my html code as well
echo "<td><input type='text' id='availability[".$row->id ."]' name='availability[".$row->id ."]' value='".$row->diathesimotita ."' size='20'/></td>";
echo "<td><input type='text' id='price[".$row->id ."]' name='price[".$row->id ."]' value='".$row->price ."' size='10'/></td>";
echo "<td><input type='checkbox' id='choice[".$row->id ."]' name='choice[".$row->id ."]' value='".$row->id ."' /></td>";
echo"</tr>";
So i am trying to take the textbox values which are in the same row with the check box but it gets only the first checked ckeckbox
Checkboxes only post a value when they are checked. You should validate them or set a value first before trying to use them in a query (for security if nothing else!). For example do this at the top of your processing code:
$checkbox_val = (!empty($_POST['choice']) ? 'Yes' : 'No'); //example only assuming non-array for 'choice'
This will set the value to 'No' if the POST value is empty or not set, guaranteeing that you always have a value. You can change the values to 0/1, true/false, etc.
Also, without seeing what is in your 3 $_POST arrays, it is impossible for us to tell you if you are getting the correct values for $price etc.
The PHP code looks fine; we need to see the HTML code, but anyway try this in SQL:
UPDATE store SET storeid='".$id."' , availability='".$availability."' , price='".$price."' WHERE productid='".$par1."'" );
Spaces really matter in SQL. If this does not work please show us the HTML code.
I have been trying to make a system for my school and some of the code is giving me a problem. i have a teachers page on which i input some details from a database and give a radiobutton with each record. when the user clicks submit it is supposed to go to a different page and put only the records into a differnt table for which the radiobutton was checked. it will put the the dates that the user has entered into a differnt table. The code for making the date input is
$c=5;
$sd=7;
$ed=11;
echo "<tr> <td>Name</td><td>Number</td><td>Form </td><td> Outings left</td><td>Night outings left</td> <td>Allowed</td><td>Start date</td><td>End Date</td></tr>";
while($row=mysql_fetch_array($sqlquery))
{
echo "<tr> <td>$row[Name]</td><td> $row[Number]</td><td>$row[Form]</td><td> $row[Outings_left]</td><td>$row[Night_outings_left]</td> <td><input type='checkbox' id='$c' name=' $c' value='checked' /></td><td><input type='date' id='$sd' name='$sd' /></td><td><input type='date' id=' $ed ' name=' $ed' /></td></tr>";
$arr= Array($n=> Array(1=>'row[name]', 2=> 'row[number]') );
$c+=5;
$sd+=7;
$ed+=11;
}
i am trying to read the entered dates in the following way but is not working properly. i just doesnt enter anything into the variable
$c=5;
$sd=7;
$ed=11;
for ($ca=1;$ca<$total;$ca++)
$startdate=$_POST['$sd'];
echo $startdate;
$enddate=$_POST['$ed'];
Take the single quotes out of the _POST, since it's reading that as a literal string:
$startdate=$_POST[$sd];
echo $startdate;
$enddate=$_POST[$ed];
do not use quotes around variables and its a good idea to use this kind of style everywhere ,like:
$var = "someText " . $var2 . " someOtherText";<br>
instead of
$var = "someText $var2 someOtherText";
I have a form in php that displays checkboxes. These checkboxes are associated with a numerical value populated from mysql. What I'm looking to do is add the values of each checkbox, but only if the box is checked.
The problem I am running into is no matter which boxes I have checked, the value from the first checkbox(es) are returned. For example, if there are 5 total checkboxes and I select the bottom 2, the returned sum is for the 2 top boxes not the bottom boxes. It seems my php code knows boxes are being checked, but just doesn't know which boxes are being checked.
Here is my form code
echo "<input type=\"hidden\" name=\"ID[]\" value=\"".$row['ID']."\" />";
echo "<tr><td> <input type=\"checkbox\" name=\"checked[]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[]\" value=\"".$row['amount']."\" />";
and here is my post
if('POST' == $_SERVER['REQUEST_METHOD']) {
$amt = 0;
$totamt = 0;
foreach($_POST['ID'] as $i => $id)
{
$id = mysql_real_escape_string($id);
$checked = mysql_real_escape_string($_POST['checked'][$i]);
$amt = mysql_real_escape_string($_POST['amount'][$i]);
if ($checked == "Y") {
$totamt = $totamt + $amt;
$amt = 0;
}
}
echo $totamt;
}
Thank you for your help.
The only checkboxes that are sent from the form are the ones that are checked, and the array indexes will start from 0 no matter which ones they are. So there's no correspondence between the indexes of the checkboxes and the indexes of the hidden fields. There are a few ways to deal with this.
One way is to put explicit indexes in the checkbox names:
echo "<tr><td> <input type=\"checkbox\" name=\"checked[".$row['ID']."]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[".$row['ID']."]\" value=\"".$row['amount']."\" />";
Then you can add up:
$totamt += $_POST['amount'][$_POST['checked'][$i]];
Another way is to put the amounts directly in the value of the checkboxes, instead of the useless Y value:
echo "<tr><td> <input type=\"checkbox\" name=\"checked[]\" value=\"".$row['amount']."\" /></td>";
Then you do:
$totamt += $_POST['checked'][$i];
A third way is to put explicit indexes in the names of all the fields, instead of letting PHP assign them when the form is submitted:
echo "<input type=\"hidden\" name=\"ID[".$i."]\" value=\"".$row['ID']."\" />";
echo "<tr><td> <input type=\"checkbox\" name=\"checked[".$i."]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[".$i."]\" value=\"".$row['amount']."\" />";
where $i is a variable that you increment as you're generating the form. This will make the indexes work the way your form code expects.
Looks like your hidden field and the actual checkbox have different names try putting them the same name
<input type='hidden" name='checkbox" value="no" />
<input type="checkbox" name="checkbox" value=$row['ID'] />
That way when you check the $_POST for checkbox name it will either show no or the id and you can determine what was checked
Basically the name of the hidden and checkbox should be the same
Another option to the ones already mentioned would to use JavaScript to do your adding and store the result in a hidden input field. You could use the onclick even to run a JS function that would add up the values (and you should definitely set it in the value of the checkbox instead of a hidden field). Then store your total into the hidden input field value. This would get passed in your POST along with the checkboxes. If you wanted to double check the amount, you could sum up the values of the checkboxes & compare to the hidden field.