douwang9650
douwang9650
2016-08-06 14:57
浏览 57

PHP,MySQL,HTML / CSS和javascript - 带有模态框弹出窗口的循环和按钮

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>";
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drpph80800
    drpph80800 2016-08-06 20:55
    已采纳

    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.

    点赞 评论

相关推荐