Handling Nested Forms in HTML/php - php

I try to figure out how to handle two nested forms. The first one sends data from select-option dropdown to another_file.php. The nested one should send data from check boxes to current file and should be handled by isset($_POST['...']). Here is my simplified code:
if (isset($_POST['a']))
{
// do something
}
if (isset($_POST['b']))
{
// do something else
}
echo "<form action='another_file.php' method='post'>"; // begin of first form
echo "<table class ='table table-hover table-condensed table-striped table-bordered'>";
echo "<thead>";
echo "<th>ID</th><th>Jídlo</th><th>Množství</th><th>por_cislo</th><th>Odebrat</th>";
echo "</thead>";
while($rows = $stmt->fetch()){
echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['jidlo'];
echo "<select name =".$rows['id']."_".$rows['por_cislo']."> ";
$stmt2 = $db->query($q2);
echo "<option value ='nic'> (vyberte potravinu) </option>";
$max_por_cislo = $rows['por_cislo'];
while($rows2 = $stmt2->fetch())
{
echo '<option value="'.$rows2['id'].'">'.$rows2['jidlo'].'</option>';
};
echo "</select>";
echo "</td> <td>" . $rows['mnozstvi'] . "g <input name = '".$rows['por_cislo']."' type='text' value = '-'></td><td>".$rows['por_cislo']."</td>";
echo "<td><form action ='this_file.php' method = 'post'>"; // begin of nested form
echo "<input type='checkbox' id='atur_peg' name='idecko[]' value=".$rows['id']."*".$rows['por_cislo']." /></td></tr> ";
};
echo "<input type='hidden' name='pc' value=".$plan_cislo.">";
echo "<tr><td colspan='2'><input name = 'go' type='submit' value='OK'/></td><td colspan='2'><input type = 'submit' name ='a' value='ADD'/></td>";
echo "<td><input type = 'submit' name ='b' value='DELETE checked'/></form></td></tr>"; // end of nested form
echo "</table>";
echo "</form>"; // end of first form
Is
there any way to do this correctly?

Instead of nested forms, use jquery to detect checkbox being checked and then change the form action url. Something on this line.. You will have to modify the same to suit your needs.
$(document).ready(function(){
$("#formname").on("change", "input:checkbox", function(){
if( $(this).is(":checked") ) {
$('#formName').attr('action', 'this_file.php');
$("#formname").submit();
}
});
});

Related

PHP dynamic table

I have to make a dynamic table n*n , the user first gives the number n and the program makes a 5*5 table with check box this part I have make it, the second part is the user checks same of the checkbox and clicks on submit and the program makes again a table 5*5 but in the place of check box which checks is colored. I have uploaded and image.
Sorry for my bad English, thanks for your time.
enter image description here
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Give value">
</form>
<?php
$rows = $cols = $name = "";
if(isset($_GET['subject']))
$rows = $cols = $_GET['subject'];
if(isset($_POST['check_list']))
$name = $_POST['check_list'];
if(isset($_GET['subject'])){
echo "<form action='my.php' method='post'>";
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td><input type='checkbox' name='check_list[]' value='value ".$td."'></td>";
}
echo "<tr>";
}
echo "</table>";
echo "<input type='submit' />
</form>";
}
// this part of code is not make the third excecution the number 3 image
echo $cols;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
foreach($_POST['check_list'] as $value){
if($tr == $value[td])echo "<td bgcolor='#FF0000'></td>";
else
echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>

update mysql datarow from php form with button

I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}

Trying to create an editable HTML table using PHP and mySQL but the table won't update

I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.

HTML button running out of alignment

My idea is very simple, I will have a search box and a submit button.
When user key in the keyword and click on the submit button, results will be shown below with an additional button. Now my problem is I have no idea on how to make the button to be located at bottom right of the table populated.
Please consider the below code for my situation:
<input type="text" name="criteriaInput" style="width: 300px;"> <input type="submit" name="submit" value="GO" />
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['inquiryMethod'])){
error_reporting(0);
$sql = 'SELECT
*
FROM
table
WHERE
fullname REGEXP \''.$_POST['criteriaInput'].'\'' ;
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("mysql",$server);
$query = mysql_query($sql);
echo "<table class=\"striped\">";
echo "<tr class=\"header\">";
echo "<td>Full Name</td>";
echo "<td>ID</td>";
echo "<td>ID Type</td>";
echo "<td>Issuance Country</td>";
echo "<td>Class</td>";
echo "</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[fullname]."</td>";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[id_type]."</td>";
echo "<td>".$row[issuance_country]."</td>";
echo "<td>{$row['class']}</td>";
echo "</tr>";
}
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</table>";
}else{
echo "Please select one of the criteria!";
}
}
?>
The submit button with value "Create" did successfully created on existence of data, however it's aligned on top left of the table.
Kindly advice Thank you.
You need to put your button into a table row and cell.
echo "<tr>";
echo "<td colspan=\"5\">"
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</td>"
echo "</tr>";
Also, your form should probably move to be outside your table.
Editing to show input outside of table:
echo "</table>";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";

Submitting one value of the Hidden input in the current row clicked

How to pass one value of the current row of multiple hidden input in PHP. I have the following code:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
I also have submit button each row that triggers the form. When I click the submit button, it submits all the value of the row of the hidden input. I only want the clicked button row value.
URL is like this:
/portfolio?portfolio_id=1&portfolio_id=2&portfolio_id=3&portfolio_id=4 and so on
I only want
/portfolio?portfolio_id=3
Have a new form for each row...
<form method="get">
</form>
Like this:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo '<form method="get">';
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
If all you're submitting is that ID, and you're using GET instead of POST you might as well not even use forms and use links instead. You can still style a link to look like a button if you want, but using a link would make a lot more sense semantically. Remove the hidden input, and have each link have the URL you want.
The form tag should be included on the looping.
change this
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
to:
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<form action='process.php' method='get'>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}

Categories