I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel
For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.
Paint Edit Update Cancel
I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids.
Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?
for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{
echo "<tr>";
echo "<td width='1500'>";
echo "<input type='text' name='name' size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Update details' name='submit_update_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Delete' name='submit_delete_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;
echo "</td>";
echo "</tr>";
}
I want to do something like
foreach($_POST as $key => $value)
{
}
so that when i click on a button it performs an action depending on the category_id.
I have tried this as suggested:
echo "<input type='submit' name='submit[add_category]'" .
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";
Now in my class, i have:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
But, i am getting the error : undefined index submit
I would give the name attributes of my submit buttons using following pattern:
name="submit[which_action][which_category]"
For example for your 'Update' button for category 123:
name="submit[update][123]"
When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
Well i would use something like this:
<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>
that way when you get it to the serverside everything will be nice and tidy in nested arrays.
Related
I'm trying to create a Table where you can 'Sign Off' items populating it. Currently the table looks to be populated fine, however when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
When I say 'Sign Off' I mean to insert someone's initials into the mysql table row associated with that item. I first of course need to get the POST submission handled correctly.
That probably wasn't articulated perfectly, so please take a look at my current code;
echo "<form action='signoff.php' method='post' id='signoffform' style='display:inline;'>";
echo "<p3><table id='myTable3' class='tablesorter'>
<thead>
<tr>
<th>To Delete</th>
<th>Directory</th>
<th>Space Used</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='signoff' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
echo "</table></p3>";
echo "</form>";
signoff.php
<?php
echo $_POST["signoff"];
echo "<br />";
echo $_POST["dir"];
?>
Please feel free to request more info
... when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
The problem is because of your name attributes, name='signoff' and name=dir. When you loop through the result set, the name attribute value gets overwritten in each iteration. Hence whatever button you click, you'll always get the last row values.
So the solution would be like this:
Change your while loop in the following way,
// your code
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><input type='text' name='signoff[".$row['directory']."]' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir[".$row['directory']."]' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
// your code
And on signoff.php page, process your form in the following way,
foreach($_POST['dir'] as $dir){
if(!empty($_POST['signoff'][$dir])){
$signoff = $_POST['signoff'][$dir];
// That how you can get $dir and the corresponing $signoff value
}
}
Sidenote: If you want to see the entire $_POST array structure, do var_dump($_POST); on signoff.php page.
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
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>";
}
I have a table with 3 columns like so in index.php:
ID | StName | Inspect
_________________________________
1 Wisconsin INSPECT
2 Alabama INSPECT
3 Nebraska INSPECT
The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?
Currently I have:
//index.php
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='Submit' value='Inspect'>
</form>
</td>";
echo "</tr>";
echo "</table>";
$resultArr[0] contains the ID value I want to send to inspect.php
So add an input field inside the form with a name attribute which will be an index key name on inspect.php page
<form action='inspect.php' method='get'>
<input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
<input type='submit' value='Inspect'>
</form>
A better and simple way to go for this is without a submit, just use a hyperlink like
Inspect
And later process this id on inspect.php page
I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.
On index.php:
$resultArr = array("dave","fff","erere");
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='hidden' name='id' value=' $resultArr[0] '>
<input type='submit' name='inspect' value='Inspect'> </td>";
echo "</tr>";
echo "</table>";
And on inspect.php:
if( $_GET['inspect']){
$name = $_GET['id'];
echo $name;
}
This outputs the value of $resultArr[0], which I think you wanted.
I have on my page a dataTable with tons of rows. Each row has an id, name, surname and an action column. In the action column there is a textarea where you can add a comment and a button that submits that comment. When I submit the comment I want the page to position itself where it was, but I cannot figure it out how. Any ideas?
Here is the snippet of the code:
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='saveComment.php#position_".$row['id']."'>";
echo "<td hidden><input hidden name='id' readonly value=" . $row['id'] . " /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td><input type='submit' name='submit' value='Comment'/></td>";
echo "<a id='position_".$row['id']."'></a>";
echo "</tr>";
}
I tried passing the id of the current row to the script that saves the comment and it does pass the value, after which it should position it back where it was, but it doesn't seem to work :/
use Ajax to submit the form, and on success:
window.location = 'saveComment.php#position_<? echo $row['id']; ?>';
I found out how it works with anchors! The problem was that I didn't pass the id in the saveComment.php script when redirecting back to the original page.
Code in the page where the form is:
if ($_POST['submit']){
$id=$_POST["id"];
header('Refresh: 0.5; URL=originalPage.php#position_' . $id);
//...rest of code goes here...
}
That's it :)
You need to use
<a name='position_".$row['id']."'></a>
instead of
<a id='position_".$row['id']."'></a>