Form element closing too soon - php

I have a PHP script which generates a table with each row (supposedly) as a form. The table is generating very nicely, but for some reason the form tag is closing immediately. I have no idea why and it's making my cranky. Any ideas?
echo '
<form method="post" action="#" id="status_row_'.$row["name"].'">
<tr class="even">
<input type="hidden" name="category" id="category" value="'.$category[0]['category'].'">
<input type="hidden" name="error_level" id="error_level" value="'.$row['level'].'">
<input type="hidden" name="service_name" id="service_name" value="'.$row['name'].'">
<td class="status-icons" name="error_circle"><div class="'.$error_circle.'"></div></td>
//rows deleted here for clarity's sake
</tr></form>'
So the Inspect Element on the generated form shows: <form method="post" action="#" id="status_row_blahblah"></form> followed by all the rows.

Your html is broken. You're trying to open a form immediately after the opening <tr>, then closing it AFTER the closing <tr>. That's illegal html. <table> structures cannot be built like that. It should probably be something more like
<form>
<table>
<tr>
<td>...</td>
</tr>
</table>
</form>
Essentially you're trying to stuff your form into the void that exists between <tr> and <td> tags. Nothing should be there except the table structure:
<tr><p>hi there</p><td>go away</td></tr>
is illegal, most browsers will render the <p> OUTSIDE the table, usually before the opening <table> tag. That's why your form is showing up where it is - the browser is attempting to do the best it can with your broken html.

Are the forms different? Or linked together in some way (i.e. is each table row it's own UNIQUE form with, or is it one giant form with different fields in every row?
If it's the former, then create a form inside each ROW element like so:
<table>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
</table>
If it's the latter, or if you only want one form submitted overall, then create the form tags outside the table, and have the form fields within the data cells:
<form>
<table>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
</table>
</form>

It looks like you are closing the <tr> tag before closing the <form> tag.

The form element was closing incorrectly because, as some people pointed out, you apparently can't have <form> tags in between <tr> and <td> tags (I don't understand why, but it is what it is).
I found this solution as a workaround:
http://federmanscripts.com/2010/01/12/form-and-table-row-nesting-workaround/

Related

When submitting this button inside a datatable doesnt submit the right row id

I have a dynamic table which is set inside a foreach, so for each item of the array fetched create a new row. I have in the last column a button for each row. When clicking that submit button I am suppose to receive the id of that in PHP. Submission is being done correctly, but I am receiving the wrong id in PHP. Its basically taking the last id of the array when submitting. Any idea why?
Here is the table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
When clicking on see_rma this is what I receive in PHP:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
If I try printing $cases["Dev_Id"]; inside loop in the table, it prints perfectly fine, so it prints the Dev_Id of each row correctly. So, that means there is nothing wrong with the array or data. I don't why is this happening but it's for sure the first time I am having this issue.
I do this in many other tables but for some reasons in this one its not working properly.
You have multiple <input> elements with the same name within your form, and all of them are going to be submitted when you submit the form, but PHP can only get one of them. That's why you end up with only the last one in $_POST.
It looks like you should be able to fix this by just moving some attributes from the hidden input into the button (replacing the hidden input).
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
Only the button that was clicked will be submitted. Note that after changing the name of the button, you won't have see_rma in $_POST any more, so if you have any code that depends on that you'll need to change it to look for the other name instead.

Send "Button"-ID on Submit

I have an PHP page, which contains a form with some different input fields, e. g. day, month, year etc.. The form method is POST, only one non-editable field (The user ID) is sent via GET.
Of course, there is a "Submit"-Button, which triggers the form Action (PHP Script on Server).
The form tags contain a table with empty cells too. Now comes my question:
If the user clicks into one of the table cells, the form should be submitted, but additional to the regular form data the ID of the table cell should be transmitted too (If via POST or GET doesn't matter to me). How can I do that?
//Edit 2:
...
<form method="post" action="<?= DOMAIN?>/.../addUserTimetable.php?uid=<?= $user->getUserID() ?>">
<select id="day" name="day">
...
</select>
...
<input name="yearend" id="yearend" ...>
<button type="submit">...</button>
<table class="bordered">
<tr>
<th>Std.</th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
</tr>
<?php
for($i=1; $i<13;$i++) {
echo "<tr>";
echo "<th>".$i. "</th>";
for($j=1;$j<6;$j++) {
echo "<td id='h".$i. "d".$j. "' onclick='???'></td>";
}
echo "</tr>";
}
?>
</table>
</form>
...
The server sided procession is fine, but I haven't got any ideas - even after two hours google - how I could transmit the cell id additionally.
That shouldn't be to hard. Have a look at the following example:
<form>
<input type="text" name="something">
<table>
<tr>
<td><input type="submit" name="cel1">
</tr>
<tr>
<td><input type="submit" name="cel2">
</tr>
<tr>
<td><input type="submit" name="cel13">
</tr>
</table>
<input type="submit" value="save">
</form>
By giving the submit buttons in the table cells a name attribute, that name will also be present as a key on the $_REQUEST. Go ahead and var_dump the $_REQUEST and you'll see you can find out in the backend which button got pushed by checking which key exists.
Note that POST / GET is completely irrelevant here, both will work just the same. And obviously you could apply some css to those buttons to make them transparent and lay them on top of the table cells, so they don't look like buttons, but just "capture" the user's click.
One last side note, are you sure you want to send the userID as a GET parameter? That would be very easy for someone with bad intentions to manipulate. Consider not sending the ID at all, but keeping it in the session on the server.

Input hidden doesn't work properly

I'm developing a simple PHP page, this is a small part of my code that doesn't work properly.
I want to use DB and print some products and for each product, I want to show a "buy" button.
If I press this button, a hidden input must be set with the product id(which later has to be sent to another page).
But, if I use var_dump to control(if my data was correct), I can see that the ID is wrong (I see that its always shown in the last)
<form id="products_list" method="post" action="step2.php">
<table>
<tr>
<td align="center">Immagine</td>
<td align="center">Nome prodotto</td>
<td align="center">Descrizione prodotto</td>
<td align="center">Prezzo unitario</td>
<td align="center">Taglie disponibili</td>
<td align="center">Colori disponibili</td>
<td align="center">Nickname disponibili</td>
</tr>
<?php
$product = mysqli_query($mysqli, "SELECT * FROM products");
while ($row = mysqli_fetch_array($product)) {
$id = $row[id_products];
print("
<tr>
<td align=\"center\">".$row[img_products]."</td>
<td align=\"center\">".$row[name_products]."</td>
<td align=\"center\">".$row[description_products]."</td>
<td align=\"center\">".$row[price_products]."</td>
<td align=\"center\">".$row[size_products]."</td>
<td align=\"center\">".$row[color_products]."</td>
<td align=\"center\">".$row[nick_products]."</td>
<input type=\"hidden\" name=\"id_products\" value=\"".$id."\"/>
<td><input type=\"submit\" name=\"buy\" value=\"Acquista\"/></td>
</tr>");
}
?>
</table>
</form>
You should open and close your form within the while-loop.
EDIT
A little bit more explanation why you should open and close from within the while loop:
If the while-loop is inside the form tags, that means the hidden field is outputted multiple times within the same form. Since they are all named the same, you're only retrieving one value after the submit (the value of the last hidden input).
If you open and close the form within the while-loop, each hidden input and button are in their own form. Which means, when that form gets submitted, you're only retrieving the value of that specific hidden field. :-)

Multiple input types with same name to be received via php

I'm developing an application where the user inputs the number of children he/she is the parent off and then he/she is taken to a page where he/she enters the values of the no. of children.
Suppose there 2 children, then there would be 2 same forms with the same id for input boxes. The question is, how can I get the values simultaneously via php? Here's the code I've written, but it doesn't seems to be working correctly.
HTML
<form action="test.php" method="post">
<table>
<tr>
<td>Name 1 </td>
<td><input type="text" name="name[]"></td>
</tr>
<tr>
<td>Name 2</td>
<td><input type="text" name="name[]"></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
(2 children, so there would be two names)
PHP
<?php
$names=$_POST['name[]'];
foreach($child as $names)
{
echo $child;
}
?>
What can I do to make it work perfectly?
What I actually want is, the names from both the textboxes should be available.
Since we don;t know how many number of children are there, we need to have one single form which would get information for all the children.
Thanks.
Try
$_POST['name']
instead of
$_POST['name[]']
You have the foreach wrong as well
Should be:
<?php
$names=$_POST['name'];
foreach($names as $child)
{
echo $child;
}
?>

Using jQuery, I am submitting a form wihout refreshing the browser and I need help in listing the submitted data in a custom table column

I have a table where it's rows are generated by a foreach loop.
Each table row has two columns, one with a form and the other with the forms submitted data, which I have called notes. (and updates using a jQuery library without refreshing the browser).
<script>
$(".notes_column").each(function(){
var $myform = $(this).find('.notes_form');
var $mynotes = $(this).find('.notes');
$myform.validate({
submitHandler: function(form) {
$.post('process.php', $myform.serialize(), function(data) {
$mynotes.html(data);
});
}
});
}); // each function
</script>
<table id="myTable" class="tablesorter" border="1" cellpadding="5">
<thead>
<tr>
<th>Notes</th>
<th>Submit Note</th>
</tr>
</thead>
<tbody>
<?php
foreach($myarray as $myvar){
echo '
<tr>
<td>ID LIKE TO PLACE THE content of the div class="notes" HERE, HOW CAN I DO IT?</td>
<td class="notes_column">
<form class="notes_form" action="" method="post">
<input class="q" type="text" name="notes" size="30" placeholder="Place your notes here..." />
<input class="searchsubmit" type="submit" name="submit" value="Submit" />
</form>
<div class="notes"></div>
</td>
</tr>
';
}
?>
</tbody>
</table>
Right now I use a jQuery each function which iterates through each table column, and for each column with the class name ".notes_column" populates a div with the class "notes" with the submitted data.
The question is listed inside the code with capitalized letters, how can I populate the other column with the forms submitted data?
Any ideas?
Ty
I would create a <div> in the first <td> such as
<td>
<div class="put_notes_here">ID LIKE TO PLACE THE content of the div class="notes" HERE, HOW CAN I DO IT?</div>
</td>
Then populate it the same as you did before
$mynotes.closest("tr").find(".put_notes_here").html(data);
Change the selector for your each to $('#myTable tr'), then you can iterate through each in your table.
Then you could either do an each on the tr object to access each td, or make use of jQuery's :first-child and nth-child(2) etc in a subsequent selector

Categories