Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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循环中的MySQL id发送到AJAX PHP脚本_Php_Mysql_Jquery_Variables - Fatal编程技术网

如何将PHP循环中的MySQL id发送到AJAX PHP脚本

如何将PHP循环中的MySQL id发送到AJAX PHP脚本,php,mysql,jquery,variables,Php,Mysql,Jquery,Variables,我正在讨论如何在单击AJAX调用PHP计数器文件时隔离特定的MySQL id。最初,我使用非javascript HTLM和回显PHP在页面上创建一个块数组,每个块表示一个媒体文件。当用户单击block时,它将转到一个PHP计数器文件并播放媒体文件。它工作得很好。尝试转换为运行PHP计数器文件的AJAX/javascript时遇到问题 问题似乎是: 未使用onclick注册正确的ID。 ID没有传递到名为php文件的Ajax,计数器也没有更新。 主块构建文件头部的javascript为: <

我正在讨论如何在单击AJAX调用PHP计数器文件时隔离特定的MySQL id。最初,我使用非javascript HTLM和回显PHP在页面上创建一个块数组,每个块表示一个媒体文件。当用户单击block时,它将转到一个PHP计数器文件并播放媒体文件。它工作得很好。尝试转换为运行PHP计数器文件的AJAX/javascript时遇到问题

问题似乎是:

未使用onclick注册正确的ID。 ID没有传递到名为php文件的Ajax,计数器也没有更新。 主块构建文件头部的javascript为:

<script type="text/javascript" src="jquery.js"></script>';

提前非常感谢您的建议

如果您可以退出php并再次进入,为什么要使用echo

?>your code here<?php

你能试试这个代码吗

$scr = 'var currentID = ' . $row['id'] .
       '$.get("counter5.php?id='. $row['id'] .'")' .
       'alert("the current id is " + '. $row['id'] .')';
剧本

<script type="text/javascript">
  $(document).on('click', '#btn-id', function(){    
    runCounter();
  })

  function runCounter(){
    <?php echo $scr; ?>
  }
</script>
完整代码

<html>
  <title>Your title</title>
  <head>
    <?php

      // get the value/content of $row variable here...

      $scr = 'var currentID = ' . $row['id'] .
             '$.get("counter5.php?id='. $row['id'] .'")' .
             'alert("the current id is " + '. $row['id'] .')';
    ?>

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
      $(document).on('click', '#sprytrigger1', function(){    
        runCounter();
      })

      function runCounter(){
        <?php echo $scr; ?>
      }
    </script>

  </head>
  <body>

    <! -- your body here -->
    <a href="#" id="sprytrigger1">Click me!</a>

  </body>
</html>

我希望这能在某种程度上帮助您。

这不是OP所要求的?因为它位于php循环中,无法轻易退出。当我尝试此操作时,我得到一个错误:警告:在第219行的***.php中为foreach提供的参数无效。我试图将$results更改为$result以匹配我以前的MySQL查询,但收到了相同的错误。请确保$results是一个关联数组,类似于以下数组[0]=>array'id'=>1、'name'=>james',[1]=>arrau'id'=>2、'name'=>eric'我想知道您是否可以给我一些关于如何为$results构建关联数组的更多信息。现在我只是使用mysql select来检索数据,包括$result和$row的id。我仍然在foreach上得到相同的错误。我想我对数组和foreach有些不了解。在添加了040913之后,我在原始帖子中添加了一个编辑:PHP以:解释While循环以及MySQL查询开始。非常感谢你的帮助,我已经试过了。当我这样做并查看页面源代码时,代码看起来是正确的,但由于某种原因,当用户单击一个块时,会发出一个看似随机的id警报。
?>your code here<?php
$scr = 'var currentID = ' . $row['id'] .
       '$.get("counter5.php?id='. $row['id'] .'")' .
       'alert("the current id is " + '. $row['id'] .')';
<script type="text/javascript">
  $(document).on('click', '#btn-id', function(){    
    runCounter();
  })

  function runCounter(){
    <?php echo $scr; ?>
  }
</script>
<html>
  <title>Your title</title>
  <head>
    <?php

      // get the value/content of $row variable here...

      $scr = 'var currentID = ' . $row['id'] .
             '$.get("counter5.php?id='. $row['id'] .'")' .
             'alert("the current id is " + '. $row['id'] .')';
    ?>

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
      $(document).on('click', '#sprytrigger1', function(){    
        runCounter();
      })

      function runCounter(){
        <?php echo $scr; ?>
      }
    </script>

  </head>
  <body>

    <! -- your body here -->
    <a href="#" id="sprytrigger1">Click me!</a>

  </body>
</html>