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>
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.
I'm creating a table and when the user clicks on the button I want it to open a php file called player_profile, where that page will call up more information on that particular player.
When it comes to passing variable through pages I'm aware that something along these lines will work.
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Yet when I add it to the button the php file wont load. Below is my code without passing a variable that works.
<?php
$result = mysql_query("SELECT *, CONCAT(FirstName,' ', LastName) AS Name FROM Player WHERE TeamID = '$TeamID' ORDER BY LastName ASC");
echo "<table id='customers' border='1'>
<tr>
<th>PlayerID</th>
<th>Name</th>
<th>Position</th>
<th>Tries</th>
<th>Tackles</th>
<th>Turnovers</th>
<th> Info </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["PlayerID"] . "</td>";
echo "<td>" . $row["Name"] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td>" . $row['Tries'] . "</td>";
echo "<td>" . $row['Tackles'] . "</td>";
echo "<td>" . $row['Turnovers'] . "</td>";
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
?>
The code above works perfectly at displaying a table and taking me to the player_profile page. But if change the button to the code below the page so that I can pass a variable it doesn't load
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
$_SESSION['varname'] = ".$row['PlayerID'].";
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
Don't use the session to pass parameters. It breaks the way users expect a page works.
Use $_GET parameters:
John
Then in player_profile.php do:
$id = isset($_GET["id"]) ? $_GET["id"] : false;
if ($id === false) {
exit("missing input");
}
// etc
The problem with using a session here is that if someone opens two different player profiles what can he expect to see? What if he refreshes one of them?
A more advanced and involved way of parameter passing uses URL Rewriting to get nice looking URLs like:
John
You have code trying to set a session inside your echo. You want it separate:
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."'>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
If you want to access the PlayerID variable on the player_profile.php you just use:
$var_value = $_GET['id'];
You could also put it into a session var at that point, but I don't know why you would need that.
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";
}
I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.
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.