Updating a html form with multiple checkboxs using php - php

I have a html form which i want to check some items in the form and then update the form.
(actually there is no order in which checkbox is going to be mark, it's totally random)
my problem is when i mark some of the checkboxes and press update my code will mark the top checkboxes in order.
This is what i mean :
before submiting :
after submiting :
this is my form code :
$row_counter=0;
if ($_SESSION['user_row_num']=="1")
{
?>
<form method="POST" action="" name="frm1">
<table class='styled-table' cellspacing='0' border='1'>
<tr>
<th scope='col' style='font-size:13px;'>number</th>
<th scope='col' style='font-size:13px;'>view</th>
<th scope='col' style='font-size:13px;'>edit</th>
<th scope='col' style='font-size:13px;'></th>
</tr>
<?php while($row_form = mysqli_fetch_assoc($query_formsearch))
{
//fetching from profile previous valuse of form access
$query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
$row_profile = mysqli_fetch_assoc($query_profile_check);
//creating form name from forms table for profile table
$profile_form_name="form_".$row_form['num'];
$profile_frm_name=$row_profile[$profile_form_name];
$a_form_aces=explode("-", $profile_frm_name);
$frm_view=$a_form_aces[0];
$frm_edit=$a_form_aces[1];?>
<tr>
<td align='center'><input class='styled-input' type='hidden' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
<td align='center'><input class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
<td align='center'><input class='styled-input' type='checkbox' name='view[]' id='view' <?php if($frm_view=="1")echo "checked"; ?> /></td>
<td align='center'><input class='styled-input' type='checkbox' name='edit[]' id='edit' <?php if($frm_edit=="1")echo "checked"; ?> /></td>
<td align='center'><input type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
</tr>
<?php } ?>
</table>
<input class='styled-input_2' style='padding: 5px; width: 140px;' type='submit' name='save_setting' id='save_setting' value="update" >
<div class='cleaner h30'></div>
</form>
this is my update code :
<?php
} // end of if($_SESSION['user_row_num']=="1")
// Check if button name "edit-msb" is active, do this
if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
{
for($i=0;$i<=$_SESSION['user_count'];$i++)
{
$row_no = ($_REQUEST['row_counter'][$i]);
$form_numb = $_REQUEST['form_num'][$row_no];
if(isset($_REQUEST['view'][$row_no])){$_REQUEST['view'][$row_no]="1";}else{$_REQUEST['view'][$row_no]="0";}
if(isset($_REQUEST['edit'][$row_no])){$_REQUEST['edit'][$row_no]="1";}else{$_REQUEST['edit'][$row_no]="0";}
$form_access=$_REQUEST['view'][$row_no]. "-" .$_REQUEST['edit'][$row_no];
$profile_form_num="form_". $form_numb;
$access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
}
if($access_query!='')
{
echo "<div class='cleaner h30'></div>";
echo "<b style='color:green;margin-left:10px;font-size:15px;'>the form successfully updated.</b>";
}
}
?>
I want each check box shows it's updated value after i submiting the form and in the order that i marking them but i don't know where is my mistake.

I've changed your code from checkbox into select option and it's working see if it'll work for you
form code :
<form method="POST" action="" name="frm1">
<table class='styled-table' cellspacing='0' border='1'>
<tr>
<th scope='col' style='font-size:13px;'>form number</th>
<th scope='col' style='font-size:13px;'>form name</th>
<th scope='col' style='font-size:13px;'>view</th>
<th scope='col' style='font-size:13px;'>edit</th>
<th scope='col' style='font-size:13px;'></th>
</tr>
<?php while($row_form = mysqli_fetch_assoc($query_formsearch))
{
//fetching from profile previous valuse of form access
$query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
$row_profile = mysqli_fetch_assoc($query_profile_check);
//creating form name from forms table for profile table
$profile_form_name="form_".$row_form['num'];
$profile_frm_name=$row_profile[$profile_form_name];
$a_form_aces=explode("-", $profile_frm_name);
$frm_view=$a_form_aces[0];
$frm_edit=$a_form_aces[1];?>
<tr>
<td align='center'><input class='styled-input' type='text' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
<td align='center'><input class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
<td align='center'>
<select class='styled-input' name='view[]' id='view' />
<option value="<?php echo $frm_view; ?>"><?php if($frm_view=="1"){echo "yes";}else{echo "no";} ?></option>
<option></option>
<option value="1" style="color:#1A75FF;">yes</option>
<option value="0" style="color:red;">no</option>
</select>
</td>
<td align='center'>
<select class='styled-input' name='edit[]' id='edit' />
<option value="<?php echo $frm_edit; ?>" ><?php if($frm_edit=="1"){echo "yes";}else{echo "no";} ?></option>
<option></option>
<option value="1" style="color:#1A75FF;">yes</option>
<option value="0" style="color:red;">no</option>
</select>
</td>
<td align='center'><input type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
</tr>
<?php } ?>
</table>
update code :
<?php
// Check if button name "edit-msb" is active, do this
if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
{
for($i=0;$i<$_SESSION['user_count'];$i++)
{
//$row_no = ($_REQUEST['row_counter'][$i]);
$form_access=$_POST['view'][$i]. "-" .$_POST['edit'][$i];
echo $profile_form_num="form_". $_POST['form_num'][$i];echo"<br>";
echo $form_access;echo"<br>";
$access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
}
if($access_query!='')
{
echo "<div class='cleaner h30'></div>";
echo "<b style='color:green;margin-left:10px;font-size:15px;'>form successfully updated.</b>";
}
}
?>

Related

Can you update in a div tag

I have a little question about something. I have some forms, where I send the input to a MySQL database. I get the return from the database, out in some div tags. Here I have 2 buttons. Button number 1 can delete the row, and button number 2 should have a function, where I can update a specific row, if I want to change the content of the row. But can I update a div tag? I can see on the net, that a lot of people use tables to do that.
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/arrangeTables.css">
</head>
<body>
<?php
include 'connection.php';
if(isset($_POST['addto'])){
// Insert to database
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];
$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', '$totime')";
$result = mysql_query($sql, $dbhandle) or die(mysql_error($dbhandle));
// Update of the row
if(isset($_POST['update'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$UpdateQuery = "UPDATE addWorkTime
SET date='$_POST[date]',
day='$_POST[day]',
fromtime='$_POST[fromtime]',
totime='$_POST[totime]'
WHERE p_id='$_POST[hidden]'";
$update = mysql_query($UpdateQuery, $dbhandle) or die(mysql_error($dbhandle));
if($update) {
echo "Succes";
} else {
echo "Der er en fejl";
}
}; // brace for if(isset($_POST['update']))
if($result){
echo "Insert successful.";
}
}; // brace for if(isset($_POST['addto']))
if(isset($_POST['delete'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$DeleteQuery = "DELETE FROM addWorkTime WHERE p_id=$hidden";
$delete = mysql_query($DeleteQuery, $dbhandle) or die(mysql_error($dbhandle));
if($delete){
echo "Delete successful";
}
}
//Return records from database
$result = mysql_query("SELECT p_id, date, day, fromtime, totime FROM addWorkTime");
?>
<form method="post">
<h3>Add your worktime to database</h3><br>
Date:
<input type="date" name="date"><br><br>
Day
<select name="day">
<option value="Mandag">Mandag</option>
<option value="Tirsdag">Tirsdag</option>
<option value="Onsdag">Onsdag</option>
<option value="Torsdag">Torsdag</option>
<option value="Fredag">Fredag</option>
<option value="Lørdag">Lørdag</option>
<option value="Søndag">Søndag</option>
</select>
From time:
<input type="time" name="fromtime">
To time:
<input type="time" name="totime">
<input type="submit" name="addto" value="submit"><br><br>
<!-- Return from the database -->
<h3>Return from database:</h3><br>
<!-- headers -->
<table>
<tr>
<th class="column0">Primary Key</th>
<th class="column1">Date</th>
<th class="column2">Day</th>
<th class="column3">From</th>
<th class="column4">To</th>
</tr>
</table>
</form>
<!--loop through through the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<form method="post">
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
<?php endwhile; ?>
</body>
</html>
If you want to update your cell table like your recording, you can do a tricky way like this code snippet:
function change1() {
var inp1 = document.getElementById('myTable').rows[1].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[1].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
function change2() {
var inp1 = document.getElementById('myTable').rows[2].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[2].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
table, td {
border: 1px solid black;
}
<table id="myTable">
<tr>
<td>column 1</td>
<td>column 2</td>
<td>action</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
<td>
<button onclick="change1()">Update</button>
</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
<td>
<button onclick="change2()">Update</button>
</td>
</tr>
</table>
<button onclick="alert('whatever function to save the edit')">Save</button>
you can improve that to give <form> tag to your <table> tag and save that using another Save button. Or you can use if conditional to your update button for change to textfield and post it to your database.
<form method="post" action='function_to_update_or_delete.php'>
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><button onclick="<?php echo change($some_information_to_your_cell)?>">Update</button></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
And, please try to make a function dynamically each row, don't hardcode it like function change1(), function change2(), function change3(), etc
Hope this will help you out. :)

Re-populate dynamic generated checkbox if it fails in validation in an edit form in Codeigniter

I try form validation in codeigniter.
how Re-populate checkbox which value come from database if it fails in validation in an edit form in Codeigniter.OR
How
<tr>
<td>Title</td>
<td><input type="text" name="title" value="<?php if(!empty($mid)){echo $movie1['title'];}elseif(validation_errors()){echo set_value('title'); } ?>"></td>
</tr>
<tr>
<td>Genre</td>
<td>
<?php
if(!empty($mid))
{
$g=explode(",",$movie1['genre']);
}
foreach($genre as $getd)
{
echo"<input type='checkbox' name='genre[]'";
if(!empty($mid))
{
if(in_array($getd['name'],$g))
{
echo"checked='checked' ";
}
}
echo"value='".$getd['name']."'>".$getd['name'];
}
?>
</td>
</tr>
Re-populate dynamic generated checkbox if it fails in validation in an edit form in Codeigniter
Add this statement after value attribute show the correct code.
if(validation_errors()){echo set_checkbox('genre[]', $getd['name']);}
<tr>
<td>Title</td>
<td><input type="text" name="title" value="<?php if(!empty($mid)){echo $movie1['title'];}elseif(validation_errors()){echo set_value('title'); } ?>"></td>
<td><?php echo form_error('title'); ?></td>
</tr>
<tr>
<td>Genre</td>
<td>
<?php
if(!empty($mid))
{
$g=explode(",",$movie1['genre']);
}
foreach($genre as $getd)
{
// echo form_checkbox('genre[]',$getd['name'],set_checkbox('genre[]', $getd['name'])).$getd['name'];
echo"<input type='checkbox' name='genre[]'";
if(!empty($mid))
{
if(in_array($getd['name'],$g))
{
echo"checked='checked' ";
}
}
echo"value='".$getd['name']."'";
if(validation_errors()){echo set_checkbox('genre[]', $getd['name']);}
echo ">".$getd['name'];
}
?>
</td>
<td><?php echo form_error('genre[]'); ?></td>
</tr>

Fetching multiple looped textboxes' values

So I have this program where the user sets up a database table. First, I ask them how many fields they want.
first.php
<html>
<form name="formCreateFields" method="post" align="center" action="second.php">
<p>Number of fields: <input type ="text" name="fieldsNum"/>
<input type="submit" name="submitFieldsNum" value=" Submit "/></p><br>
</form>
</html>
Then I loop the fields, depending on their input above.
second.php
<?php
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
for ($z=1; $z<=$_POST['fieldsNum']; $z++) {
echo "<table align='center'>
<tr>
<th rowspan=2> <big> $z </big> </th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>
<tr>
<td><input type='text' name='fieldName$z'></td>
<td><input type='text' name='fieldType$z'></td>
<td><input type='text' name='fieldLength$z'></td>
</tr>
</table><br><br>";
}
<input type='submit' name='submitFieldSetup'>
</form>";
?>
I'm having problems after this. I've been trying to test fetching them by putting them in an array and using foreach to view them but can't seem to get anywhere. I thought it was okay to use something like $_POST['fieldName$z'] but I guess I was wrong.
I just need to find out how I could fetch all the inputs in the second file. Any ideas? Thanks in advance! :)
If fieldsNum is a number, the for should be fine in this case, just properly concatenate the values:
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
echo "<table align='center'>";
echo '
<tr>
<th rowspan=2></th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>';
for ($z = 1; $z <= $_POST['fieldsNum']; $z++) {
echo "
<tr>
<td><input type='text' name='inputs[$z][name]'></td>
<td><input type='text' name='inputs[$z][type]'></td>
<td><input type='text' name='inputs[$z][length]'></td>
</tr>
";
}
echo '</table><br><br>';
echo "<input type='submit' name='submitFieldSetup'>";
echo '</form>';
Then in third.php;
if(isset($_POST['inputs'])) {
$inputs = $_POST['inputs'];
foreach($inputs as $input) {
echo $input['name'];
echo $input['type'];
echo $input['length'];
}
}

when i add a comment i get this warning

hello guys i have a problem with this code
when id add a comment and comment insert in database
and when i make refresh on the same page i get this warning :
One of the fields are still empty,
i think the problem in the first code
<?php
if ($_POST['add'] and $_POST['add']=='comm'){
$comm_name =strip_tags($_POST['comm_name']);
$comm_country =strip_tags(mysql_real_escape_string($_POST['comm_country']));
$c =strip_tags(mysql_real_escape_string($_POST['comm']));
$comm_thread =strip_tags(mysql_real_escape_string($_POST['comm_thread']));
$status =$_POST['status'];
$getidtopic=$_GET['id_topic'];
$post_code=$_POST['post_code'];
if ($comm_name=='' or $comm_country=='' or $c=='' or $post_code=='' ){
echo "<script>alert(\"One of the fields are still empty
\");</script>";
}else if ($_POST['post_code']==$_SESSION['code']) {
$insertcomm=mysql_query("insert into comments values('','$comm_name','$comm_country','$comm','$comm_thread','$status') ")or die (mysql_error);
echo "<script>alert(\"your comment has been adding\");</script>";
}
}
?>
and this is the comment's form
<form action='' method='post' >
<table class='rightcol' width='100%' cellpadding='0' cellspacing='5'>
<tr>
<td colspan='3' id='addcomm'>add comm</td>
</tr>
<tr>
<td width='15%' ><div id='title_comm' value=''>name : </div></td>
<td ><input type='text' name='comm_name' value='<?if (!$insertcomm){
echo $comm_name;
}?>'/></td>
</tr>
<tr>
<td width='15%' ><div id='title_comm'>country </div></td>
<td ><input type='text' name='comm_country'
value='<?if (!$insertcomm){echo $comm_country;}?>'/>
</td>
</tr>
<tr>
<td valign='top' width='15%'><div id='title_comm'>comment : </div></td>
<td width='50%'>
<textarea cols='55' rows='12' name='comm'>
<?if (!$insertcomm){echo $c;}?>
</textarea></td>
<td valign='top' ><div id='note_comm'>
your comment will not insert if you try to use some thing bad
</div></td>
</tr>
<tr>
<td width='15%' ><div id='title_comm'><span style='color:red'>code : <br/>write these codes </span></div></td>
<td ><input type='text' name='post_code'/></td>
</tr>
<tr>
<td ><div id='code'>
<?php
$text=rand(400,80000);
echo $_SESSION['code']=$text;
?>
</div></td>
</tr>
<td colspan='4' ><input type='submit' name='addcomm' id='add' value='add comm'/></td>
</table>
<input type='hidden' name='comm_thread' value='<?php echo $getidtopic;?>' />
<input type='hidden' name='add' value='comm'/>
<input type='hidden' name='status' value='2'/>
</form>
that is because, when you refresh the page, the $_POST fields are reset, and the fields get empty, so when the page executes its PHP line if ($comm_name=='' or $comm_country=='' or $c=='' or $post_code=='' or $post_code=='' ){ it will find them to be empty.
You need to show us the table definition to better figure out what the issue is exactly. My guess is that your SQL statement is not properly prepared.

Update sql data from form and checkboxes

I have created a form that update the data in a sql column, so far all good.
The problem I have is that I would like to choose what columns are going to be updated, with checkboxes.
So I want to categorize two files as movies, I just check the checkbox and write movies in the form.
But its not only that. Every column is already connected to a checkbox, but I use this checkbox with an delete query, so I can delete multiple rows.
So my question is more like, how can I combine this two function with "one" checkbox.
This is some of my code.
This is my table with the sql outputs, and with the delete button
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
echo "<tr>";
echo "<td>".$row['user_name'] . "</td> ";
?>
<td class="download"> <?php echo $row['file_name']; ?></td>
<?php echo "<td>".$row['file_time'] . "</td> "; ?>
<?php echo "<td>".$row['file_ip'] . "</td> "; ?>
<?php echo "<td>".$row['category'] . "</td> "; ?>
<td>
<form action="delete.php" method="post">
<input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>">
</td>
<?php
}
echo "</tr>";
echo "</table>";
?>
<input type ="submit" value ="Delete">
</form>
This form is the one with the update function
<form action="function.php" method="post">
category: <input type="text" name="category" />
<input type="submit" />
</form>
and
function.php
include('core/inc/init.inc.php');
if(isset($_POST['category'])){
$category = $_POST['category'];
mysql_query("UPDATE files SET category='$category'
/*WHERE category='genom' */ ");
header("Location: profile.php?uid=" . $_SESSION['uid']);
}else{
header("Location: profile.php?uid=" . $_SESSION['uid']);
};
How can I combine these two functions?
You can have multiple submit buttons in the same form. So you can do something like this:
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<form action="updateordelete.php" method="post">
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
?>
<tr>
<td> <?php echo $row['user_name']; ?></td>
<td class="download"> <?php echo $row['file_name']; ?></td>
<td><input type="text" name="file_time[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_time']; ?>" /></td>
<td><input type="text" name="file_ip[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_ip']; ?>" /></td>
<td><input type="text" name="file_category[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['category']; ?>" /></td>
<td><input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>"></td>
</tr>
<?php } ?>
</table>
<input type ="submit" name="submittype" value = "Update">
<input type ="submit" name="submittype" value = "Delete">
</form>
And then, in updateordelete.php:
if($_POST['submittype']=="Delete"){
(do delete code...)
}elseif($_POST['submittype']=="Update"){
(do update code...)
}
For each of the values, you can access them using $_POST['file_category']. For example:
$file_categories=$_POST['file_category'];
foreach($_POST['done'] as $file_id){
echo $file_categories[$file_id];
}

Categories