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.
Related
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'];
}
I've got here a table with all the data from my database. On the end on every row, I have put a checkbox with the value 1, to update the 'accept' status in my database, which is default by 0.
My problem is, that the value of a ticked checkbox should update the 'accept' status ONLY in the entry in its row after the submit button got pressed.
So basically I need to check if the checkbox is ticked, and if its ticked, the 'accept' status in the row of the checkbox gets the value of 1. For that, I think I need to get the 'match_id' of the row.
This is how it looks on the website:
This is the table:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('lr') or die(mysql_error());
$result = mysql_query("SELECT * FROM `challenge`") or die(mysql_error());
echo "<table class='match-table'>";
echo "<thead><tr> <th><h1>match_id</h1></th> <th><h1>Team</h1></th><th><h1>Accept Status</h1></th><th><h1>Accept</h1></th> </tr></thead><tbody>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['match_id'];
echo "</td><td>";
echo $row['team'];
echo "</td><td>";
echo $row['accept'];
echo "</td><td>";
?>
<form action="" method="post">
<input type="checkbox" name="challenge_accept" value="1"/>
<?php
echo "</td></tr>";
}
echo "</tbody></table>";
?>
<input type="submit" value="Save"/>
<input type="reset" value="Delete"/>
</form>
Any suggestions how I can do that?
Your HTML is utterly invalid, with MULTIPLE <form> being opened and only one </form> at the end of the page. You're also splitting your tags into garbage like
<tr><td><form></td></tr>
[many repeats]
</table>
</form>
Your form fields also don't include ANY method of identifying which row was actually clicked on, e.g. <input type="submit" name="selected_row" value="<?php echo $row['id'] ?>" />. As it stands right now, you've hardcoded the value 1, so no matter which of however many buttons you spit out, they'll ALL be sending 1 back to your code.
I work with this code:
<form method="get" name="MobileDetails">
<input name="brand" id="brand" value="<?php echo $brand;?>" type="hidden">
<input name="brid" id="brid" value="<?php echo $brandid;?>" type="hidden">
<button type="button" name="submitButton" value="get Details" onclick="getDetails()">
</form>
java script
<script type="text/javascript">
function getDetails(){
var brand = document.getElementById('brand').value;
var brandid = document.getElementById('brid').value;
document.MobileDetails.action = 'details.php?brand='+brand+'&id='+brandid;
document.MobileDetails.submit();
}
</script>
But it does not work in while loop. Whats the problem? My code is given below.
When i click on the button it do not do anything. But the code work great with out while loop given on the top.
<?php
require_once('connection.php');
$SQL= "SELECT*FROM mobile ORDER BY price ASC LIMIT 10";
$result= mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)){
$brand=$db_field['brand'];
$id=$db_field['id'];
$model=$db_field['model'];
echo "<form method='get' name='MobileDetails'>";
echo " <input name='brand' id='brand' value='". $brand ."' type='hidden'>";
echo" <input name='brid' id='brid' value='". $id ."' type='hidden'>";
echo" <input name='mod' id='mod' value='". $model ."' type='hidden'>";
echo" <button type='button' name='submitButton' value='get Details' onclick='getDetails()'/>
</form> ";
echo "CLICK HERE";
}
?>
You're using several times the same id. Ids have to be unique.
Uou are dealing with multiple id's. The job of an ID is to be unique identifier for the element. I suggest just using
<form action="details.php" type="get">
this will do exactly what you are trying to achieve without using the function.
The thing with element ID's is that they need to be unique for the page; however, as you may see, not required for HTML to be displayed. When calling your JS function getDetails(), it grabs the element by ID but when you have multiple ID's in the page, this will fail.
So what can you do? Well, in your loop, you create a new form for each 'brand'. You can pass a reference of the form to the grabdetails and then, by NAME, grab the values from that form.
Rather than using Javascript to generate a link based on given details put in a hidden field, you should just generate the action at the PHP level.
echo "<form method='get' name='MobileDetails' action='details.php?brand=$brand&id=$brandid'>";
But since you do have hidden fields, using just action='details.php' the form will take the user to
details.php?brand={brand}&brid={id}&mod={model}
You should look into POST or making your button into a plain link rather than having a form.
I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)
$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
}