Linking html button click - php

$result = mysql_query('SELECT * FROM teams');
while($row = mysql_fetch_array($result))
{
$team= $row['team'];
$goals= $row['goals'];
$squadsize= $row['squadsize'];
$league= getTheirLeague($team);
echo $team. "</br>";
echo "Goals Scored " . $goals. "</br>";
echo "League " . $league. "</br>" . "</br>";
<form method="POST" action="football.php">
<button type="button">Edit</button>
<button type="button">Remove</button>
</form>
}
I am trying to add a remove and edit feature to my teams, for every team i print out i have a form being printed with 2 buttons. What i am unsure of is how i can tie the button clicks up to the definitive team that the button belongs too.

As each row (team) gets its own form, simply add a hidden field with the team identifier (assuming $row['team'] for this example).
Please note that IE has terrible <button> support in forms. I'd advise using submit inputs...
<form method="POST" action="football.php">
<input type="hidden" name="team"
value="<?php echo htmlspecialchars($row['team']) ?>">
<input type="submit" name="edit" value="Edit">
<input type="submit" name="remove" value="Remove">
</form>
You can then tell which team form was submitted by checking $_POST['team'] and which button was pressed using...
if (isset($_POST['edit']) {
// edit clicked
}
if (isset($_POST['remove']) {
// remove clicked
}

Related

How can I call dynamic ISSET by multiple ID button?

As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).
This is my code:
<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >
<?php
if (mysqli_num_rows($result) > 0)
{ ?>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
<div class="card-body">
<input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</div>
</form>
And this is the process
<?php
if(ISSET($_POST['mo'])){
$demandeur = $_SESSION['username'];
$model = $_POST['model'];
$chassit = $_POST['chassit'];
$sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
VALUES ('$demandeur','$model','$chassit')";
if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
?>
The input name="model" it insert always the last value !!!
Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:
<label class="card-body">
<input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</label>
Please note:
you never use $_POST['mo'] value that should contain selected model
You have same ID in every iteration, and that is invalid HTML
Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.
Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.
There are a few ways I can think of to make this work:
Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.

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 to store HTML form value for other pages in PHP and MySQLi?

I have created a database in phpMyAdmin with table for topics such as Math, Physics etc. Each topic has a topic_id as primary key in table tb_topic. Each topic can have multiple videos. Admin can add videos to each topic which requires topic_id as foreign key in table tb_video_management to store videos against a topic.
Backend:
There is a button to add videos in form.php
form.php:
<?php
$query = "SELECT topic_id, topic_name,aid FROM tb_topic";
$result = $con->query($query);
while($query_row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><form action='edit.php' method='get'>
<button type='submit' name='edit' id = 'edit' value='<?php echo $query_row['topic_id'];?>' class='btn btn-success'>EDIT</button><br />
</form></td>
</tr>
<?php
} ?>
When this button is clicked, page is navigated to another page "edit.php" which has 2 buttons, one for add new video and second for viewing video gallery.
edit.php:
$id = $_GET['edit'];
echo 'topic id----> ' . $id;
<form action='add_video.php' method='get'>
<button type='submit' name='add' id = 'add' value='<?php echo $id;?>' class='btn btn-success'>ADD NEW VIDEO</button>
</form>
<form action="video.php" method="get">
<button type="submit" name="goback" id="goback" value="submit" class="btn btn-primary">VIEW GALLERY</button>
</form>
The class where videos are added has a form for topic name, details etc.
add_video.php
$id = $_GET['add'];
echo 'topic id----> ' . $id;
<form action="add_video.php" method="post">
Video Title: <input type="text" name="video_name"><br />
Video Detail: <input type="text" name="video_detail"><br />
Video Question: <input type="text" name="video_question"><br />
Video Link: <input type="text" name="video_link"><br />
<input type="submit" name="submit" value = "Submit"><br />
</form>
When "submit" button is clicked, following code executes:
if(isset($_POST['submit'])) {
if(!(empty($_POST['video_name']) || empty($_POST['video_detail']) || empty($_POST['video_question']) || empty($_POST['video_link']))) {
$name = $_POST['video_name'];
$detail = $_POST['video_detail'];
$question = $_POST['video_question'];
$link = $_POST['video_link'];
$insert = "INSERT INTO tb_video_management(topic_id, video_name, video_detail, video_question,video_link) VALUES ('$id','$name','$detail', '$question','$link')";
if(!mysqli_query($con,$insert))
echo "error";
header('location:edit.php');
}
}
The problem I am facing is, when I click submit button, the value in $id is lost (may be because another button (submit) is pressed) and it fails to insert the record as there is no value for "topic_id" anymore. I cannot resolve this issue of keeping foreign key value even after pressing submit button.
So far I have been creating extra table to hold value for topic_id which is definitely not the right approach.
To use the $id into another page, you must have to store that in such a way that it will be remain exist after submit.That can be done by using the hidden field.
You may set the value of $id like:
<input type="hidden" name="topic_id" value="<?php echo $id?>">
Once you submit and redirect to add_video.php, you may get the value of $id same as another field video_detail.
On submit look like:
if(isset($_POST['submit'])) {
$topic_id = $_POST['topic_id'];
}

Can't pass hidden form value from database to PHP if-statement

My database table (Related) contains 3 columns:
related_id
article_id
object_id
It's a table which keeps track of relationships between articles and objects. I have stripped the code. Now it just contains the delete button (x). If someone press that button I want the user to be redirected to if(isset($_POST['deleteRelated'])) to get a "Are you sure"-message etc. But the hidden ID isn't passed correctly. The last related_id in the table is 29. When I try to echo out the hidden ID i just get 29 for every delete button (x).
The complete version of the code below gives me a table with the article title, object title and the delete button (x). Because of a submit button can't pass a value by itself I need a hidden value. But when I pass it by pressing the delete button (x) i just gets 29 every time.
Code
if(isset($_POST['deleteRelated'])) {
echo $_POST['hidden-id']; // Will just display the last 'related_id' value.
else {
echo '
<form method="post">';
$stmt = $db->prepare("SELECT * FROM Related");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $related) {
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
}
echo '
</form>';
}
If I type:
<input type="submit" name="deleteRelated" value="' . $related['related_id'] . '">
It will display the correct value from the database instead of the x for the delete/submit button. But when I press the delete/submit button I just get the last related_id which is 29.
Can someone solve this problem? Should not be that hard?
Explanation
You have one <form> for the entire table, which includes (say) a dozen <input type="hidden" name="hidden-id" value="...">s.
Those values will ALL be sent to the server when the form is submitted (how do you expect it to know to only send the hidden-id which is next to the specific submit button that was pressed?) This is why you are only seeing the last hidden-id – they are all being sent, so the final one overrides/wins.
Solution
One solution would be to have a <form> per row instead of one <form> for the whole table:
foreach ($result as $related) {
echo '<form method="POST" action="...">';
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
echo '</form>';
}
That way, only the hidden-id value of the pressed button would be sent.
Alternatively, you really don't need a form for each row, you could use a button here instead and ditching those hidden inputs.
Example:
foreach ($result as $related) {
echo '<button type="submit" name="deleteRelated" value="' . $related['related_id'] . '">Delete</button>';
}
So now each value of that pressed button on each row will go to:
$related_id = $_POST['deleteRelated'];
You will have to put the in the foreach.
In your code, you have one form with a lot of button submit.
Try this:
foreach ($result as $related) {
echo '
<form method="POST" action="...">
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">
</form>';}
If I understood you right, why do you need multiple hidden inputs?
you might want to put this input once:
<input type="hidden" name="hidden-id" value="" />
Next, change the submit button to something like this:
echo '<input type="submit" name="deleteRelated" value="x" onclick="setValue('" . $related['related_id'] . "'); />';
And use a js function:
setValue(val) {
document.getElementsByName("hidden-id")[0].value = val;
}
Easy solution: Put your form tag inside foreach loop
$stmt = $db->prepare("SELECT * FROM Related");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $related)
{
echo '<form method="post">';
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
echo '</form>';
}

PHP - Delete on button click not working

I need to make a delete button that only the admin can see. The button needs to delete an item in my database but I'm having troubles with the last part.
I used this code to create the button and to call the delete function when it's clicked
if ($_SESSION['UserID'] == 1) {
echo '<button name="featureDelete"> Delete </button>' . '<br>';
if (isset($_POST['featureDelete'])) {
$deleteFeature = $feature->Delete($row);
}
}
And this is my delete function in my class
public function Delete($row)
{
$db = new db();
$sql= "DELETE FROM features WHERE FeatureID ='".$row['FeatureID']."'";
$db->conn->query($sql);
}
So I can see the button, but when I click it nothing happens, I even tried echo'ing something, but didn't get a result. What am I missing?
You have no form. Only a button.
echo '<form method="post"><button name="featureDelete"> Delete </button></form>' . '<br>';
You have to create a form for your button :
<form action="" method="post">
<input type="submit" name="featureDelete" value="Delete" />
</form>
you have to create a form and send the variable $row.
Try:
<form method="POST"> <input type="text" value="1" name="row" /> <button type="submit" >DELETE</button></form>
And then, on the PHP, before executing the SQL
$row = $_POST['row'];

Categories