Jump to content

Dynamically create buttons with actions from MySQL database items


Jacques58

Recommended Posts

Good day,

 

I am creating a WebAPP with HTML, PHP and MySQL.

I am new to PHP and to this forum, I tried to search for a related topic but I could not find one.

The part I'm struggling with is, when creating a table listing all my products from my database, I need a button to be created with it, so that when I click on that button, the item connected to this button gets added into a separate table in the database.

 

Any help or guidance would be greatly appreciated.

 

 

Link to comment
Share on other sites

Hi,

 

Thank you and noted.

 

Below is the part where I get stuck.

On clicking of the button, how can I pass the variable '$row[prodCode]' to another php script, to use this variable?

 

<?php

require('dbconnect.php');

$ri = "SELECT prodDesc, prodPrice, prodCode FROM products";
$result = mysqli_query($con, $ri);

if ($result->num_rows > 0) {
    echo "<p>Sample</p>";
    echo "<table border=1px><tr><th>Item</th><th>Price</th></tr>";
  
    while ($row = $result-> fetch_assoc()) {
        echo
        "<tr><td width='100' align='center'>".$row["prodDesc"].
        "</td><td width='100' align='center'>".$row["prodPrice"].
        "</td><td>".
        "<button id='use' type='button' value='$row[prodCode]' >Use</button>".
        "</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
mysqli_close($con);
?>

Link to comment
Share on other sites

A <button> by itself doesn't do anything. What you need is a form which contains the data you want to send as well as a submit button to trigger form submission.

 

Forms are a basic HTML feature, and there are thousands of tutorials explaining them in great detail (like this one). To set input data without user interaction, you use a hidden field.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.