Php 如何使用从数据库导入的值

Php 如何使用从数据库导入的值,php,jquery,html,mysql,ajax,Php,Jquery,Html,Mysql,Ajax,大家好,这是我的问题。 下面的代码从我的数据库中获取数据,并将其显示在输入字段和按钮中。我希望这样,如果我点击按钮,它应该得到值(从db导入)。但我面临的问题是,所有输入和按钮都有相同的ID,所以它只捕获第一个按钮的值(或者我认为是这样)。我怎样才能使我点击的每个按钮都有自己的独立值呢 <?php $dbcon=mysqli_connect("localhost","root",""); mysqli_select_db($dbcon,"codex");

大家好,这是我的问题。 下面的代码从我的数据库中获取数据,并将其显示在输入字段和按钮中。我希望这样,如果我点击按钮,它应该得到值(从db导入)。但我面临的问题是,所有输入和按钮都有相同的ID,所以它只捕获第一个按钮的值(或者我认为是这样)。我怎样才能使我点击的每个按钮都有自己的独立值呢

    <?php  
    $dbcon=mysqli_connect("localhost","root","");  
    mysqli_select_db($dbcon,"codex"); 
    require('config/server.php');
    ?>
    <table class="table table-striped"> 
    <th>ID</th>  
     <?php  
    $view_users_query="select * from users";//select query for viewing 
     users.  
    $run=mysqli_query($dbcon,$view_users_query);//here run the sql 
    query.
    while($row=mysqli_fetch_array($run))//while look to fetch the result 
    and store in a array $row.  
    { 
    ?>    

    <!--here showing results in the table -->

    <form id="loginForm" method="" action="" novalidate>
    <tr>
    <div class="panel2"> 
    <main class="content">

    <td><input name="hanis" id="hanis" type="text" value="<?php echo 
    $row['email']?>"  autofocus /></td>

    <td><button type="button" class="btn btn-success btn-block" 
    name="hanis" id="hanis" onclick="hanisdata()" value="<?php echo 
    $row['email']?>" ><?php echo $row['email']?></button><</td>

    </main></div>
    </div>
    </div>
    </form>
    <?php } ?>
    </tr>


    <script type="text/javascript">

    function hanisdata() {
    var hanis=$("hanis").val();

    alert(hanis); 
    // AJAX code to send data to php file.
    $.ajax({
        type: "POST",
        url: "hanis.php",
        data: {hanis:hanis},
        dataType: "JSON",
        success: function(data) {
         $("#message").html(data);
        $("p").addClass("alert alert-success");
        },
        error: function(err) {
        alert(err);
        }
       });

        }

       </script>

身份证件

注意:-不要对元素使用相同的id

您可以通过像
onclick=“hanisdata(this)”

示例

<button type="button" class="btn btn-success btn-block" 
    name="hanis" id="hanis" onclick="hanisdata(this)" value="<?php echo 
    $row['email']?>" ><?php echo $row['email']?></button>

然后您可以在js中获取特定元素,然后可以在下面的示例中找到父元素并搜索输入字段

JS代码

<script type="text/javascript">

 function hanisdata(el) {
    var hanis=$(el).parent().find("input").val();

    alert(hanis); 
    // AJAX code to send data to php file.
    $.ajax({
        type: "POST",
        url: "hanis.php",
        data: {hanis:hanis},
        dataType: "JSON",
        success: function(data) {
         $("#message").html(data);
        $("p").addClass("alert alert-success");
        },
        error: function(err) {
        alert(err);
    }
  });

 }

</script>

函数hanisdata(el){
var hanis=$(el.parent().find(“input”).val();
警报(哈尼斯);
//将数据发送到php文件的AJAX代码。
$.ajax({
类型:“POST”,
url:“hanis.php”,
数据:{hanis:hanis},
数据类型:“JSON”,
成功:功能(数据){
$(“#消息”).html(数据);
$(“p”).addClass(“警报成功”);
},
错误:函数(err){
警惕(err);
}
});
}

不要使用
ids
使用
,附加点击全部,然后使用
data-*
属性或通过遍历第一个子tr>输入来查找值您不应该为每个标记保留相同的
id
。您可以使用类,也可以使用类,表tr中的div和main做什么?那是无效的。