dongxi3859 2017-07-17 19:30
浏览 137
已采纳

如何将php变量插入属性标记的描述中

Hi I am trying to add function of dynamic deleting rows list and want to name id's of html tags using php code, do you have any ideas how to insert php code into description of tag atributes properly(the code is in while loop it must help me to delete selected rows from list dynamicly), thanks.

<?php
$connect = mysqli_connect("localhost", "root", "", "guest_book");
$query ="SELECT * FROM users ORDER BY ID DESC";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Webslesson Tutorial | Datatables Jquery Plugin with Php MySql and Bootstrap</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
</head>
<body>
<br /><br />
<div class="container">
    <h3 align="center">Datatables Jquery Plugin with Php MySql and Bootstrap</h3>
    <br />
    <div class="table-responsive">
        <table id="employee_data" class="table table-striped table-bordered">
            <thead>
            <tr>
                <td>Id</td>
                <td>Name</td>
                <td>Browser name</td>
                <td>Designation</td>
                <td>Age</td>
                <td>Remove</td>
            </tr>
            </thead>
            <?php
            $id = 1;
            while($row = mysqli_fetch_array($result))
            {
                $but_id = "but"."$id";
                $row_id = 'row'."$id";
                echo ' 
                              <div id = "$row"> // this line <?php ?> dont help

                               <tr>  
                                    <td>'.$row["id"].'</td>  
                                    <td>'.$row["name"].'</td>  
                                    <td>'.$row["browser_name"].'</td>  
                                    <td>'.$row["ip"].'</td>  
                                    <td>'.$row["email"].'</td>  
                                    <td><button type="button" name="remove" id ="$id" class="btn btn-danger btn_remove">X</button></td>// this line 
                               </tr>  
                               </div>

                                                              ';

                ++$id;
            }
            ?>
        </table>
    </div>
</div>
</body>
</html>
<script>
    $(document).ready(function(){
        $('#employee_data').DataTable();
        $('.btn').click(function(){
            var button_id = $(this).attr("id");
            alert(button_id);
            $('#row'+button_id+'').remove();
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • dongpu9481 2017-07-17 19:32
    关注

    Because you're putting the whole string in single quotes you have to concatenate the value:

    echo ' 
          <div id = "'.$row['id'].'"> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀