Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}
Related
I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.
Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download
I have a database where I view all the records, the last column is the table ID, I click it and I want to be able to edit that rows data only, here's what I got but it doesn't waork after updating phpmyadmin:::
<?php
include "db.inc.php";
$id=$_GET['id'];
$order = "SELECT * FROM ircb where id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Date</td>
<td>
<input type="text" name="cdate"
value="<? echo "$row[cdate]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
<tr>
<td>Item</td>
<td>
<input type="text" name="item"
value="<? echo "$row[item]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
I end up getting no results returned, when I hover over the link it does display the correct table row id but when I click the link I get empty boxes containing some of the code like <? echo in the fields..no true values though..and the page header after clicking the link does show ::: ...../edit_form.php?id=8 for row 8 so I assume something in my query is not quite right.
thanks
It is because you try to GET a name "id" from a form which is POSTed ;) And also embed PHP in html more clearly. For example, value="<?php echo $row['id']; ?>" is more "true" rather than value="<? echo "$row[id]"?>" ;)
I had following code.My textbox attend is looped for number of times.I want to get value of each attend when my form is post to insert value in database.
<form method="post" action="insert.php">
<label>Subject</label>
<input type="text" maxlength="30" name="subject" />
<label>Total Lectures</label>
<input type="text" maxlength="30" name="total" />
<table width="537" border="1">
<tr>
<td width="90">Name</td>
<td width="139">Roll Number </td>
<td width="136"> </td>
<td width="144">Attendence</td>
</tr>
<?php
$query=mysql_query("SELECT * FROM STUDENT");
$i=0;
while($rec=mysql_fetch_array($query)){
$i++;
?>
<tr>
<td><?php echo $rec['name']; ?></td>
<td><?php echo $rec['roll_number']; ?></td>
<td> </td>
<td><input type="text" maxlength="10" name="atten" /></td>
</tr>
<?php } ?>
</table>
<input type="submit" value="submit" />
</form>
and my insert.php page is
if($_SERVER['REQUEST_METHOD']=='POST'){
$query=mysql_query("SELECT * FROM STUDENT");
while($rec=mysql_fetch_array($query)){
$attend=$_POST['atten'];
$subject=$_POST['subject'];
$total=$_POST['total'];
$query1=mysql_query("INSERT INTO course VALUES('$i','$subject','$total','$attend','')") or die(mysql_error());
}
}
I am getting only 1 value of text box.
The problem is that you have many inputs all with the name atten. When you post this form, only one of those values will be carried forward.
If you change the name to atten[] all of the values would be posted as an array, which you would then have to loop through to build you insert query.
You could access this posted array as follows:
$attendee_array = $_POST['atten'];
foreach($attendee_array as $attendee) {
// perform insert or build insert query (prefereable as you can do all these inserts at once) here
// make sure to escape your data for input
}
Change the names of your inputs:
<input name="subject[]" value="Lorem" />
<input name="total[]" value="ipsum" />
<input name="atten[]" value="dolor" />
Then:
$_POST['subject'][0] == 'Lorem'
$_POST['total'][4] == 'amet'
If so, that would make my life ten times easier, as I could send an
indefinite amount of information through a form and get it processed
by the server simply by looping through the array of items with the
name "subject". etc.
You need to use input array like:
<input type="text" maxlength="10" name="attend[]" />
And then you will get all the values of attend in an array and you can loop through that $_POST['attend'].
I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.
I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).
Here is the form
<center><h2>Create Event</h2></center>
<form method="post">
<?
$form = new common_functions();
if($form->check_saved_forms($my_username, "event") == TRUE){
$this->existing_forms(); //allow the user to select existing forms that they have created
}
?>
<tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
<tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
<tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
<tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>
</table>
<?
//this is the section giving me trouble
$form->add_friends($added_friends);
$form->register_tasks($tasks,$nums);
?>
</form>
Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.
Here is register (add_friend is the same, but labeled differently)
function register_tasks($tasks,$nums){
<table>
<td><input type="textbox" size="50" name="register_description"/> </td></tr>
<td><input type ="textBox" name="register_num" /> </td>
<td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
</table>
<?
if(count($tasks) > 0){
?>
<table>
<tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
<?
foreach (array_combine($tasks, $nums) as $task => $task_num){
?>
<tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
<td><input type="submit" name="remove_task" value="Remove"/></td>
<td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
<td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
<?
}
?></table><?
}
You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.