如何显示数据上的按钮点击在一个循环使用php
how to display data on button click in a loop using php
我有一个项目,我在一个while循环中显示一个按钮标签。在每次单击按钮时,我想显示一个带有相应UserId的警告框。下面是我的代码:
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo1" value="<?php echo "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div>
<?php } ?>
下面是我的validate函数:
function validate1(id2)
{
// var id2;
id2 = document.getElementById('demo2').getAttribute('value');
alert(id2);
}
但是它总是显示我最后一个用户id…而我想在每次点击时显示每个用户的userid
有人能帮忙吗?
在这里,你调用的函数是未定义的validate1
,你也不需要在你的函数声明中获得任何参数,因为当你调用它时你没有传递任何参数。
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo" value="<?php echo "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div>
JS
function validate(){
var id2 = document.getElementById('demo').getAttribute('value');
alert(id2);
}
在你的代码中试试
HTML:<button onClick="validate('<?php echo $RegisterId; ?>')">Show Interest</button>
Javascript: function validate(id2)
{
alert(id2);
}
你的代码需要修改。
首先,你做了一个规定发送id
到javascript函数,但是,你没有传递id
给它。
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<div id="demo1" value="<?php echo $dis1['RegisterId'];?>">
<button onClick="validate('<?php echo $dis1['RegisterId'];?>')">Show Interest</button>
</div>
<?php } ?>
Javascript: function validate1(id2) {
// var id2;
//id2 = document.getElementById('demo2').getAttribute('value');
alert(id2);
}
使用此代码,您的单击甚至不应该返回最后一个id。你的javascript函数看起来不太好。
应该可以;
<?php
$data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error());
while ($dis1 = mysql_fetch_array($data)) {
?>
<!-- removed id attribute, as they're unique, you can't set it to every div.
If you really need id attribute, you can set one by using your RegisterId (e.g. id="demo-<?php echo $RegisterId; ?>)
And moved value attribute to button tag, it's much more useful in it. -->
<div>
<button onClick="validate(this)" value="<?php echo "$RegisterId" ?>">Show Interest</button>
</div>
<?php } ?>
Javascript function validate(element){
alert(element.value)
}
相关文章:
- 创建一个循环来简化HTML和CSS代码
- 如何使用“;getElementById”;在一个循环中
- jQuery触发器事件在一个循环中多次出现
- js:如何制作一个循环,将20个不同的东西添加到一个对象中
- 每第n个图像具有下一个循环的余数偏移
- CasperJS-如何治疗'单击'在一个循环中
- 我怎样才能把它变成一个循环,而不是一个递归函数
- 在一个循环中读取xml文件的所有节点
- ajax 返回带有循环的图像长度,检查最后一个循环
- JQuery Slider动画只发生一个循环
- JQuery 滑动一个表格形成,另一个循环滑动
- 如何跟踪有序列表中的顺序,该列表具有一个循环列表项,提供不同的参数
- Node.js:如何在一个循环中使用回调调用方法
- 我将如何批量添加大量 jquery 脚本,在一个循环中只有一个细微的差异
- 循环遍历 Angular HTTP 请求中的对象,等到响应后再运行下一个循环
- 从嵌套循环 Javascript 中的一个循环访问参数
- jQuery点击函数在for循环中只在最后一个循环中工作
- 我可以为 4 个不同的下拉列表创建一个循环吗?
- 翡翠模板在一个循环中有多个数组
- 仅使用一个循环生成嵌套数组