I have a list of checkboxes created by a while loop but when the form is submitted the checkboxes are cleared. I have tried to use the unique id of the value in the record by putting it in a hidden field and then using it in an if query before marking the checkbox as checked but it does nothing
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?>
<input type="hidden" name="audit_id_confirm" value="<? echo $row['auditID'];?>">
</td>
<td width="33"><input type="checkbox" name="audit_selected[]"
value="<? echo $row['auditID'];?>"
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
</td>
</tr>
<?php
}
?>
I sorted it by checking id=f the id number was in the arrays that had been posted.
<?php
$selected = $_POST['audit_selected'];
while($row = mysqli_fetch_array($result)){
$audit = $row['auditID'];
if(in_array($audit, $selected)) {
$check="checked='checked'";
}else{
$check = '';
} ?>
<tr class="hover">
<td width="180">
<?php echo $row['auditName'].' '.$num_audited; ?></td><td width="33"> <input type="checkbox" name="audit_selected[]" value="<?php echo $row['auditID'];?>" <?php echo $check; ?> >
</td>
</tr>
<?php }
?>
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
Replace above line with this:
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked='checked'"; }?>>
You are not properly providing checked attribute to the Html entity.
One of the information you need to store in the table, is the state of the checkbox.
So, you can create a new column in your table, call it "checkboxstate".
Now inside your code use the following code to insert the checkbox:
<input type="checkbox" name="<?php echo($posted); ?>">
Thus far we have created several checkboxes whom names are the same as $posted, in other words each checkbox will have the name of its row's "$posted" value.
Now the only thing that is left is submitting the form and storing the checkbox info, which will return either TRUE or FALSE, and we will save that in our table, in the "checkboxstate" column.
So now in our table we will have a bunch of rows with several columns, one of the columns will be "checkboxstate" whom value will be either TRUE or FALSE.
At this point we have pretty much solved the problem, all that is left is to insert a simple if to either show a checked checkbox or an unchecked one.
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
$cbs = $row['checkboxstate'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?></td>
<td width="33">
<?php if($cbs == "true") echo "<input type='checkbox' name='$posted' checked='checked'>"; else echo "<input type='checkbox' name='$posted'>";
</td>
</tr>
<?php
}
?>
Related
I have a list of items that will be fetched from DB. Each row have one checkbox and an associated value in DB. If it is checked and saved the value changes to 1 else it will be 0.
So on page load there is a drop down to select the items.Based on the selection, the data will be fetched. Values with 1 will be shown checked and 0 will be unchecked.
I am trying to update this to DB on button click. So newly checked ones will be saved with status 1 and unchecked will be updated with status 0.
If one value was previously 1 and is unchecked during update, the value should change to 0.
My code:
HTML:
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<tbody>
<tr class="even pointer" id="<?php echo $row['tcode'] ?>">
<td class="a-center ">
<?php if($row['selected'] == 0){ ?>
<input class="flat" type="checkbox" name="check_list[]" value="<?php echo $row['tcode'] ?>">
<?php } else{ ?>
<input class="flat" type="checkbox" name="check_list[]" value="<?php echo $row['tcode'] ?>" checked>
<?php } ?>
</td>
<td class=" "><?php echo $row['task_name'] ?></td>
<td class=" "><?php echo $row['tcode'] ?></td>
<td class=" "><?php echo $row['task_category'] ?></td>
</td>
</tr>
</tbody>
<?php } ?>
PHP:
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
$projectcode=$_POST['projectcategory'];
// Loop to store and display values of individual checked checkbox.
echo $_POST['check_list'];
foreach($_POST['check_list'] as $selected){
$sql2="UPDATE mappedtask SET selected=1 WHERE pcode = '$projectcode' AND pcode = '.$_POST[tcode].' ";
$result2=$conn->query($sql2);
}
}
}
I have set up an update query which will update values entered into text fields on a while loop.Then for some reason only the last row data in the loop will be updated and the rest will stay the same.i know the issue .cause the the last attribute row overwrite the other in form so when update update only the last row
code
<?php
include'../config/connect.php';
$id = $_SESSION['agent'];
$sql = "Select * from nps where agent_id = '$id'";
$result = $cont->query($sql);
if ( $result->num_rows > 0 ){
?>
<form action="update.php" method="post">
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['phone']; ?> </td>
<td> <?php echo $row['survey_date']; ?> </td>
<td> <input type="hidden" name="id" value="<?php echo $row['agent_id']; ?>"> <?php echo $row['agent_id']; ?> </td>
<td> <?php echo $row['agent_name']; ?> </td>
<td> <?php echo $row['nps_rating']; ?> </td>
<td> <?php echo $row['sats']; ?> </td>
<td> <?php echo $row['agent_satisfaction']; ?> </td>
<td> <?php echo $row['ir']; ?></td>
<td><textarea name="comment" ><?php echo $row['comment']; ?></textarea></td>
</tr>
<?php
}
};
?>
</table>
<div><input type="submit" name="submit" > </div>
</form>
update.php file
<?php
session_start();
include'../config/connect.php';
$comment = $_POST['comment'];
$id = $_POST['id'];
if($_POST['submit']){
$sql = "UPDATE nps SET comment='$comment' WHERE id='$id' ";
};
if ($cont->query($sql) === true){
echo 'done';
}else{
echo 'notdone';
}
?>
First of all, you have two input names: id and comment. Once submitted, the form will be processed from start to finish, meaning the values that are actually processed in your PHP will be the last ones.
You will want to make these unique, and since you’re ID is hidden, it is less important. My suggestion would be to dynamically create names, such as comment_{id} and than in your PHP with the query to update, you go through all of your $_POST values, replace out the comment_ from the key, and execute a query to update the values.
You are also open to SQL injection and should take steps to prevent that.
You must have have inputs with unique names. I suggest to make a input name from row’s ID and attribute name, as for e.g. comment will be like ‘1-comment’. Then in the update script you need to iterate over the data and make the update query for each data. Cheers.
hye all expert, i have a page (history.php) that retrieve their old data after user login:- beside each TITLE there a check box,
TITLE
<checkbox> BAT101
<checkbox> BAT102
<checkbox> BAT201
<button> COMPARE SELECTED
for example, let say if user check checkbox for BAT101 and BAT201 then click button COMPARE SELECTED then go to the new page .php that will display like this:-
TITLE PERCENTAGE RESULT
BAT 101 30%
BAT 201 50.2%
The data for percentage result were stored in a database, same with the title..
Anyone know the source code should i implement?
This is my code for history.php currently..many thanks...!!
<?php
session_start();
if(isset($_SESSION['idmember'])){
$idmembersession = $_SESSION['idmember'];
}
include'configure.php';
?>
<html>
<title>History Page</title><head></head>
<body>
<table>
<tr>
<th></th>
<th>TITLE</th>
<th>ACTION</th>
</tr>
<?php
$query = "SELECT * FROM documents where idmember='$idmembersession'";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query,MYSQL_ASSOC)){
$title = $data['subject'];
?>
<tr>
<td><input type="checkbox" name="checkbox" value="<?php echo $title ?>" /></td>
<?php
echo "<td>$title</td>";
}
?>
</tr>
</table>
Compare selected
</body></html>
use this for creating check box...
<input type="checkbox" name="selectedcheck[<?php echo $title ?>]" value="<?php echo $title.'=>'.$iddoc; ?>" />
it will create a array with name selectedcheck[].
in the newpage.php you can catch this array of selected check box by this using
<?php
if(isset ($_POST["vehicle"]))
{
$selectedcheckbox = $_POST["selectedcheck"];
foreach($selectedcheckbox as $title=>$value)
{
echo $title . " Value=" . $value;
}
}
?>
i have a upload result form for a certain semester. where i have collected all the subjects and student list for a certain semester.but my problem is i can't insert the result to the database.only a single row is inserted,
controller
$data=array();
for($i=0;$i<count($_POST['Number']);$i++){
if($_POST['Number'][$i] != ''){
$data=array(
'Id'=>$_POST['id'][$i],
'Dept'=>$_POST['Dept'][$i],
'SubjectCode'=>$_POST['sCode'][$i],
'SubjectName'=>$_POST['sName'][$i],
'Semister'=>$_POST['Semister'][$i],
'MidNumber'=>$_POST['Number'][$i]
);
$this->load->model('Upload_Result_model');
$this->Upload_Result_model->upload_midResult($data);
}
}
form:
<table>
<tr>
<th>Student ID</th>
<?php foreach($subjects as $s):?>
<th><?php echo $s->subjectName; echo "<br>"; echo "(".$s->subjectCode.")"; ?></th>
<?php foreach($student as $st):?>
<input type='hidden' id='id[]' name='id[]' value='<?php echo $st->Id;?>'>
<?php endforeach;?>
<input type='hidden' name='Dept[]' id='Dept[]' value=<?php echo $Dept;?>>
<input type='hidden' name='Semister[]' id='Semister[]' value=<?php echo $Semister;?>>
<input type='hidden' id='sCode[]' name='sCode[]' value='<?php echo $s->subjectCode?>'>
<input type='hidden' id='sName[]' name='sName[]' value='<?php echo $s->subjectName?>'>
<?php endforeach;?>
</tr>
<?php foreach($student as $st):?>
<tr>
<td><?php echo $st->Id?></td>
<?php for($i=0;$i<count($subjects);$i++):?>
<td><input type='text' size='7' id='Number[]' name='Number[]'/></td>
<?php endfor;?>
</tr>
it will be very much helpful for me if anyone fix this problem
i dont think the html form elements will come in as arrays in php. try doing print_r($_POST); to see the passed variables and their values. they might come in just comma separates.
i think you might need to do something like $_POST['id']=explode(',',$_POST['id']); and so on to fix your problem
the Numbers variable also has other problems you should look in to
i think
foreach($student as $st):
should be after the . Now you open multiple rows, but not closing them. Also Ids are inside 2 for loops, so they are more than the other elements, I am not sure if this is what you try to do.
So, if this is ok you can try:
$Numbers = #$_POST['Number'];
$ids = #$_POST['id'];
foreach($Numbers as $a => $b)
{
if($Numbers[$a]!='')
{
$data=array(
'Id'=>$ids[$a];, ... }
}
}
I have a form that takes the following inputs:
Name: IBM
Surface(in m^2): 9
Floor: (Checkbox1)
Phone: (Checkbox2)
Network: (Checkbox3)
Button to send to a next php page.
All those values above are represented in a table when i press the submit button.
The first two (name and surname) are properly displayed in the table.
The problem is with the checkboxes. If i select the first checkbox the value in the table should be presented with 1. If its not selected the value in the table should be empty.
echo "<td>$Name</td>"; // works properly
echo "<td>$Surface</td>"; // works properly
echo "<td>....no idea for the checkboxes</td>;
Some part of my php code with the variables:
<?php
if (!empty($_POST))
{
$name= $_POST["name"];
$surface= $_POST["surface"];
$floor= $_POST["floor"];
$phone= $_POST["telefoon"];
$network= $_POST["netwerk"];
if (is_numeric($surface))
{
$_SESSION["name"]=$name;
$_SESSION["surface"]=$surface;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($floor) && ($phone) && ($network))
{
$_SESSION["floor"]=$floor;
$_SESSION["phone"]=$phone;
$_SESSION["network"]=$network;
header("Location:ExpoOverzicht.php");
}
}
?>
Second page with table:
<?php
$name= $_SESSION["name"];
$surface= $_SESSION["surface"];
$floor= $_SESSION["floor"];
$phone= $_SESSION["phone"];
$network= $_SESSION["network"];
echo "<table class=\"tableExpo\">";
echo "<th>name</th>";
echo "<th>surface</th>";
echo "<th>floor</th>";
echo "<th>phone</th>";
echo "<th>network</th>";
echo "<th>total price</th>";
for($i=0; $i <= $_SESSION["name"]; $i++)
{
echo "<tr>";
echo "<td>$name</td>"; // gives right output
echo "<td>$surface</td>"; // gives right output
echo "<td>...</td>"; //wrong output (ment for checkbox 1)
echo "<td>...</td>"; //wrong output (ment for checkbox 2)
echo "<td>...</td>"; //wrong output (ment for checkbox 3)
echo "<td>....</td>";
echo "</tr>;";
}
echo "</table>";
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="18"/></td>
</tr>
<tr>
<td>Surface(in m^2):</td>
<td><input type="text" name="surface" size="6"/></td>
</tr>
<tr>
<td>Floor:</td>
<td><input type="checkbox" name="floor" value="floor"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="checkbox" name="phone" value="phone"/></td>
</tr>
<tr>
<td>Network:</td>
<td><input type="checkbox" name="network" value="network"/></td>
</tr>
<tr>
<td><input type="submit" name="verzenden" value="Verzenden"/></td>
</tr>
</table>
There might be a few spelling mistakes since i had to translate it.
Best regards.
Instead of directly assigning your checkbox variables, see if they have been checked or not first.
$verdieping = isset($_POST["floor"]) ? $_POST["floor"] : 0;
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
This way, if the user hasn't ticked a checkbox, you have a value of '0' assigned to it instead of an undefined variable.
If you declare a checkbox with:
<input type="checkbox" name="mycheckbox" value="1">
you can check the value after submitting the form by:
if(!empty($_POST["mycheckbox"])) {
// checkbox was checked
}
else {
// checkbox was not checked
}
In this php page you can write like this, it may be solution of your question
if (!empty($_POST))
{
$standnaam = $_POST["name"];
$oppervlakte = $_POST["surface"];
$verdieping = $_POST["floor"];
$telefoon = $_POST["telefoon"];
$netwerk = $_POST["netwerk"];
if (is_numeric($oppervlakte))
{
$_SESSION["name"]=$standnaam;
$_SESSION["surface"]=$oppervlakte;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($verdieping) && ($telefoon) && ($netwerk))
{
$_SESSION["floor"]=$verdieping;
$_SESSION["phone"]=$telefoon;
$_SESSION["network"]=$netwerk;
header("Location:ExpoOverzicht.php");
}
}