简体   繁体   中英

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. Originally I used non-javascript HTLM with echoed PHP to create an array of blocks, each representing a single media file, on a page. When user clicks on block then it goes to a PHP counter file and plays the media file. It is working fine. Running into problems when trying to convert to AJAX/javascript running of the PHP counter file.

The problems appear to be:

  1. the proper ID is not registered using the onclick.
  2. the ID is not being passed to the Ajax called php file and the counters are not updating.

The javascript in head of main block building file is:

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

Hundreds of blocks, each with separate MySQL ID are built and displayed on main page.

ADDITION 040913: The PHP starts with:

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:

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

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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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