Create multiple forms in a html table - php

I've got a MySQL database and a table called courses. I would like to show the records of this table within an html table, but I would also like to edit them with a submit button at the end.
I know this has been asked before, but I'm still struggling with how I should properly proceed.
I currently have:
<tbody>
<?php
foreach ($db->getResultSet() as $row) {
extract($row);
?>
<tr>
<form id="myTable" name="editrecord" action="course.php" method="post">
<input type="hidden" name="code" value="<?php echo $code; ?>" /><!--PrimaryKey-->
<td><?php echo $code; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
<td><input type="text" name="desc" value="<?php echo $desc; ?>" /></td>
<td><input type="submit" name="Edit" value="Edit!" /></td>
</form>
</tr>
<?php
}
?>
</tbody>
Now this seems to work fine. However I'm aware that <form> should not be inside <tr> or anywhere else other that <td>
I'm aware I could use various divs and set display properties to table layout etc. however I'd like to avoid this as I am using pre-made javascript that can sort and filter data which requires me to use <table>
I also tried a for loop outside the table to create 1 form per record and use the primary key as the form's id. Then proceed with echo "<td><input form='$code' .... But this doesn't work in Explorer
What is the correct way to implement something like this?

Related

MySql datetime field in HTML/php table is only showing date when using form

I have a datetime MySql field named down_stamp that I have populating an HTML table via php:
<? $sql_el = "SELECT * FROM events WHERE loc_id = '$lid'";
$result_el = mysql_query($sql_el);
while ($row_el = mysql_fetch_array($result_el)) {
?>
<tr>
<td><?=$row_el['down_stamp']?></td>
</tr>
</form>
<? } ?>
This produces results in the format I'd like: 2013-09-15 08:30:00. However, I'd like to make this field modifiable, so if I just convert it into a form element like this:
<form name="fm_editevent" method="post">
<tr>
<td><input name="down_stamp_ed" required="required" value=<? echo $row_el['down_stamp']; ?> type="text"></td>
<td><input value="Update" type="submit" name="update_el"></td>
</tr>
</form>
<? } ?>
Then the down_stamp field only displays the date: 2013-09-15. I don't think it's a width issue as there's plenty of room in the text box. I tried adding <td width=100> (twas a guess). I also tried to declare the time in php when expanding the variable, which just gave me an error and the date 1970-01-01 01:33:33
<td><input name="down_stamp_ed" required="required" value=<? echo date("Y-m-d H:i:s", $row_el['down_stamp']);?> ></td>
How can I get the full datetime field data to echo in this form text box?
You missed the quotation marks...
Use:
<td><input name="down_stamp_ed" required="required" value="<? echo $row_el['down_stamp']; ?>" type="text"></td>
(without the quotation marks the result would look like this:
<td><input name="down_stamp_ed" required="required" value=2013-09-15 01:33:33 type="text"></td>
and just because you're lucky your browser interprets "2013-09-15" as the value of the field ;-)
You need to wrap value in " :
<form name="fm_editevent" method="post">
<tr>
<td><input name="down_stamp_ed" value="<? echo $row_el['down_stamp']; ?>" type="text" required></td>
<td><input value="Update" type="submit" name="update_el"></td>
</tr>
</form>
<? } ?>
You need quote the value attribute, e.g:
echo "< .... value=\"$date_from_db\">";
Without them, you're generating
... value=2014-10-21 15:16:00
which is parsed as an attribute "value" whose value is 2014-10-21, and an unknown/illegal attribute "15:16:00".

PHP POST value from a looped table

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

Is this possible to accomplish with html and php?

I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>

Getting specific table row based on id mysql DB

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]"?>" ;)

Trouble with dynamic form values with PHP

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.

Categories