简体   繁体   English

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

[英]How to send MySQL id within PHP loop to AJAX PHP script

I am battling with how to isolate specific MySQL id's when clicked for AJAX call to PHP counter file. 我正在为单击PHP计数器文件的AJAX调用时如何隔离特定的MySQL ID进行斗争。 Originally I used non-javascript HTLM with echoed PHP to create an array of blocks, each representing a single media file, on a page. 最初,我将非JavaScript HTLM与回显的PHP结合使用,以在页面上创建一个块数组,每个块代表一个媒体文件。 When user clicks on block then it goes to a PHP counter file and plays the media file. 当用户单击块时,它将转到PHP计数器文件并播放媒体文件。 It is working fine. 一切正常。 Running into problems when trying to convert to AJAX/javascript running of the PHP counter file. 尝试将PHP计数器文件转换为AJAX / javascript运行时遇到问题。

The problems appear to be: 问题似乎是:

  1. the proper ID is not registered using the onclick. 正确的ID未使用onclick注册。
  2. the ID is not being passed to the Ajax called php file and the counters are not updating. 该ID未传递到称为php文件的Ajax,并且计数器未更新。

The javascript in head of main block building file is: 主块构建文件开头的javascript是:

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

Hundreds of blocks, each with separate MySQL ID are built and displayed on main page. 数百个块,每个块都有单独的MySQL ID,并显示在主页上。

ADDITION 040913: The PHP starts with: 地址040913:PHP开头为:

WHILE ($varX <= $varY) {
mysql_select_db($database_cms_test, $cms_test);
$query = "SELECT * FROM reference WHERE rank=$varRank";
$result = mysql_query($query) or die();
$row = mysql_fetch_array($result); ...

The Ajax part starts here: Ajax部分从这里开始:

<script type="text/javascript">
function runCounter() {
    var currentID = 222; 
    $.get("counter5.php?id=222"); 
    alert("the current id is " + 222);
} 
</script>

<a title="Media File Name" href "#" onclick= "runCounter();" id="sprytrigger1">

The equivalent Ajax code prior to echoing is: 回显之前的等效Ajax代码为:

echo '<script type="text/javascript">';
echo 'function runCounter() {';
echo ' var currentID = ';
printf ("%s", $row['id']);  
echo '; $.get("';
echo 'counter5.php?id=';
printf ("%s", $row['id']); 
echo '"); alert("the current id is " + ';
printf ("%s", $row['id']);  
echo ');} </script>';

echo '<a title="';
printf ("%s", $row['song_name']);
echo '" href "#';
echo '" onclick= "runCounter();';
echo '" id="sprytrigger';
echo $varRank;
echo '">';

In the counter5.php file, the main id request code is: 在counter5.php文件中,主要的ID请求代码为:

$id = mysql_real_escape_string($_GET['id']);

Thank you so much in advance for your advice / suggestions. 提前非常感谢您的建议。

Can you try this code... 你可以试试这个代码...

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

script 脚本

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

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

Full code 完整代码

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

I hope this will help you in some way. 希望对您有所帮助。

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

?>your code here<?php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM