Getting post data from selected rows in a table - php

I've been trying to think this through, but can't seem to figure out how to get data from only one selected row in a table. I have a table that I am populating with php, lets say it has a name, tn, address in one column. I was thinking of putting a checkbox at the end of the column and having the user check it if the data can be sent to the server. Now, if I do that how do I get the data from that column? I could use javascript to populate the table also, but I'm still stuck with the same problem of how to get the data from the column to update my tables.
<?php
foreach (defaultPay() as $key => $values):
foreach ($values as $value):?>
<tr>
<td>
<label for="number"></label><input type="text" id="number" name="empNum" value="<?= $value['empNum']; ?>">
</td>
<td>
<label for="name"></label><input type="text" id="name" name="empName" value="<?= $value['empName']; ?>">
</td>
<td>
<label for="stdHrs"></label><input type="text" step="any" id="stdHrs" name="stdHrs" value="<?= $value['unitRate']; ?>">
</td>
<td>
<label for="uniRate"></label><input type="text" step="any" id="uniRate" name="uniRate" value="<?= $value['rate']; ?>">
</td>
<td>
<label for="salRate"></label><input type="text" step="any" id="salRate" name="salRate" value="<?= $value['salary']; ?>">
</td>
<td>
<label for="gross"></label><input type="text" step="any" id="gross" name="gross" value="<?= $value['gross']; ?>">
</td>
</tr>
<?php }
} ?>

I suppose you are populating the table from some kind of php array. If you don't have unique identifier, each element have index in array. You could use that index and set it as checkbox value.
For example:
<input type="checkbox" value="<?php echo $index; ?>" />
where $index is unique table index. After form submition you will have selected indexes of the table. Remember to keep te table order the same.

If only one row will be selected by the user a button within a form will do. Forms and hidden input does not affect page rendering. Just make the form CSS display:inline; The whole form can be put in one small <td> table element regardless of how much data you want to store in multiple <input type="hidden"/> tags.
'<tr><td><form action="save.php" method="post" ><input type="hidden" name="id" value="1234" /><input type="hidden" name="col1" value="' . $row[1] . '" /><input type="hidden" name="col2" value="' . $row[2] . '" /> <button type="submit">Save</button></form></td><td>Text describing the row</td></tr>';
The first column in the table has a "Save" button which submits the data.

Related

PHP - How can I retain values of textbox array after form submit?

I just have a question.
I have a group of textbox with the same name. (name = "quantity[]")
How can I retain the values of the form after submit? For example, I have 5 textbox with the same name. I filled out 4 of them and submitted the form. I want to display an error that a field is empty and retain the values that I put before I submitted the form.
I can do it if the textbox names are different. But if they have the same, I can't make it work.
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="submit" name="submit">
Thanks for your help php people. Your help is appreciated.
If field quantity is not fixed, that is, it varies. You should use for loop like this
<?php
if(!empty($_POST['quantiy'])){
$count =count($_POST['quantity']);
for($i=0; $i<$count; $i++){
?>
<input type="text" name="quantity[]" value="<?php echo $_POST['quantity'][$i];?>">
<?php
}
}
else
{
?>
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<?php
}
?>
your problem is that you loose the reference to the input fields when the form is submitted. Since all fields have the same name you cannot be sure, which one provided the input.
What you can do: You could populate your form fields by popping the $_POST like so:
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo array_pop($_POST['quantity']);?>">
What happens is the following:
upon submission your $_POST-variable gets populated depending on which inputs have any values
after submission at re-rendering the $_POST['quantities'] array gets disassembled, the last element gets removed and it populates the form input
A word of caution: If the user fills out field 1,2 and 4 with this code the fields 1,2 and 3 will be populated.

How to populate old values in search field after searching in cakephp 2.x

I have a search form
View file:
<td>Product</td>
<td><input type="text" name="data[Search][product]" value="<?php echo $oldData['Search']['product'];?>"></td>
<td>Product type</td>
<td><input type="text" name="data[Search][product_type]"></td>
<td>Growing Practice</td>
<td><input type="checkbox" name="data[Search][growing_method][]" value="organic" />Organic<br>
<input type="checkbox" name="data[Search][growing_method][]" value="ecological" />Ecological<br>
<input type="checkbox" name="data[Search][growing_method][]" value="ipm" />IPM<br>
<input type="checkbox" name="data[Search][growing_method][]" value="bio_dynamic" />Bio-Dynamic<br>
<input type="checkbox" name="data[Search][growing_method][]" value="conventional" />Conventional<br>
<input type="checkbox" name="data[Search][growing_method][]" value="other" />Other
</td>
<td>Ugly</td>
<td><input type="checkbox" name="data[Search][ugly]" value="y" /><br>
<td>Miles</td>
<td><input type="number" name="data[Search][miles]" id="search_mile"></td>
<td>from</td>
<td>Zip</td>
<td><input type="text" name="data[Search][zip]" id="search_zip"></td>
<td><button type="submit" id="search_post">Search</button> </td>
After clicking on the search button the search takes place but I need the submitted values to stay back in the form so the user can see what they have searched on.
In controller I tried to the values as we set it.
$this->set('oldDatas',$this->request->data);
But I was not able to get it in the view from the following code.
<td><input type="text" name="data[Search][product]" value="<?php echo $oldData['Search']['product'];?>"></td>
I searched a log but was not able to get the desired result.
Is there a way to populate the values back in the form after submitting.
If anyone have any idea about it please let me know
Thanks
Try storing the data in a PHP session variable. Cake has a session component where you can write to variables. You can use the Session Helper to set the value of the input in the view.
Controller:
$this->Session->write('product', $this->request->data['Search']['product']);
View:
<td>
<input type="text" name="data[Search][product]" value="<?= $this->Session->read('product') ?>">
</td>
http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html

unable to store my all retrieve the data from the Mysql table to my new html form.

Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.

set $_POST value when radio button is selected

I have a page with some radio buttons and form. When I select one radio button I need its id to be set to $_POST['id'] varible so when I submit the form I can check which radio button was selected and act accordingly. I know I can't do it directly so tried using
onclick="document.getElementById("id").value="'.$row['id'].'"
on radio button but it didn't work.
$d="select * from {$_c['dbprefix']}card where active='1' order by s_sent desc, s_order asc limit 0,{$_c['filtr']['step']}";
$r=mysql_query($d);
if (mysql_num_rows($r)>0) {
echo '<div class="cards">';
echo '<form method="POST">';
$i=0;
while ($row=mysql_fetch_array($r)):
echo '<div class="item">';
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
echo '<label for="'.$row['id'].'" class="itemin" style="height:'.$_c['card']['sy'].'px"><img src="pub/cards/'.$row['id'].'s.jpg" /></lable>';
echo '</div>';
$i++;
if ($i%3==0) echo '<div class="cl-left"></div>';
endwhile;
echo '<div class="cl-left"></div>';
echo '</div>';
}
?>
<div class="dvasl">
<div class="sl1">
<fieldset>
<legend>Saatja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[from_name]" value="<?php echo $ap['set']['from_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[from_email]" value="<?php echo $ap['set']['from_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Saaja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[to_name]" value="<?php echo $ap['set']['to_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[to_email]" value="<?php echo $ap['set']['to_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
</div>
<div class="sl2">
<fieldset>
<legend>E-kaardi tekst</legend>
<table class="info">
<tr>
<td><textarea name="item[text]" cols="20" rows="8" class="inpedit2"></textarea></td>
</tr>
</table>
</fieldset>
</div>
<div class="cl-left"></div>
</div>
<div class="buttons">
<div class="t2"><input type="submit" value="Vaata kaardi eelvaadet" name="button[nahled]" class="unbutt1"></div>
</div>
<input type="hidden" name="id" value="<?php echo $ap['card']['id']; ?>">
<input type="hidden" name="action" value="itemadd">
</form>
<?php
$k=floor(($i-1)/3);
if ($k<2) echo '<div class="spacer-'.(2-$k).'"></div>';
?>
Its better if you can use a hidden field in the form and update its value with the id of the selected radio button and when the form will be posted you can get its value and more over this after once you changed the value of the hidden field you can access it with javascript also with id attibute and can get it in the post by name attribute
add a hidden field named
<input type="hidden" id="checked_radio" name="checked_radio" value="" />
and then change the value with javascript on click of button
<input type="radio" id="foo" name="foo" onclick="document.getElementById('checked_radio').value=this.value;" />
The value of the selected radio button gets posted, and the name is used to group radio buttons together. (Only one radio button with the specified name can be selected.) Instead of trying to post the value of the id attribute you should add a value attribute and set that. You could set it to the same value as the id attribute, or remove the id attribute completely.
When I select one radio button I need its id to be set to $_POST['id'] variable
You are changing the value, not the id, to change the id use:
onclick="document.getElementById("id").id="<?php echo $row['id'] ?>";
Anyway, I think you should use the name attribute for this.
change this line
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
to
echo '<input id="'.$row['id'].'" type="radio" value="'.$row['id'].'" name="id" class="detail">'.$_l['detailtitle'].'</input>';
when your form submit,you will get the selected id as $_POST['id']

Handle submitted $_POST values, when more than one form/submit button is available on one page

This foreach loop creates about 120 table rows.
In every row there are two input fields where the user can update/change the first and last name.
After changing the input value, the user clicks on the "Save" button to send the input values with $_POST.
<table>
<? foreach ($members as $member) {?>
<form name="member" action="" method="POST">
<tr>
<td><input name="id" type="hidden" value="<? echo $member['id'] ?>"></td>
<td><input name="first_name" type="text" value="<? echo $member['first_name'] ?>"></td>
<td><input name="last_name" type="hidden" value="<? echo $member['last_name'] ?>"></td>
<td><input type="submit" value="Save"></td>
</tr>
</form>
<? } ?>
I see the problem, that there are 120 input fields with name="id" and name="first_name" and name="last_name".
Normaly I would take the submited value like this and but it into a variable:
$first_name = $_POST['first_name'];
Will the form only submit the values inside the <form>-tags where the submit button is located or is there another solution for preventing that values get mixed up between other forms?
Since your form tag is inside of your foreach loop, then yes: each submit button will only submit the first_name and last_name values that are on the same form as the clicked submit button.
However, your html is not valid. Valid html would dictate that you either create each form within a table cell (td tag), or find a different way to correlate a save button with specific fields (with some javascript or different markup). See this answer: https://stackoverflow.com/a/1249715/2061789
You will need to use different table for each form. But you can distinguish the fields, by keeping different names, based on a counter. Like this:
<?
$i = 0;
foreach ($members as $member) {
?>
<table>
<form name="member" action="" method="POST">
<tr>
<td><input name="<?php echo 'id_'.$i;?>" type="hidden" value="<? echo $member['id'] ?>"></td>
<td><input name="<?php echo 'first_name_'.$i;?>" type="text" value="<? echo $member['first_name'] ?>"></td>
<td><input name="<?php echo 'last_name_'.$i;?>" type="hidden" value="<? echo $member['last_name'] ?>"></td>
<td><input type="submit" value="Save"></td>
</tr>
</form>
</table>
<?php
$i++;
} ?>

Categories