Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用php在循环中单击按钮时显示数据_Javascript_Php - Fatal编程技术网

Javascript 如何使用php在循环中单击按钮时显示数据

Javascript 如何使用php在循环中单击按钮时显示数据,javascript,php,Javascript,Php,我有一个项目,其中我在while循环中显示一个按钮标记。在每次单击按钮时,我都希望显示一个带有相应用户ID的警报框。这是我的密码: <?php $data = mysql_query("Select RegisterId,FName,Image1 from Information where RegisterID='$profileMonth1'") or die(mysql_error()); while ($dis1 = mysql_fetch_array($data)

我有一个项目,其中我在while循环中显示一个按钮标记。在每次单击按钮时,我都希望显示一个带有相应用户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 "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div> 
<?php } ?>
但它总是向我显示最后一个用户id。。而我想在everyclick上显示每个用户的用户ID

有人能帮忙吗?

在你的代码中试试这个

HTML:


在这里,您调用的函数是未定义的
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> 

您的代码需要一些修改

首先,您已经准备好向javascript函数发送
id
,但是,您没有向它传递
id

PHP

<?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 } ?>

使用这段代码,您的点击甚至不应该返回最后一个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代码中,您正在寻找一个应该是demo1的id demo2。此外,它应该是document.getElementById(“demo1”).innerHTML。这将为您提供div上的文本。此外,您的php代码需要一些改进。我会在下面的答案中写一个解决方案你在初始化$RegisterId变量吗?@Erick在问题中说他得到了所有id,但它只提醒最后一个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="demo" value="<?php echo "$RegisterId" ?>">
<button onClick="validate()">Show Interest</button>
</div> 
function validate(){
    var id2 = document.getElementById('demo').getAttribute('value');
    alert(id2);
}
<?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 } ?>
function validate1(id2) {
  // var id2;
  //id2 = document.getElementById('demo2').getAttribute('value');
  alert(id2);
}
<?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 } ?>
function validate(element){
    alert(element.value)
}