Send column name from button - php

I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>

you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?

Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";

Related

Differentiate between multiple buttons on a table. html php

I am creating an edit edit/delete user table and have created an 'edit' button for each record populated in the table. I want to do several things.
1. when an edit button is pressed for a specific user, open a new page called "EDIT."
2. populate form controls in the "EDIT" page with the corresponding user information for the specific 'edit' button that was pressed.
My question is, how do I differentiate between which button is pressed on the users table?
this is what my table looks like:
And this is the code for generating the table and buttons.
if (!$_REQUEST['search']) {
$sql = "SELECT * FROM users_table ORDER BY lname ASC";
$retvalues = mysqli_query($conn, $sql);
$counter = 1;
while ($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC)) {
$lname = capword($row['lname']);
$fname = capword($row['fname']);
if ($row['admin'] != 1) {
$stringAdmin = "No";
$admincolor = "<td>";
} else {
$stringAdmin = "Yes";
$admincolor = "<td style='color:red;'>";
}
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
echo "<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo "<td><input type='submit' name='edit' value='edit'></td>";
echo "</tr>";
$counter++;
}
}
You can differentiate the each edit button by using unique id
<button class='edit' data-id="<?php echo $_GET["id"];?>"> EDIT</button>.
While click on the edit button, read the data-id by using the following code
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
//code - you need to do
})
Better to show the data on next page, use bootstrap model box and AJAX request which will interact lot of the users.
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
if(id) {
$.ajax({
url : your url("profile/edit/"+id)
type : 'post',
success: function(response){
if (response.success) {
$('#modal').modal('show');
}
}
}):
}
});
If you change the value attribute of "Edit" submit button to the row id you can use this value to know what record id will be edited and populate it.
"<input type='submit' name='edit' value='edit'>"
change to
"<button name='edit' value='{$row['id']}'>Edit</button>"
Note that if the whole table is the form container, all inputs in table will be posted, not only the current row.
I would make the edit button into a hyperlink instead. Then there's no need to have form code. I'm assuming you don't need to pass the "delete" parameter to your edit page (as that's a different operation).
Instead of
echo "<td><input type='submit' name='edit' value='edit'></td>";
write
echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>";
Then in edit.php look for the variable
$_GET["id"]
and use that to search the database and display the appropriate record for editing.
P.S. If you still want your "Edit" hyperlink to look like a button it's quite easy to do that with CSS.
I did this by creating and calling a javaScript function that passes the id of the row as an argument.
echo"<tr>";
echo"<td>".$counter. ".</td>";
echo"<td>".$lname. " , " .$fname."</td>";
echo"<td>".$row['email']."</td>";
echo"<td>".$row['password']."</td>";
echo $admincolor . $stringAdmin ."</td>";
echo"<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo"<td><input type='button' name='edit' value='edit' onclick='javascript:editUser(". $row['id'].");'></td>";
echo"</tr>";
the function redirects the user to an 'edit' page while passing the value of the id in the url.
function editUser(id){
window.location = "edituser.php?id="+id;
}
once on the edit page, i used $_GET to retrieve the id value and edit my entry.
You can change and add a form to your table like this:
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
<form action="edit.php" method="post">
echo "<input type="text" name="id" value="$row['id']" style="visibilty: hidden;">";
echo "<td><input type="checkbox" name="delete"></td>";
echo "<td><input type="submit" name="edit" value="edit"></td>";
</form>
echo "</tr>"
First we have defined a form with POST method: <form action="edit.php" method="post"> and let's say edit.php will handle the request.
Then we have added a hidden form element to store and pass the user's ID: <input type="text" name="id" value="$row['id']" style="visibilty: hidden;"> It was possible to use value="..." of "edit" button but if you are dealing with passing more than one variables with POST or GET method, the trick is using an extra form element with visibility: hidden; CSS property.
Eventually, it will pass the value of $row['id'] to edit.php when the form has been submitted.
and we can handle the request in edit.php like this:
<html>
<body>
User with this ID number: <?php echo $_POST["id"]; ?> will be edited.
</body>
</html>
Official guide: http://php.net/manual/en/tutorial.forms.php
You can either use POST or GET method to pass the variables. For a detailed comparison: https://stackoverflow.com/a/504993/2104879

Radio Buttons Submit passes wrong value

I have a few Radio Buttons which shell send their value in the post method, when the submit button is clicked. I am pretty sure it is easy, but for any reason it does not work. It sends the calue of the submit button instead. Please help me. This is my code for now(this code is inside a PHP script):
echo "<form action=\"nutzerverwaltung.php\" method=\"post\">";
echo "<table [...]";
while ($row = $alluser->fetch_assoc())
{
echo "<tr>
HERE---> <td><input type=\"radio\" name=\"select_to_delete\" value=".$row["id"]."></td>
<td>".ucwords(strtolower(str_replace(".", " ", $row["username"])))."</td>
<td>".$row["username"]."#via-ev.de</td>
<td>".$row["permissionlevel"]."</td>
</tr>";
}
echo "</table><br />";
echo "<input type=\"submit\" name=\"select_to_delete\" class=\"nv_button\" />
</form>";
Putting my comments to an answer, since it was clearly the issue. As posted 17 mins. prior to my answer.
Both your radio and submit form elements hold the same name attribute.
I would call that a collision/conflict. Rename one, mainly your submit button.
You can have radio buttons holding the same name attribute as an array, but not the submit button. Elements of the same "group" can have the same name attribute.
your submit input have the same name attribute just remove that
echo "<input type=\"submit\" class=\"nv_button\" />
in html forms if you have inputs with same name the last one value will be send as your value

onclick action not working as intended with radio buttons

For the last 4 hours I've been struggling to get something to work. I checked SO and other sources but couldn't find anything related to the subject. Here is the code:
<?php
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{
$id=$oferta['id_oferta'];
echo "<input type='radio' name='selectie' value='$id' id='$id'> <a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo "</br>";
}
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
echo "</form>";
echo "</div>";
}
else
{
}
?>
The while drags all of the user's entries from the database and creates a radio button for each one of them with the value and id (because I don't really know which one is needed) equal to the entry's id from the db. I echoed that out and the id is displayed as it should so no problems there.
The delete script works ok as well so I won't attach it unless you tell me to. All good, no errors, until I try to delete an entry. Whatever I choose from the list of entries, it will always delete the last one. Note that I have two other inputs echoed out, those will be the "view" and "modify" buttons for the entry.
I really hope this is not JavaScript related because I have no clue of JS. I think this will be of major help to others having this problem. Please let me know if I need to edit my question before downrating. Thanks!
After edit:
This is the delete script, which as I said earlier works fine.
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
echo $id;
require_once('mysql_connect.php');
$query = "DELETE FROM oferte Where id_oferta = '$id'";
mysql_query($query) or die(mysql_error());
//header('Location: oferte.php');
}
else
{
//header('Location: oferte.php');
}
?>
The session is started as well, like this:
<?php
session_start();
?>
The reason the last $id is deleted is because this line is outside/after the while loop:
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
You want to move this line inside the loop so that you have a button that executes delete for each radio button.
Update:
To have links to delete and
echo "<input type='radio' name='selectie' value='$id' id='$id'> ";
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
Also I do not think the radio button is needed here at all since you are not really doing anything with it. You could simply echo out the value of your choice and have these links as follows:
echo $oferta['denumire_locatie'] . ' '; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";
The problem, in this case, is JavaScript related, yes. What I recommend you to do is to simply add a Remove link for each item.
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";
Your $id is outside your while() loop.
The last one is getting deleted because the $id has the last one's value when the loops is exited.
Include all your code :
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
Inside your while loop.
When the rendered html reaches the browser, it will be something like this:
<input type='radio' name='selectie' value='1' id='1'> <a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'> <a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'> <a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'> <a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />
With this markup you won't be able to accomplish what you want without using javascript to update the onclick attribute whenever you select a radio button.
On the other hand, instead of using the client-side onclick event you can use the button's default behaviour, which is to submit the form.
You'll just have to set the action attribute:
<form method="post" action="http://myurl.php">
and write the myurl.php page which will just read the posted variable $_POST['selectie'] and call the delete method with the posted id.

Inserting Image to MySQL Database

I am working on a solution that will help me submit specific images from a list of images to a MySQL Database.
My database consists of the following:
Database
id(INT)
photo(BLOB)
caption(VAR)
Code
I am first retreiving images from a list and giving them each a submit button.
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
$pictureImage parses the data URL and then puts it into an actual image.
The submit button is below each of those images.
I am then making it so that when the submit button is pressed, it is added to the database.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Problem
I am running into an issue where the last image in my list is the one being submitted to the database, rather than the image with the corresponding submit button. How do I make it so that it is grabbing the photo with the corresponding submit button?
Any help would be appreciated greatly.
You need to include any identifier to the image inside the form in order to store it.
For example you can try building the forms like this:
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageid' value='{$data->images->thumbnail->url}'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
And when you want to store the image on the form submit you can actually pick up the identifier of the image (in this case I used the URL you posted):
if(isset($_POST['submit'])) {
$sql="INSERT INTO $usertable (image) VALUES ('$_POST[imageid]')";
}
You are not posting any data when you click on your submit buttons.. i would suggest that for each form you would have a hidden field with the url of the image.
something like this:
<form action='tag.php' method='post'>
<input type="hidden" value="{$data->images->thumbnail->url}" name="pic"/>
<input type='submit' name='submit' value='Click Me'>
</form>
Problem
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
By the time this loop is done, you will have the last image in the loop as the value for $pictureImage.
So when it gets to this point, you're $pictureImage is still... the last image.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Solution
I'm not exactly sure what data value you want to save because right now it looks like the whole tag. But whatever it is, you need to put it into a form field first ...
foreach ($media->data as $data) {
..
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageurl' value='<img src=\"{$data->images->thumbnail->url}\">' />";
..
}
and then retrieve it in the POST part of your script:
if(isset($_POST['submit'])) {
$imageurl = $_POST['imageurl'];
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$imageUrl')";
}
This way, it won't always be the last value in your list, rather, it will be the one you selected.

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

Categories