PHP/HTML: While loop with submit-buttons - php

I got a select box where i can choose a Project-ID and pass it to the next form with a submit button. Right now it looks like this:
This is my code:
<form name="form2" action="formular3.php" method="post">
<p><strong>Choose Project:</strong></p>
<select name = "project_id">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value =" . $row['project_id'] . "> Project_ID: " . $row['project_id'] . " - (" . $row['name'] . ")</option>";
}
?>
</select>
<input type="submit" value="Send" />
</form>
Now I want to have a list which has a Submit-button for every row. It should look like this:

If you are only sending one option per form, then remove the select tag entirely.
Better Method (and notice mysqli_fetch_assoc() instead of mysqli_fecth_array():
echo "<form name=\"form2\" action=\"formular3.php\" method=\"post\">";
while($row=mysqli_fetch_assoc($result)){
echo "Project_ID: {$row['project_id']} - ({$row['name']}) ";
echo "<button name=\"project_id\" value=\"{$row['project_id']}\">Send</button>";
}
echo "</form>";
This will submit the value in the clicked button without having to write so many form blocks into the html. You will only need to adjust the actual displaying of the buttons with <br>, table cells, etc.
My previous method that will work, but is not DRY:
while($row=mysqli_fetch_array($result)){
echo "<form name=\"form2\" action=\"formular3.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"project_id\" value=\"{$row['project_id']}\">";
echo "Project_ID: {$row['project_id']} - ({$row['name']}) ";
echo "<input type=\"submit\" value=\"Send\" />";
echo "</form>";
}

Use submit button inside the loop,
<p><strong>Choose Project:</strong></p>
<select name = "project_id">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value =" . $row['project_id'] . "> <input type='submit' value='Send' />Project_ID: " . $row['project_id'] . " - (" . $row['name'] . ")</option>";
}
?>
</select>
</form>

You need do stop occuring current submit event and give submit button to a id, use jquery to click on submit you should post a spicific form.
like on of the submit will be clicked, you will run this script.
$(document).on('click', '#submit', function () {
$(this).closest("form").submit();
});

Related

PHP Update always update empty string

let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data
<?php
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';
$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';
$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {
$sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";
mysqli_query($conn, $sql1) or die(mysqli_error($conn));
echo "yess";
}
?>
Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.
Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)
Add the name attribute (and fix type):
<input type='text' name='product_name' id='product_name' ...
Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.
Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).
You defined your data form in one form tag and submit form in the another form tag:
<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
When you submit your first form but your data is on another form
this is what is wrong

Display Contents of Textbox That was created by PHP

I would like some help printing what is in a certain textbox that was created by an echo command.
while($row = $result->fetch_assoc()){
$stringTest = $row['Price'];
$AssetId = $row['AssetId'];
echo "<center><div> <h3>Cost: ".$stringTest."";
echo '<form action="" method="get"><input type="text" name="uid">';
echo "</br><input class='myButton' type='submit' Name='Submit1' VALUE='I have bought'></a></form>";
/** ^ Input value I would like to get *//
echo "<a href='https://www.roblox.com/item-item?id=".$AssetId."' class='myButton'>Buy</a></h3></div></center>";
}
Use the code below to get the value from submit:
if(isset($_GET['Submit1'])) {
echo $_GET['Submit1'];
}
When the user clicks submit, it will echo the value of it.
If you want to print PHP element in a textbox you should put it in the value tag of the input
<?php
echo "<input type='text' value='" . $val . "'>";
?>

A button result in a mysqli_fetch_array cannot get through to the next page

I have a php page with this code that passes a variable of a button to next page:
<div><center><table>
while($row = mysqli_fetch_array($result))
{
echo "<td><form action= display.php method= 'post'><input type='hidden'
name='projectid' value=".$row['projectid'].">
<input type= 'submit' name= 'type'
value= 'View/Amend Project Details'></form></td>\n";
}
echo "</table></div>";
I have this on my next page in a table:
$projectid= $_POST['projectid'];
echo "<td>" . $row['projectname'] . "</td>";
I still cannot see the problem, any idea?
The problem is that you're trying to use the $row on your second page, and it isn't set there.
You have to either do the mysqli query again, or transfer the value of $row['projectname'] through the form, using a hidden input field.
<?php
while($row = mysqli_fetch_array($result,,MYSQLI_ASSOC))
{
echo " name='projectid' value=".$row['projectid']."> value= 'View/Amend Project Details'>\n";
}

multiple submit buttons in a loop php

I need to display a list of elements and after each and every element a delete button is added dynamically. Whenever the user presses a delete button the corresponding element should be deleted and rest of the list should be shown.
I have written the following php code to accomplish this:
for($i=0;$i<count($b);$i++)
{
$a=$b[$i];
echo "<li>$b[$i]</li> ";
$p="remove"."$j";
echo "<form action='' method='post'> <input class='z' type='submit' name='$p' value='delete'> </form>";
$j++;
}
if($_POST['$p'])
{
//code for deleting
}
The problem is whenever the user presses the delete button only the last element added is getting deleted and rest of the buttons are not working.Please tell me how to detect which button has been pressed dynamically and delete the corresponding element using php.
Thank you
You need to associate each button with its respective element. You'll wanna do this dynamically with an id or hidden input or something.
for($i=0;$i<count($b);$i++)
{
$a=$b[$i];
echo "<li>" . $b[$i] . "</li> ";
$p="remove" . $i;
echo "<form action='' method='post'>";
echo "<input type='hidden' name='item' value='" . $i . "' />";
echo "<input class='z' type='submit' name='delete' value='delete'> </form>";
$i++;
}
if($_POST['delete'])
{
$item = $_POST['item'];
//code for deleting $item
}
You're putting each delete button in its own form - you could also add in a hidden input with the ID to remove?
echo "<form action='' method='post'>\n";
echo "<input type='hidden' name='toDelete' value='" .$i ."'>\n";
echo "<input class='z' type='submit' name='$p' value='delete'>\n";
echo "</form>\n";
You'd then look for the element to delete with:
if(isset($_POST['toDelete'])) {
// $_POST['toDelete'] has the index number of the element to remove
}

What am I doing wrong with my PHP text boxes piece of code?

// Hy..I have the following piece of code:
mysql_select_db("baza_chestionar", $con);
$result = mysql_query("SELECT intrebari.descriere_intrebare,intrebari.nume_admin,intrebari.id_chestionar FROM intrebari WHERE intrebari.nume_admin='".$_SESSION['nume_admin']."' AND intrebari.id_chestionar='".$_SESSION['nr_chestionar']."' ");
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.")&nbsp". $row['descriere_intrebare'];
echo "<br><br>";
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >
<input type='text' size='30' name='intrebare[]'>
</form> ";
echo "<br><br>";
}
echo "<input type='submit' value='Salveaza raspunsuri' onclick='myform.submit()'/>";
mysql_close($con);
// This select some data from a table and display it and for each data a textbox. I have another page that takes the data from the textboxes and insert it in another table. Here is the code:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO raspunsuri values ('','$textbox')";
var_dump($sql);
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
// But it only inserts the first value of the first text box. What am I doing wrong? I declared the name of the text box as an array an I loop thru with a foreach statement..
You're looping the myform form as as well. The FORM tag should be taken out of the while loop and the submit button should be within the form tag (so you can use it without JS). Also, just print_r your POST data to confirm you have the data there.
Your problem is that you use more than one form tag in your page. So only one value text box is send to your second page. You must declare the form tag before the while loop and you close the tag after your loop.
Your form tag is echoed inside the loop. Put is outside.
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >\n";
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'] ."<br /><br /><input type='text' size='30' name='intrebare[]'><br /><br />";
}
echo "<input type='submit' value='Salveaza raspunsuri' name="submit" />\n";
."</form>\n";

Categories