Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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
在循环时保留PHP中的变量_Php_Javascript - Fatal编程技术网

在循环时保留PHP中的变量

在循环时保留PHP中的变量,php,javascript,Php,Javascript,我一直在寻找一种方法来做到这一点 我使用PHP while循环从数据库中获取一些变量: <?php while ($row = mysql_fetch_array($query)) { $id = $row["ID"]; ?> <script type="text/javascript"> <!-- function go_there() { var id= "<?php echo $id ?>" var where_to= confirm("Are

我一直在寻找一种方法来做到这一点

我使用PHP while循环从数据库中获取一些变量:

<?php while ($row = mysql_fetch_array($query)) {
$id = $row["ID"];
?>

<script type="text/javascript">
<!--
function go_there()
{
var id= "<?php echo $id ?>"
var where_to= confirm("Are you sure you want to delete?");
 if (where_to== true)
 {
   window.location="index.php?p=delete&id=" + id;
 }
 else
 {

  }
}
//-->
</SCRIPT>
<td><form><INPUT TYPE="button" value="Go!" onClick="go_there()"></form></td></tr>
<? } //end while
?>
有时候我会得到10个结果,然后我会得到10个!屁股。但当我进入删除页面时,$is变量为off,因为无论我按下哪个底部,结果都与上一个结果相同


有没有关于如何绕过这个问题的想法,所以我得到了确认框,仍然保留了要删除的正确id?

这是一个更正的脚本

<?php while ($row = mysql_fetch_array($query)) {
$id = $row["ID"];
?>      
<SCRIPT language="JavaScript">
<!--
function go_there()
{
var id= "<?php echo $id ?>"
var where_to= confirm("Are you sure you want to delete?");
 if (where_to == true)
 {
   window.location="index.php?p=delete&id=" + id;
 }
 else
 {

  }
}
-->
</SCRIPT>
  <td><form><INPUT TYPE="button" value="Go!" onClick="go_there()"></form></td></tr>
您的按钮将不起作用,因为您已将注释标记和->添加到函数中。如果您需要帮助,请评论


另外,你能在评论中澄清你的问题吗?我们可以为您提供更好的帮助。

您正在为每个条目创建一个名为go_there的全局函数,因此只保留最后一个。相反,只创建一个函数,并从按钮上的属性获取id值

function go_there(button)
{
  var id= button.getAttribute("data-id");
  var where_to= confirm("Are you sure you want to delete?");
  if (where_to== true)
  {
    window.location="index.php?p=delete&id=" + id;
  }
}
然后:


在我看来,您似乎也在循环中创建javascript函数,因此它只触发其中一个函数,而您正在创建具有相同名称的多个函数。它怎么知道该用哪一个呢。您应该创建1个javascript函数,并将id作为参数传递给它

<script type="text/javascript">

function go_there(id)
{

 var where_to= confirm("Are you sure you want to delete?");
 if (where_to== true)
 {
   window.location="index.php?p=delete&id=" + id;
 }
 else
 {

  }
 }

 </SCRIPT>
 <?php while ($row = mysql_fetch_array($query)) {
$id = $row["ID"];

 echo '<td><form><INPUT TYPE="button" value="Go!" onClick="go_there(<?php echo $id; ?>)"></form></td></tr>'
 } //end while
 ?>

总的来说,这是一种糟糕的方法。您应该避免内联编写处理程序,除非您有一个特定的情况,这是唯一的方法。如果在问题中的元素上附加一个处理程序,效果会更好-无论如何,对于firefox…:

<!-- output our inputs with the id value in an attribute data-id-value -->
<?php while ($row = mysql_fetch_array($query)): $id = $row["ID"]; ?>
   <td><form><INPUT class="go-button" TYPE="button" value="Go!" data-id-value="<?php echo $id ?>"></form></td></tr>
<?php endwhile; ?>

<script type="text/javascript">
   var goButtons = document.querySelectorAll('input.go-button'), // select all out buttons
       // define our handler function
       goThere = function (event) {

            // get the id the we encoded from php from our data attr
            var id = event.target.getAttribute('data-id-value');

            if(confirm('Are you sure you want to delete?')) {

                // if confirm is true then redirect to the delete url
                window.location = "index.php?p=delete&id=" + id;
            }
       };

   // loop over the buttons and attach the handler
   for (var i = 0; i < goButtons.length; i++) {
       goButtons[i].addEventListener('click', goThere);
   }
</script>

这里的问题是,这不一定是跨浏览器的。这通常是人们使用jQuery或类似的库的原因之一,这样他们就不必规范dom查询和侦听器附件。

您的问题是什么?将go_那里作为参数化函数,以$id作为参数;使其调用onclick也参数化。或者更好的做法是,为按钮本身指定一些值,然后在函数中检查该值。现在我真的不知道你在说什么。说到javascriptHTML,我有点像n00b。关于脚本的注释不会引起问题,尽管在2013年没有任何意义。我对脚本进行了一些编辑,没有让大家阅读全部内容,底部是有效的,但我的问题是每次while循环进行时,var id=都会发生变化,然后我按下我得到的底部之一,它将删除的是mysql语句中最后一个id的id好的。对不起,我真的不明白你的问题。请不要给我打分,因为这个问题没有很好地解释!我试试看。然后我就可以在我的大屏幕上使用javascript了,这似乎很有效。如果我想传递更多的变量,我该怎么办?很多,这让它对我有用。我希望我能投票支持这个答案,但我需要一个代表才能这么做对不起:但非常感谢,你救了我一天…@MortenBallePetersen你不需要投票支持它,只需接受它作为正确答案:如何使用post方法进行此操作?而不是使用window.location,您只需使用正确的post值提交表单,使其执行删除操作,或者在执行提交之前将action属性更改为delete URL。
<!-- output our inputs with the id value in an attribute data-id-value -->
<?php while ($row = mysql_fetch_array($query)): $id = $row["ID"]; ?>
   <td><form><INPUT class="go-button" TYPE="button" value="Go!" data-id-value="<?php echo $id ?>"></form></td></tr>
<?php endwhile; ?>

<script type="text/javascript">
   var goButtons = document.querySelectorAll('input.go-button'), // select all out buttons
       // define our handler function
       goThere = function (event) {

            // get the id the we encoded from php from our data attr
            var id = event.target.getAttribute('data-id-value');

            if(confirm('Are you sure you want to delete?')) {

                // if confirm is true then redirect to the delete url
                window.location = "index.php?p=delete&id=" + id;
            }
       };

   // loop over the buttons and attach the handler
   for (var i = 0; i < goButtons.length; i++) {
       goButtons[i].addEventListener('click', goThere);
   }
</script>