【问题标题】:Javascript popup only showing the last result in PHP queryJavascript 弹出窗口仅显示 PHP 查询中的最后一个结果
【发布时间】:2014-02-18 00:02:43
【问题描述】:

我有以下代码块,但我不确定如何做我想做的事情。

本质上,我希望 javascript 弹出窗口显示该行的值,但(显然)它只显示最后一行数据,因为弹出窗口不是为每一行设置的,而是在单击时调用变量。

任何帮助将不胜感激!

<?php
$result = mysql_query("SELECT hr_overtime.overtime_id, hr_user.name, hr_overtime.overtime_date, hr_overtime.overtime_type, hr_overtime.overtime_from, hr_overtime.overtime_to, hr_overtime.overtime_amount, hr_overtime.details
FROM hr_overtime 
inner join hr_user
on hr_user.user_id = hr_overtime.user_id
where hr_overtime.overtime_status = 'Pending' order by hr_overtime.overtime_date ASC");
echo "
<table border='0'>
<tr>
<th class='tablecell_header'>Consultant</th>
<th class='tablecell_header'>Date</th>
<th class='tablecell_header'>Type</th>
<th class='tablecell_header'>From</th>
<th class='tablecell_header'>To</th>
<th class='tablecell_header'>Amount</th>
<th> </th>
<th> </th>
<th> </th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  $work = $row['details'];
  echo "<tr>";
    echo "<td class='tablecell'>" . $row['name'] . "</td>";
    echo "<td class='tablecell'>" . $row['overtime_date'] . "</td>";
    echo "<td class='tablecell'>" . $row['overtime_type'] . "</td>";
    echo "<td class='tablecell'>" . $row['overtime_from'] . "</td>";
    echo "<td class='tablecell'>" . $row['overtime_to'] . "</td>";
    echo "<td class='tablecell'>" . $row['overtime_amount'] . "</td>";?>
    <td class='tablecell'> <button onclick="myFunction()">Show work</button>         </td><script>
function myFunction()
{
alert("<?php echo $work;?>");
}
</script>
        <?php
    echo "<td valign='middle'><form action='manager_overtime_approve.php'     method='post'>
<input name='approve_id' type='hidden' value='" . $row['overtime_id'] . "' />
<input type='submit' value='APPROVE' id='edit' />
</form></td>";
    echo "<td valign='middle'><form action='manager_overtime_reject.php' method='post'>
<input name='cancel_id' type='hidden' value='" . $row['overtime_id'] . "' />
<input type='submit' value='REJECT' id='edit' />
</form></td>";
 echo "</tr>";
  } 
    echo "</table>";
?>

【问题讨论】:

  • 警告,此扩展自 PHP 5.5.0 起已弃用,将来将被删除。 mysql_query

标签: javascript php mysql while-loop


【解决方案1】:

我认为,在结果 HTML 中,您有多个 myFunction() 定义,而 javascript 只使用最后一个。您可以使用 HTML 标签属性来显示正确的信息。

【讨论】:

    【解决方案2】:

    试试这个

    $work .= $row['details'];
    

    并将javascript设置为循环
    祝你好运

    【讨论】:

      【解决方案3】:

      那是因为myFunction 是一个Javascript 函数,它被定义一次 并且 与PHP 共享相同的范围。所以$work 只保留最后一个值。

      我建议这样:

      echo "<tr data-detail-value='$work'>";
      ...
      <td class='tablecell'> <button onclick="myFunction(this)">Show work</button>
      ...
      function myFunction(btn) {
         alert(btn.parentNode.parentNode.getAttribute("data-detail-value"));
      }
      

      【讨论】:

        【解决方案4】:

        没有必要单独使用一个您使用不正确的函数。您可以简单地为此使用内联警报:

        <td class='tablecell'> <button onclick="alert('<?php echo $work;?>');">Show work</button>
        

        【讨论】:

        • 这有什么不同?
        • 不需要单独的函数。内联警报将起作用。
        • 你是对的,但我的问题是为了激励你更多地解释你的解决方案。这是一个很好的解决方案,您必须在此处添加更多内容,以便 OP 了解这种方法的可用性
        • 这很有意义,现在我已经看到它写下来了...... >_
        【解决方案5】:

        你不能在你的 foreach 循环中插入myFunction() 的声明,这样你就覆盖了每个条目的行为,因此它只适用于最后一行。

        一个快速的解决方案是插入整个 $work 变量作为 onclick 函数的参数,例如。

        <td class='tablecell'> <button onclick="myFunction('<?php echo $work ?>')">Show work</button>
        

        然后在foreach循环之外你可以定义myFunction比如:

        function myFunction(details){
          alert(details)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-15
          • 2016-02-08
          • 2017-08-13
          • 1970-01-01
          • 2015-09-19
          • 1970-01-01
          • 2013-09-16
          • 1970-01-01
          相关资源
          最近更新 更多