I have a table being generated from PostgreSQL through PHP.
The user needs to have the option of deleting the row or updating it.
So I have successfully implemented a delete button utilizing hidden elements to pass the key information for the deletion to my delete.php
Now I am trying to add an update button which will send the php to my update.php rather than my delete.php.
However, when I open my form I tell it where to send the data upon submission.
My question then is, is there a way that I can have both buttons available and depending on which button is pressed the form posts the data to the appropriate php- delete.php vs update.php? Perhaps some attribute of input buttons I cannot think of that can direct where to POST rather than having it as a form attribute?
Here is an example of what I have:
echo '<form action="delete.php" method="POST">';
echo '<tr>';
echo '<td class="even">' . $row['id'] . '</td>';
echo '<td class="odd">' . $row['name'] . '</td>';
echo '<td class="even">' . $row['countrycode'] . '</td>';
echo '<td class="odd">' . $row['district'] . '</td>';
echo '<td class="even">' . $row['population'] . '</td>';
echo '<input type="hidden" name="todelete" value="'.$row['countrycode'].'" />';
echo '<input type="hidden" name="cityname" value='.$row['name'].'" />';
echo '<input type="hidden" name="tablename" value="'.$_POST["search-type"].'" />';
echo '<td> <input class="odd" type="submit" name="updateBtn" value="Update?" /></td>';
echo '<td> <input class="even" type="submit" name="deleteBtn" value="Delete?" /></td>';
echo '</tr>';
echo '</form>';
This is generated for each row of the table so each of the forms is unique and only that data is sent.
Let me know if you have any ideas how I can implement the two buttons!
Simplest method is to have a SINGLE script as your action, then have a basic
<?php
if (isset($_POST['deleteBtn'])) {
... do delete stuff
} else if (isset($_POST['updateBtn'])) {
... update stuff here ...
}
Otherwise you're stuck with some javascript to dynamically change the action target.
Related
I have PHP code creating multiple forms on a single page and the submission of any of these forms should trigger an API call based on which form was submitted.
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
// $listOfApples is a list of 10 apples each with a unique ID
foreach ($listOfApples as $apple) {
echo '<form method="post" action="mypage.php">';
echo '<input type="hidden" name="appleId" value="' . $apple->{"id"} . '">';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
No matter which of the form buttons I click, the value in $_POST['appleId'] is the ID of the first apple in the list. I don't have much experience with PHP or HTML forms, is my approach completely off?
I am seeing some small mistakes as
echo '<input type="hidden" name="appleId" value"' . $apple->{"id"} . '">';
You should fix it,instead of foreach yor can use for loop to display forms,
as I haven't $listofapples,I used 10,you can query for row numbers than place row numbers value as $listofapples,then echo your unique id to input value,
Finally it's working as you wished
Check it & let me know if it's the perfect fit for you
<?php
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
$listOfApples = 10;
// $listOfApples is a list of 10 apples each with a unique ID
$sn=0;
for ($i = 1; $i <= $listOfApples; $i++) {
echo '<form method="post" action="hy.php">';
echo '<input type="hidden" name="appleId" value=" ' .++$sn. '"/>';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
?>
Here is the code that displays the table in a form where you are to insert manually only the last field.
After filling you should go ahead and save the form into the database, but since there are no specific number of input fields (as they will vary), I need to use an array to capture all the data.
$quam = mysqli_query('select * from locstock where loccode="' . $loc . '"') or die(mysqli_error());
while ($row = mysqli_fetch_array($quam)) {
echo '<tr><td>' . getItemcode($row['itemid']) . '</td>';
echo '<td>' . getItemname($row['itemid']) . '</td>';
echo '<input type="hidden" value="' . ($row['itemid']) . '" name="itemid[0][]">';
echo '<td>' . ($row['quantity']) . '</td>';
echo '<input type="hidden" value="' . ($row['quantity']) . '" name="iquant[0][]">';
echo '<td><input type="text" title="Enter Manual Count" placeholder="Enter Manual Count" name="tally[0][]"></td>';
echo '</tr>';
}
How can I retrieve the data?
You can easily count() your INPUT fields data. as your values are part of array -> name="iquant[0][]" you can easily process them. See output of print_r($_POST); after submitting your form.
I have a PHP form that updates records in a database. It looks something like this.
//update a record
$query1 = 'UPDATE mytable SET name="'.$name.'", description="'.$desc.'",
img="'.$img.'" WHERE id="'.$id.'" ';
mysqli_query($con,$query);
//get record set
$query2 = 'SELECT * FROM mytable WHERE id="'$id'"';
$result = mysqli_query($con,$query2);
echo '<form action="my-update-page.php" method="post">';
//table heading row
echo '<table width="1000" border="1" cellspacing="0" cellpadding="1">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>NAME</td>';
echo '<td>description</td>';
echo '<td>Image</td>';
echo '</tr>';
//display data
while($row = mysqli_fetch_array($result))
{
echo '<input type="hidden" name="id" value="' . $row['id'] . '" />';
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td><input type="text" name="name" value="'. $row['name'].'" /></td>';
echo '<td><textarea name="description">'.$row['description'].'</textarea></td>';
echo '<td><input type="text" size="3" name="img" value="'. $row['img'].'"/>;
echo 'Upload Image</td>';
echo '</tr>';
}
//closing tag for table
echo '</table>';
echo '<br /><input type="submit" value="submit" /></form>';
I want my upload.php page to open in a popup where the user can upload the image. I'm pretty sure I can manage doing that. Where I get stuck is after the file is uploaded, I want the popup to close and file name to show in the form input.
Modify to reflect your names, but window.opener is the link to the other window. At that point, access any elements the same way.
window.opener.forms['myform'].elements['fileinput'].value = nameOfFile;
window.close();
As the comment above says, use AJAX to do the file post.
I've looked through some posts around here, but am still struggling to get my code to work correctly....
I've got a form that is being dynamically created so I never know how many rows it will have, the number of columns is static though. Here is my current code...
echo '<form name = "confirmPlayers" enctype="multipart/form-data" action="page.php" method="POST">';
echo '<input type="hidden" name="confirm" value="1"/>';
echo '<input type="hidden" name="pg_function" value="match"/>';
echo '<table border = "1">';
echo '<tr> <td colspan = "7"> <b>Matched Members</b></td> </tr>';
echo '<tr> <td> Match </td> <td>Division</td> <td>Number</td> <td>Upload Number</td> <td>KDGA Name</td> <td>Upload Name</td> <td>Score</td></tr>';
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[]" value="1" checked>';
echo '<td> ' . $row['division'] . ' </td>';
echo '<td> ' . $row['mplayer_number'] . ' </td>';
echo '<td> ' . $row['tplayer_number'] . ' </td>';
echo '<td> ' . $row['mfirst_name'] . ' ' . $row['mlast_name'] . ' </td>';
echo '<td> ' . $row['tfirst_name'] . ' ' . $row['tlast_name'] . ' </td>';
echo '<td> ' . $row['score'] . ' </td>';
echo "</tr>";
}
echo '<tr> <td>';
echo '<input type="submit" value="Submit" />';
echo '</td> </tr>';
echo '</table> </form>';
I've tried several variations on creating the array but just seem to be creating a mess. I basically need all of the information from each cell returned in order to pass on to the database for further processing. I was hoping to return an array with a row for each corresponding row in the generated table, and then keys/indexes for each column displayed. Right now if I use a print_r($_POST['match_chkBox']); then it is returning correctly with the number of rows I left checked before submitting, but any other changes.. not so pretty.
Would appreciate any help you can give... this is driving me nuts!
try giving value unique id
echo '<td> <input type="checkbox" name="match_chkBox[]" value=value=\"".$row['id']."\"checked>';
if i were you i will post $row[id] (as defined by angel) only and fetch respective data at my php code from the database.
but if you want to take all data with you you can simply do the following..
<pre>
$i=0;
while($row = mysql_fetch_array($result)){
$i++;
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[$i]" value="'.$row['id'].'" checked>';
echo '<td> ' . $row['division'] . ' <input type="hidden" name="division['.$i.']"> </td>';
echo '<td> ' . $row['mplayer_number'] . ' <input type="hidden" name="mplayer_number['.$i.']"> </td>';
// and so on....
echo "</tr>";
}
</pre>
in since html form sent only checked checkbox to the server you can have the following code to check user selections
<pre>
foreach($_POST['match_chkBox'] as $k=>$value) {
$divi = division[$k];
$mplay = mplayer_number[$k];
//now insert into your database.
}
</pre>
hope this will help
Set the value of the checkbox to the unique key of that row, there by when the form is posted, match_chkBox[] will contain the unique keys.
echo"<input type='checkbox' name='match_chkBox[]' value=\"$row[unique_id]\" />";
Use view source to see if each rows value is set to the unique_id of that row
How do i pass id to delete record in this code?
<form action="index.php">
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
</form>
The above code is in index.php and it is submitting it to itself.
Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:
<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">
And in receiving code:
if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
//delete item with $id_to_delete
}
}
Here's how I would do it:
Change
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
To
echo '<td><input type = "button" value = "Delete" onclick = "btnDel(' . $row['id'] . ')" /></td>';
Add the following field to the form (outside of the while loop of course):
<input type="hidden" name="id" id="userid" />
Define the following javascript function:
function btnDel(id) {
if (confirm("Really delete id=" + id + "?")) {
document.getElementById('userid').value = id;
document.forms[0].submit();
}
}
Then you can retrieve that value using $_GET['id'] or $_POST['id'] depending on your form's method.
EDIT: Here's a working demo, and its source
Use this to submit the id as a part of form.
<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />
or you can send values in URL to do the same thing
An example:
Delete
A full working sample
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
switch($_GET['action']) {
case "delete":
$query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
$result = mysql_query($query);
break;
//well other actions
}
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
You could also send it in the url at action="index.php" given that you move it below while(($row = mysql_fetch_array($query)) != NULL) { like this
echo "<form action='index.php?var=" . $row['id'] . "'>";
You would then get the variable using $_GET['var']
If the id needs to stay hidden (on the page and in the link) a hidden input element and submitting the form using method="post" like previously suggested would be the better way to go.
From the table:
echo '<input type="hidden" name="user_id" value="'.$row['id'].'" />';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
This will be sent to the server as a parameter named user_id. Ideally, you should be using the POST method in the form.
This assumes that the user id is named, well id in the table.
BTW, the != NULL is unnecessary.
It's sufficient to write:
while($row = mysql_fetch_array($query)) {
NULL evaluates to FALSE in a boolean context.
Update:
As mentioned in comments to another answer, there is an issue with your approach. With multiple rows, you won't be able to distinguish between user ids.
One solution would be use multiple forms.
Another option would be to have the name of the submit button include the id. You'd then parse this out of the name of the $_POST array keys.