I have this table in my View that contains the single form.
like below . in this table , I show all rows of one of my table of database and showing it to the user . then user must fill empty inputs of this form and then hit the submit button to submit the form and then I update the table in my controller with new data that user filled recently.
<table id="myTable" class="display">
<form action="" method="POST">
<thead>
<tr>
<th>آیدی</th>
<th>کد پرسنلی</th>
<th>نام</th>
<th>نام خانوادگی</th>
<th>روزانه</th>
<th>لیست</th>
<th>سرویس</th>
<th>سایر</th>
<th>جمع</th>
<th>آسفالت</th>
<th>توقف شب</th>
<th>غیبت</th>
<th>کمک هزینه تست</th>
<th>توضیحات</th>
</tr>
</thead>
<tbody>
<?php if (isset($allReadyTasks) and $allReadyTasks) { ?>
<?php foreach ($allReadyTasks as $task): ?>
<tr>
<td><?php echo $task->id;?></td>
<td><?php echo $task->personalCode;?></td>
<td><?php echo $task->fname;?></td>
<td><?php echo $task->lname;?></td>
<td><input class="form-control taskInputs" value="<?php echo $task->daily; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->list; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->service; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->other; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->sum; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->asphalt; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->nightPause; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->absent; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->allowance; ?>"</td>
<td><textarea class="form-control"></textarea></td>
</tr>
<?php endforeach; ?>
<?php } ?>
</tbody>
</form>
</table>
my question is : all the inputs have the save name . how can I update the all rows of my table of database with one form and inputs that have same name .!?
Try something like this
<input type="number" class="form-control" name="inputs[]"/>
After that,when you get it with POST, you will have an array of inputs and you can loop throw it with a foreach.
Related
Good morning I am seeking for a help regarding on my php code. I have a problem updating a value in database using get method.
Here is the function I have a form method which is GET and i have a forloop where in inside that loop counts the number of data with the same tracking code now. for example i have a 3 same tracking code so i have a 3 textbox and also i have a 3 submit button. Now i want to update the textbox 1 and click submit it will automatically updated.
For reference here is my code:
enter code here<form method="get" action="view-reservation.php">
<table class="table table-striped">
<tr>
<td>January</td>
<td>February</td>
<td>March</td>
<td>April</td>
<td>May</td>
<td>June</td>
<td>July</td>
<td>August</td>
<td>September</td>
<td>October</td>
<td>November</td>
<td>December</td>
<td></td>
</tr>
<?php foreach ($view as $key => $data):?>
<tr>
<td><input type="hidden" name="track" size="1" value="<?php echo $data->track?>">
<input type="hidden" name="pid" size="1" value="<?php echo $data->pid?>">
<input type="text" name="jan" size="1" value="<?php echo $data->jan?>"></td>
<td><input type="text" name="feb" size="1" value="<?php echo $data->feb?>"></td>
<td><input type="text" name="mar" size="1" value="<?php echo $data->mar?>"></td>
<td><input type="text" name="apr" size="1" value="<?php echo $data->apr?>"></td>
<td><input type="text" name="may" size="1" value="<?php echo $data->may?>"></td>
<td><input type="text" name="jun" size="1" value="<?php echo $data->jun?>"></td>
<td><input type="text" name="jul" size="1" value="<?php echo $data->jul?>"></td>
<td><input type="text" name="aug" size="1" value="<?php echo $data->aug?>"></td>
<td><input type="text" name="sep" size="1" value="<?php echo $data->sept?>"></td>
<td><input type="text" name="oct" size="1" value="<?php echo $data->oct?>"></td>
<td><input type="text" name="nov" size="1" value="<?php echo $data->nov?>"></td>
<td><input type="text" name="dec" size="1" value="<?php echo $data->dec?>"></td>
<td>
<a href="view-reservation.php?track=<?php echo $data->track?>&&pid=<?php echo $data->pid?>&&jan=<?php echo $data->jan?>"
class="label label-danger">Update Payment</a></td>
</tr>
<?php endforeach;?>
<?php endif?>
</form>
</table>
Here is my query when i execute:
if(isset($_GET['track']) && isset($_GET['pid']) && isset($_GET['jan']))
{
//
$jan = filter($_GET['jan']);
$track = filter($_GET['track']);
$pid = filter($_GET['pid']);
$query = $con->query("update payment set jan = '500' where track = '$track' and pid='$pid'") or die(mysqli_errno());
if($query)
{
echo'updated';
}
}
I have a page to update existing recipe records which are in a MySQL database. It brings in the dish details which also includes a separate table of ingredients. However my code only displays one line in the table of ingredients, which I think is the ingredient with the highest ID. I need it to display all the ingredients, but can't work out what needs changing in my code. It gives the user the option to edit and add/delete (using JavaScript) rows if required.
I'm quite new to PHP, so my code may not be efficient but it does update the records. It just won't display all of them in the first instance.
Here's the snippet from my code. The Connection and $DishID are defined earlier on the page.
<table id="Table" border="1">
<tr>
<td>#</td>
<td>IngID</td>
<td>Volume</td>
<td>UnitID</td>
<td>Delete</td>
<td>Add</td>
</tr>
<tr>
<td>1</td>
<?php
$cdquery="SELECT i.IngID, i.IngName, di.Volume, i.UnitID
FROM Ing i
join DishIng di
on i.IngID = di.IngID
Where di.DishID = $DishID";
$cdresult=mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
while ($cdrow=mysqli_fetch_array($cdresult)) {
$IngID=$cdrow["IngID"];
$IngName=$cdrow["IngName"];
$Volume=$cdrow["Volume"];
$UnitID=$cdrow["UnitID"];
}
php?>
<td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
<td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
<td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>
<td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</tr>
</table>
Many thanks in advance.
Bring your input insdie the while loop
and your button outside of while loop like this
echo '<form>';
while ($cdrow=mysqli_fetch_array($cdresult)) {
$IngID=$cdrow["IngID"];
$IngName=$cdrow["IngName"];
$Volume=$cdrow["Volume"];
$UnitID=$cdrow["UnitID"];
?>
<td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
<td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
<td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>
<td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
</tr>
</table>
<?php }?>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</form>
Note mysqli_ does not automatically secure your application, bind your value.
<table id="Table" border="1">
<tr>
<td>#</td>
<td>IngID</td>
<td>Volume</td>
<td>UnitID</td>
<td>Delete</td>
<td>Add</td>
</tr>
< ?php
$DishID = 'put the value here!!!!!!!';
$cdquery="
SELECT
i.IngID,
i.IngName,
di.Volume,
i.UnitID
FROM
Ing i,
DishIng di
WHERE
i.IngID = di.IngID
di.DishID = '".$DishID."'
";
$cdresult = mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
$i = 1;
while ($row = mysqli_fetch_all($cdresult, MYSQLI_ASSOC)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="IngID[]" value="<?php echo $row['IngID']; ?>"></td>
<td><input type="text" name="IngName[]" value="<?php echo $row['IngName']; ?>"></td>
<td><input type="text" name="Volume[]" value="<?php echo $row['Volume']; ?>"></td>
<td><input type="text" name="UnitID[]" value="<?php echo $row['UnitID']; ?>"></td>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</tr>
< ?php } ?>
My problem is that i have a dropdown box that i load the content based on my selection.However in one of these pages that i get the content i have an static method that i need to call before showing the content.all the method does is to check if the that value was set in the session. If it was populate the field. Looks like the method does not fire if i load the content using jquery ajax.
Here is the html i am loading based on my selection:
<table>
<tr>
<td><label class="required">Enter Current Number:</label></td><td><input type="text" size=13 id="current_number" name="current_number" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "current_number"); ?>"></td>
</tr>
<tr><td colspan="2"><strong>Please Enter the Address associated to your current phone number</strong>
NOTE: You may only keep your current phone number if the address of your current phone number is in the same state as the address where you are signing up for CenturyLink service. </td></tr>
<tr>
<td width=20%><label class="required">Address Line 1:</label></td><td><input type="text" size=25 id="address_line1" name="address_line1" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "address_line1"); ?>" ></td>
</tr>
<tr>
<td>Address Line 2:</td><td><input type="text" size=25 id="address_line2" name="address_line2" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "address_line2"); ?>"></td>
</tr>
<tr>
<td><label class="required">Apartment:</label></td><td><input type="text" size=5 id="Apartment" name="Apartment" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "Apartment"); ?>"></td>
</tr>
<tr>
<td><label class="required">City:</label></td><td><input type="text" size=15 id="City" name="City" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "City"); ?>"></td>
</tr>
<tr>
<td><label class="required">State:</label></td><td><select id="State" name="State" >
<option value="">Select a State</option>
<?php foreach (Util::getStatesCodes() as $key => $value) {?>
<option value="<?php echo $key; ?>" <?php Util::getSessionValuebySessionName("phonePorting", "City")==$key)echo "selected";?>><?php echo $key; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td><label class="required">ZipCode:</label></td><td><input type="text" size=15 id="Zipcode" name="Zipcode" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "Zipcode"); ?>" ></td>
</tr>
</table>
Here is the static method:
public static function getSessionValuebySessionName($ssessionName, $keyName){
if(!empty($_SESSION[$ssessionName][session_id()][0][$keyName]))
{
return $_SESSION[$ssessionName][session_id()][0][$keyName];
}
So pretty much what i want to do is to load the sessions values(in the text fields) if they are not empty in the html content when i make the selction in my dropdown. Hope it makes sense.
thank you.
problem resolved. I forgot to start the session in the constructor.....
I have a 5x2 HTML table: http://jsfiddle.net/duxTU/
The table is enclosed in a form.
When the form is submitted it redirects to same page
The input field values are stored in an associative array
The values are displayed in respective fields where they were initially entered.
Code:
<?php
if(isset($_POST['submit'])) {
$contacts_array array(
$_POST["name1"] => $_POST['name2'],
$_POST["name3"] => $_POST['name4'],
$_POST["name5"] => $_POST['name6'],
$_POST["name7"] => $_POST['name8'],
$_POST["name9"] => $_POST['name10']);
$array_filtered=array_filter($contacts_array);
$arrayKeys = array_keys($array_filtered);
$arrayValues = array_values($array_filtered);
}
?>
The array has been filtered to omit null values that may creep in.
Now, how do I make the the HTML form display values? I know about sticky forms but since i must keep the form size constant(5x2), I am trying to use code similar to following
<input type="text" value="<?php echo $arrayKeys[0]; ?> ">
<input type="text" value="<?php echo $arrayValues[0]; ?> ">
... till $arrayKeys[4] and $arrayValues[4], respectively.
This gives me undefined offsets, for example $arrayKeys[5] was not set as no value was entered in the respective form column before submission.
Any solution to this issue??
Maybe this would work for you:
<?php
function getPost($param){
return isset($_POST[$param])?$_POST[$param]:"";
}
?>
<form action="#" method="post">
<table>
<tr>
<td>Name</td><td>Age</td>
</tr>
<tr>
<td><input type="text" name="name1" value="<?= getPost("name1") ?>"></td>
<td><input type="text" name="name2" value="<?= getPost("name2") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name3" value="<?= getPost("name3") ?>"></td>
<td><input type="text" name="name4" value="<?= getPost("name4") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name5" value="<?= getPost("name5") ?>"></td>
<td><input type="text" name="name6" value="<?= getPost("name6") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name7" value="<?= getPost("name7") ?>"></td>
<td><input type="text" name="name8" value="<?= getPost("name8") ?>"></td>
</tr><tr>
<td><input type="text" name="name9" value="<?= getPost("name9") ?>"></td>
<td><input type="text" name="name10" value="<?= getPost("name10") ?>"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
The getPost helper was created because I didn't want to have to type the conditional every time.
You can filter out the nulls for whatever other form processing you are doing.
If you just want to display values, a foreach would work great:
foreach ($array_filtered as $k=>$v) {
echo "<input type='text' value='".$k."'>";
echo "<input type='text' value='".$v."'>";
}
foreach loops through an array. $k is the current key. $v is the current value.
Of course, this should be inside of your form HTML.
I have two tables named tbl_collectiondesireddatetimelocations and tbl_locations
Structure of tbl_collectiondesireddatetimelocations is :
pk_collectiondesireddatetimelocationid
, fk_tbl_locations_locationid, fromdate, todate, fromtime, totime
And structure of tbl_locations is
pk_locationid, name
I have following page:
In the first column location is populated from tbl_locations table.
What i want is to fill the data for the requested column? Data will be retrieved from the tbl_collectiondesireddatetimelocations. For e.g. location id of bangalore is 1. I will check if there is a location id 1 in the tbl_collectiondesireddatetimelocations fk_tbl_locations_locationid column. If it exists it will retrieve the fromdate,todate,fromtime,totime column values and fill that data for that particular location.
I have written following code :
<table style="width:100%;margin:0px;" class="maintable">
<tr><td colspan="1"></td><td colspan="4">
<font style="font-weight:bold;">Requested</font></td>
<td colspan="4"><font style="font-weight:bold;">Assigned</font></td>
</tr><tr>
<td>Location</td>
<td>From Date</td>
<td>To Date</td>
<td>From Time</td>
<td>To Time</td>
<td>From Date/td>
<td>To Date</td>
<td>From Time</td>
<td>To Time</td></tr>
<?php
foreach($locations as $location)
{
foreach($desireddatetimelocations as $desireddatetimelocation)
{
if($desireddatetimelocation['fk_tbl_locations_locationid']==
$location['pk_locationid'])
{
$fromdate=$desireddatetimelocation['fromdate'];
$todate=$desireddatetimelocation['todate'];
$fromtime=$desireddatetimelocation['fromtime'];
$totime=$desireddatetimelocation['totime'];
}
}
?>
<tr><td><?php echo $location['name'];?></td>
<td><input type="text" name="txtFromDate_
<?php echo $location['pk_locationid'];?>" class="field" style="width:80px;"
readonly="" value="<?php echo $fromdate;?>"/></td>
<td><input type="text" name="txtToDate_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $todate;?>"/></td>
<td><input type="text" name="txtFromTime_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $fromtime;?>"/></td>
<td><input type="text" name="txtToTime_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $totime;?>"/></td>
<td><input type="text" name="txtAssignedFromDate_
<?php echo $location['pk_locationid'];?>"
class="date-pick field" style="width:80px;"/></td>
<td><input type="text" name="txtAssignedToDate_
<?php echo $location['pk_locationid'];?>"
class="date-pick field" style="width:80px;"/></td>
<td><input type="text" name="txtAssignedFromTime_
<?php echo $location['pk_locationid'];?>"
class="time-pick field" style="width:80px;" /></td>
<td><input type="text" name="txtAssignedToTime_
<?php echo $location['pk_locationid'];?>"
class="time-pick field" style="width:80px;"/></td></tr>
<?php
}
?>
</table>
Ideally data should be displayed for bangalore and chennai location as table tbl_collectiondesireddatetimelocations contains data for these locations. But i am getting wrong result:Data is repeated for other locations also
Wrong output:
I am confused where I am getting this wrong?
The problem is, that the variables $fromdate, $todate, aso. are still set in the second run even if no location found.
As a quick fix you can just reset them before the inner loop:
foreach($locations as $location)
{
$fromdate='';
$todate='';
$fromtime='';
$totime='';
foreach($desireddatetimelocations as $desireddatetimelocation)
{
Another approach would be to create an associative array of the location=>date/time mapping and use that in the loop:
<?php
$dateTimes = array();
foreach($desireddatetimelocations as $desireddatetimelocation)
{
$dateTimes[$desireddatetimelocation['fk_tbl_locations_locationid']] =
$desireddatetimelocation;
}
foreach($locations as $location)
{
?>
<tr>
<td><?php echo $location['name'];?></td>
<td>
<input type="text" name="txtFromDate_<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly=""
value="<?php echo isset($dateTimes[$location['pk_locationid']]) ?
$dateTimes[$location['pk_locationid']]['fromdate'] : '';?>"/>
</td>
.....