doubi7346 2017-11-13 07:57
浏览 141

如何获取由php脚本动态生成的div值?

I want to alert the name of the item whose place bid button I will click but my code seems to alert only laptop. Can anyone tell me how to get div values that are dynamically generated through a php script. Below is the code for the page.I want to alert name of that item whose place bid I click on. Since the page is dynamically generated which gets item details from a database containing item details.

<?php
    $con = mysqli_connect("localhost","root","*****","bidding") or
    die(mysqli_error($con));
    $select_query = "select * from items";
    $select_query_result = mysqli_query($con, $select_query) or
    die("Query Mistake");
?>
<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Bid</title>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="stylesheet" href="materialize/css/materialize.css"/>
        <link rel="stylesheet" type="text/css" href="seperate2.css">
        <script src="jquery-3.2.1.min.js"></script>
        <script src="materialize/js/materialize.min.js"></script>
        <script type="text/javascript">
        function post_data(){
            var item_name = document.getElementById("item_name").innerHTML;
            alert(item_name);
        }
        </script>
</head>
<body>
    <div class="container">
        <h1>Bid</h1>
        <div class="row">
            <?php while($row = mysqli_fetch_array($select_query_result)) { ?>
            <div class="col s3" >
                <div id="item_name"><?php echo"$row[item_name]"?></div>
                <div id="starting_price"><?php echo"$row[starting_price]"?</div>
                <div><?php echo"$row[description]"?></div>
                <button class="btn waves-effect waves-teal" 
                 onclick="post_data()">Place Bid</button>
            </div>
            <?php } ?>
        </div>
    </div>
</body>
</html>

</div>
  • 写回答

2条回答 默认 最新

  • drasebt1835 2017-11-13 08:00
    关注

    ID of element in HTML must be unique. Use class. In loop you declare static name of ID, that are not unique.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类