php - How to make a unique $_POST to a form action? - php

I'm having a trouble finding a solution to this. Here is the case, assume that I have 5 books in the database, i will display them by doing a while then mysql_fetch_array assume that I create a table. This is the plot
echo"
<td>
<form action='bookext.php' method='post'>
<input type='submit' value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
the $row['book_title'] works fine because it must display what is the title of the book in the database. But, how can I get it's unique value which is book_id and send it to bookext.php?
PS: Sorry for my title, I can't pull the right english

First of all your current input needs a name attribute:
"<input type='submit' name="title" value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
Since you're using the POST method this will insure the value is available in the $_POST array, like $_POST['title'].
For the id you can add a hidden input to your form:
"<input type='hidden' name='book_id' value='".$row['book_id']."'>"
This will be available to you in $_POST['book_id'] when you submit.

Just insert hidden field to your form
<input type="hidden" name="POST_NAME" value="YOUR_VALUE_TO_IDENTIFY">

Don't use forms in tables.
<form action='bookext.php' method='post'>
<table>
<tr>
<?php foreach($books AS $book){ ?>
<td>
<button type="submit" name="bookId" value="<?php echo $book['id']; ?>">
Title of the book
</button>
</td>
<?php } ?>
</tr>
</table>
</form>
bookext.php
<?php
$id = $_POST['bookId'];

Related

Delete data from database using php and sql

So I have a php page that looks like this
<?php
echo "<table border='1' width= 300px >
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>";
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
echo "<tr>";
echo "<td>" . $final3['profile_name'] . "</td>";
echo "<td>"
?>
<form action="friendlist.php" method= "POST">
<button id="add-friend-btn" type= 'submit' name = 'submit'>Unfriend</button>
</form>
<?php
"</td>";
echo "</tr>";
}
echo "</table>";
When I press the button, I need the corresponding name to delete it. The only problem I'm facing is that How do I get the name corresponding to the button.
I think we need to relate the buttons and the name somehow, so when a specific button is pressed i get the corresponding name
From the question and comments, and a glance at your code it sounds like you probably actually need two pieces of data to be submitted to the server when your button is clicked:
The action which should be undertaken in response to the request (i.e. unfriending)
The ID of the person being unfriended
To achieve that you can add some hidden fields to your form. These are invisible to the user but will be available to PHP in the $_POST data when the form is submitted.
Something like this:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="unfriend_id" value="<?=$friend_id_got ?>" />
<input type="hidden" name="action" value="unfriend" />
<button id="add-friend-btn" type="submit" name= "submit">Unfriend</button>
</form>
Following on from the comment from #ADyson:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="cancel_id" value="<?=$friend_id_got ?>" />
<button id="add-friend-btn" type="submit" name="submit">Unfriend</button>
</form>
By including a hidden field in the form, you're able to store more information.
You can see that I'm storing the ID of the friend you're unfriending in the value of the hidden field, so when the form is submitted (the button is clicked) you'll have access to "cancel_id" in the POST data, which will obviously contain the ID of the friend to unfriend.

How can i edit the perticular row of HTML table using edit button in PHP?

The following code is for fetching the data from database and showing them into HTML table
<?php
//Database connect file.
include 'connect.php';
$get_detail=mysqli_query($con,"SELECT * FROM Wheel_Tag");
if ( mysqli_num_rows($get_detail) > 0)
{session_start();
echo"<br><br><br><br>
<table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'>
<thead>
<tr>
<th>tag_title</th>
<th>description</th>
<th>show_in_welcome</th>
<th>status</th>
</tr>
</thread>
<tbody>";
while($row = mysqli_fetch_assoc($get_detail))
{
$_SESSION['tag_title'] =$row['tag_title'];
$_SESSION['description'] =$row['description'];
$_SESSION['show_in_welcome'] =$row['show_in_welcome'];
$_SESSION['status']=$row['status'];
echo
"<tr>
<td>{$_SESSION['tag_title']}</td>
<td>{$_SESSION['description']}</td>
<td>{$_SESSION['show_in_welcome']}</td>
<td>{$row['status']}</td>
<td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>
</tr>\n";
}
}
?>
And here is my edit.php to edit the row
<?php
session_start();
echo"<form action='tag_show.php' method='post'>
<div id='header'>
<h1>Edit Your Profile</h1>
</div>
<div style='float:right; width:63%;'>
<br><br><br>
Tag Title<br><input name='tag_title' type='text' value='{$_SESSION['tag_title']}'><br><br>
Desciption<br><input name='description' type='text'value='{$_SESSION['description']}'><br><br>
Show in welcome<br><input name='show_in_welcome' type='text' value='{$_SESSION['show_in_welcome']}'><br><br>
Status<br><input name='status' type='text'value='{$_SESSION['status']}'> <br><br>
<input name='submit1' type='submit' value='Update'>
</form>";
?>
My problem is when I click on edit button the respective row value not come in the form of edit.php, instead of the last row value come by using session. Can anyone please solve my problem. I want the particular row value when I click on edit button.
You should change the form on you listing to just a link:
<td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>
to:
<td><a href='edit.php?id={$row['tag_title']}'>{$row["tag_title"]}</a></td>
and than on the edit.php page make a mysql query to get the details about that single Wheel_Tag that you are loading, something like:
SELECT * FROM Wheel_Tag WHERE tag_title = {$_GET['id']}
and display the elements.
If you want to save everything on SESSION so you don't make another request for the single Wheel_Tag on edit.php than you would need to store an array in $_SESSION which stores all the Wheel_Tags and than link the edit.php with the tag index on that array.
In general, there are a lot of issues with your code in regard to best practices. I would suggest you to start reading a book or a course about php and web in general.

How to get the particular row value when click on edit button in php?

The following code is in tag_show.php.I have to show all the data from the database and I am trying to edit the database from php code so i form the edit button at the end of all row in html table.
echo"<br><br><br><br>
<table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'>
<thead>
<tr>
<th>tag_title</th>
<th>description</th>
<th>show_in_welcome</th>
<th>status</th>
</tr>
</thread>
<tbody>";
while($row = mysqli_fetch_assoc($get_detail))
{
$_SESSION['tag_title'] =$row['tag_title'];
$_SESSION['description'] =$row['description'];
$_SESSION['show_in_welcome'] =$row['show_in_welcome'];
$_SESSION['status']=$row['status'];
echo
"<tr>
<td>{$_SESSION['tag_title']}</td>
<td>{$_SESSION['description']}</td>
<td>{$_SESSION['show_in_welcome']}</td>
<td>{$row['status']}</td>
<td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>
</tr>\n";
}
and the edit.php is
<?php
session_start();
echo"<form action='tag_show.php' method='post'>
<br><br><br>
Tag Title<br><input name='tag_title' type='text' value='{$_SESSION['tag_title']}'><br><br>
Description <br><input name='description' type='text' value='{$_SESSION['description']}'><br><br>
Show in welcome<br><input name='show_in_welcome' type='text' value='{$_SESSION['show_in_welcome']}'><br><br>
Status<br><input name='status' type='text' value='{$_SESSION['status']}'> <br><br>
<input name='submit1' type='submit' value='Update'>
</form>";
?>
but the $_SESSION returns the last row value in HTML table. But i want the receptive row value when i click on edit button.
You don't need a session variable to pass the value to edit.php file.
Hope there is a unique key to identify the record from database. I assume it as recordid. Pass this value to edit.php as a query string
tag_show.php
$output = '';
while($row = mysqli_fetch_assoc($get_detail)){
$output '<tr>
<td>'.$row['tag_title'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['show_in_welcome'].'</td>
<td>'.$row['status'].'</td>
<td>edit</td>
</tr>';
}
In edit.php, you can get the recordid from query string and retrieve the data from database to populate the form
<?php
if($_GET['editid']!=''){
//get record corresponding to the 'editid' from database
//I assume the record is for and store in a variable $editdata
?>
<form action="tag_show.php" method="post">
<br><br><br>
Tag Title<br><input name="tag_title" type="text" value="<?=$editdata['tag_title']?>"><br><br>
Description <br><input name="description" type="text" value="<?=$editdata['description']?>"><br><br>
Show in welcome<br><input name="show_in_welcome" type="text" value="<?=$editdata['show_in_welcome']?>"><br><br>
Status<br><input name="status" type="text" value="<?=$editdata['status']?>"> <br><br>
<input name="submit1" type="submit" value="Update">
</form>
<?php
}
?>
Looks like you are never updating the session data with the information received from the form. You will need something like this somewhere in your code.
$_SESSION['tag_title'] = $_POST['tag_title'];
$_SESSION['description'] = $_POST['description'];
$_SESSION['show_in_welcome'] = $_POST['show_in_welcome'];
$_SESSION['status']= $_POST['status'];
A few things to consider.
Make sure your db info does not overwrite the values after user submits form.
Always sanitize/validate user input before using it (unlike the above code, since it's just an example).
here session make no sense. each time loop runs previous value replaced therefore you are getting last value. Instead of you can use the concept of query strings. Instead of Printing Form print a anchor tag
<a href='file.php?id='.$row["tag_title"]>Edit</a>
Being your tag_title unique.
if(isset($_GET['id))
{
$id = $_['id']
// Your code based on id
}
Now using id run mysql query and select desired data and display in form
Certainly your code will return the last row as the session array overwrites the previous row's value after every iteration.
Make the session array as multi dimensional like $_SESSION[$id]["tag_title"] and then in your edit.php refer to the session array using the id value.
$_SESSION[$id] = array('tag_title' => $row['tag_title'], 'description' => $row['description'], 'show_in_welcome' => $row['show_in_welcome'], 'status' => $row['status'])
Then increment the $id after each iteration.

Using an Update SQL query in a foreach loop in PHP

I am trying to create a table showing multiple users data. The data in the table will then be able to be edited and updated. Below is an example of the way the form is laid out:
echo "<form action=AdminUpdateLecInfo.php method=post>";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo " <tr class='data'>
<input type='hidden' name='id'".$return['ID']."'' value= '".$return['ID']."' />
<td class = 'title'><input class = 'title' type='text' name='title'".$return['ID']."'' value= '".$return['Title']."' /></td>
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
Once the table is created the information is passed to the 'update.php' script.
$sql="UPDATE completeinfo SET Title='".$_POST['title'][$return['ID']]."'
WHERE ID = '".$_POST['id'][$return['ID']]."'";
header("Location:Home.html");
The problem I'm having is that I need to add the '".$return['ID']."' to the name of each input field so that not all users details are updated with the same values. I am unsure if I need to apply a foreach loop around this query so that it applies to each user and updates their details. Currently however the update query is not working presumably because the post method is not fetching the values from the form correctly.
Your problem is the name of your fields, use that :
$return_id = $return['ID'];
echo "
<tr class='data'>
<td class = 'title'>
<input type='hidden' name='id$return_id' value='$return_id' />
<input class='title' type='text' name='title$return_id' value='$return_id' />
</td>
</tr>";
Before doing your mysql update, do var_dump($_POST);, you'll be shown the content of the HTTP POST parameters so you can see what they are and how to use them in the query.
You have to use the names of your text fields as array so you can iterate your array
name='title'".$return['ID']."'
name='id'".$return['ID']."'

getting values from table when checkbox is checked php

I have this code to show my table:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<table cellspacing='0'>
<?php
if(isset($_GET["ordem"])){
if($_GET["ordem"] == 'descendente'){
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação</th>";
echo "<th>Enviar mail</th>";
echo "</tr></thead>";
}
elseif($_GET["ordem"] == 'ascendente'){
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=descendente'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação </th>";
echo "<th>Enviar mail</th>";
echo ("</tr></thead>");
}
}
else{
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=ascendente'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação</th>";
echo "<th>Enviar mail</th>";
echo("</tr></thead>");
}
while($stmt->fetch()){
echo("<tbody>");
echo("<tr><td>$nomeUser</td>");
echo("<td>$email</td>");
echo("<td>$nomeVoucher</td>");
echo("<td>$categoria</td>");
echo("<td>$preco</td>");
echo("<td>$confirmacao</td>");
$content = file_get_contents($file,$filePDF);
echo("<td><INPUT TYPE='checkbox' NAME='mail[]' multiple='yes'></td>");
echo("</tr>");
echo("</tbody>");
}$stmt->close(); ?>
I have a checkbox in my table and I would like to know how can I get the values of each rows from the table when i selected the checkbox. I want to send email when the user selected multiple checkbox.
Thanks
It's possible that the code above is a snippet of a larger page, but if not:
You aren't actually wrapping your input elements in an HTML form tag. Doing so will cause the user agent (presumably a browser) to treat each input tag as something to be submitted to your backend form.
You should wrap the tables in a table element; tbody is a child of a table.
Regardless of the above:
It looks like your PHP code above will render the entire tbody each time the statement fetches a new row, which is a bit weird. I'd assume you only want to render a row containing mail options?
To the best of my knowledge, there is no multiple attribute allowed in a checkbox element. You may be thinking of the select element.
You are not setting a value attribute on your checkbox input tag. If the user actually submits a form, you'll either get a set of empty mail[] variables, or nothing at all, I'm not actually sure which.
Consider the code below: Note that the checkboxes have the same name attribute, but different values.
<form method="post">
<table>
<tbody>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val1" id="mail-val1" />
<label for="mail-val1">Value 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val2" id="mail-val2" />
<label for="mail-val2">Value 2</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val3" id="mail-val3" />
<label for="mail-val3">Value 3</label>
</td>
</tr>
</tbody>
</table>
<input type="submit" />
</form>
Given this form, if the user selects all three of the checkboxes and submits, your server will receive a POST request with the payload:
mail[]=val1&mail[]=val2&mail[]=val3
Depending on how PHP parses that response (it's been about a decade since I've dealt with PHP), you'll probably have an array variable accessible to your application:
# mail == ["val1", "val2", "val3" ]
Hope this helps.

Categories