简体   繁体   English

使用PHP在HTML中获取表值

[英]Grab table value in html using php

This is my code and it is inside table tags. 这是我的代码,它在表格标记中。 It grabs data from my database and put it in table. 它从我的数据库中获取数据并将其放在表中。 My database also has auto increment column named id. 我的数据库还具有名为id的自动增量列。 What I want to do is when I click the cross icon that is in anchor, I will get the id for that column. 我想要做的是,当我单击锚点中的十字图标时,我将获得该列的ID。

I tried putting the value of $row['id'] in an input type='hidden' but had no success with it. 我尝试将$ row ['id']的值放入输入type ='hidden'中,但没有成功。

Another idea I has is making an anchor id=".$row['id']." 我的另一个想法是制作锚id =“。$ row ['id']。” so every icon has a unique id pertaining to the id for that line. 因此,每个图标都有与该行的ID相关的唯一ID。 Though I don't if it is good. 虽然好,我不知道。 I can't check because I have no idea how to get an element's id so I can check. 我无法检查,因为我不知道如何获取元素的ID,因此可以进行检查。 And I don't know how to pass this value to php. 而且我不知道如何将此值传递给php。

<?php        
    $varSQL = "SELECT empNum, empName FROM tool.users";
    $result = mysql_query($varSQL, $Con);
    while ($row = mysql_fetch_assoc($result)){
        echo "<tr>";
        echo "<td>".$row['empNum']."</td>";
        echo "<td>".$row['empName']."</td>";
        echo "<a href='#'><img src='icons/cross.ico' alt='Delete' height='16' width='16'></a></td>";
        echo "</tr>";
    }
?>

It is because you do not have id in your query. 这是因为您的查询中没有id It should look like this: 它看起来应该像这样:

SELECT id, empNum, empName FROM tool.users

You can use data attribute - 您可以使用data属性-

echo "<a href='#'><img src='icons/cross.ico' alt='Delete' data-rowid='" . $row['id'] . "' class='cross-icon' height='16' width='16'></a></td>";

Also added a class to be be used as selector(Not required for all cases). 还增加了一个class被用作选择器(不适用于所有的情况下)。 And with jquery access them when the icon is click - 并通过jquery在单击图标时访问它们-

$('.cross-icon').on('click', function() {
    var id = $(this).data('rowid');
})

Fiddle Example 小提琴的例子

For hidden fields there is no need of jQuery - 对于hidden字段,不需要jQuery-

<input type="hidden" name="id" value="<?php echo $row['id']?>"

Or pass it as query string - 或将其作为查询字符串传递-

href='page-to-redirect.php?id=<?php echo $row["id"]?>'

What you want to do with this id? 您要如何使用此ID? You can get the id using javascript (not PHP) and then using this value in an AJAX request or put this value into an input form and send it throw post but always you need to use JS not PHP. 您可以使用javascript(而非PHP)获取ID,然后在AJAX请求中使用此值,或将该值放入输入表单中并发送给它,然后发送,但始终需要使用JS,而不是PHP。

<?php        
     $varSQL = "SELECT id, empNum, empName FROM tool.users";
     .........    
     echo "<td><a href='#'><img src='icons/cross.ico' data-rowid=$row['id'] alt='Delete' height='16' width='16'></a></td>";
     ........

Try this one. 试试这个。

In select query add id also and Get the data-rowid value for your further operations 在选择查询中,还添加id并获取data-rowid值,以进行进一步的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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