John Davidson

html - get id from buttons with same name in php

0 comments
Message:


So I have a website that runs a cycle on a database as long as there is a record in it.
It also puts two buttons in each row within the table.
Each button has the same name.
If I press one of the buttons, how can I get the ID of that button (which points to a Youtube link from the database) to be included in a SQL query.


Here is my code:


<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ruben MediaShare</title>
</head>
<body>
<form method='POST'>
<?php

session_start();

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "Welcome to the member's area, " . $_SESSION['modname'] . "!";
} else {
echo "Please log in first to see this page.";
}


$link = mysqli_connect("localhost", "asd", "asd", "mediareq");

if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "SELECT * FROM requests";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>link</th>";
echo "<th>Twitch Név</th>";
echo "<th>Üzenet</th>";
echo "<th>Engedélyezés</th>";
echo "<th>Elutasítás</th>";
echo "</tr>";

while($row = mysqli_fetch_array($result)){
echo "<tr>";
$link = $row['link'];
echo "<td>" . "<iframe width='560' height='315' src='$link' frameborder='0' allowfullscreen></iframe>" . "</td>";
echo "<td>" . $row['ttvname'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='enable'></input>" . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='eject'></input>" . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
if (isset($_POST['enable'])) {
$ID = $_POST[$link];
echo $ID;
}
} else{
echo "Még nem kaptál videót, várjál már egy kicsit vagy csak tolj egy F5öt Pog Pog Pog";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</form>
</body>
</html>

The website looks like this: https://i.stack.imgur.com/sYUQt.jpg


If it can't be done that way, what can I do in this case to keep the loop but since it has to be dynamic, and somehow get it to pick the link from each button that it stores in the ID (or some other way)



My Answer:
html



Get ID from Buttons



// Assuming $result is the result of your SQL query
while ($row = mysqli_fetch_assoc($result)) {
echo "";
echo "";
echo "";
echo "";
echo "";
}
?>
{$row['title']}{$row['description']}";
echo "
";
echo "";
echo "";
echo "
";
echo "





In the above code, each button has a value attribute set to the ID of the corresponding record in the database. When a button is clicked, the ID of that button will be sent as a POST parameter to the `process.php` file.

In your `process.php` file, you can retrieve the ID of the button that was clicked using `$_POST['youtube_link']` or `$_POST['download_link']` depending on which button was clicked. You can then use this ID in your SQL query to fetch the corresponding Youtube link from the database.

Rate this post

3 of 5 based on 9591 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us