简体   繁体   中英

PHP, MySQL, HTML/CSS & javascript - While Loop and Buttons with Modal Box popup

So I've been breaking my brain over this one and I still can't figure it out. I have only basic knowledge of PHP and Modal Boxes and Javascript. So here's my concern...

I have PHP getting MySQL data from my database and placing this data in a table on my website [Bear in mind, this is a website for a fastfood restaurant]. I have a while loop that runs and adds each record in a table, and I add an ORDER button for each item. Each button is intended to open a modal box with the relevant item's information (so that I can add it to the shopping cart: i do have the shopping cart in order though).

THE PROBLEM:
Only the [ORDER] Button of the first item opens a modal box. The other buttons DON'T OPEN UP MODAL BOXES.

<?php
$con = mysqli_connect("localhost","root","root","mcd");
if(!$con){
    die("Can not connect".mysql_error());
}
$query = mysqli_query($con, "select * from pizza");
echo "<table border=1 align='center' width=50%>
<tr>
<th>Name</th>
<th>Medium</th>
<th>Large</th>
</tr>";
    while ($record = mysqli_fetch_array($query)) {
        echo "<tr>";
        echo "<td align='right'>".$record['pizzaName']."</td>";
        echo "<td align='right'> R".$record['PriceMedium']."</td>";
        echo "<td align='right'> R".$record['PriceLarge']."</td>";
        echo "<td align='right'> <input id='myBtn' type='button' value='ORDER'/>";
        echo "</tr>";
   }
   echo "</table>";
?>

I believe your problem come from the fact that your input has a id defined but no class.

As Id are intended to be unique on a page, if you bind a javascript function on elements selected by id, only the first element having this id will be bound.

I suggest you use

class='myBtn'

instead of

id='myBtn'

then adapt your javascript part accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM