如何为每个循环将javascript添加到php中?

如何为每个循环将javascript添加到php中?,javascript,php,jquery,foreach,Javascript,Php,Jquery,Foreach,如何为每个循环将下面的脚本添加到我的php中?那么你喜欢谁?id传递了正确的参数 任何帮助或示例代码都将不胜感激 我已经包括了可以工作的脚本和可以工作的php,我只是不确定如何将这两个文件结合起来 <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script> <script> $(document).r

如何为每个循环将下面的脚本添加到我的php中?那么你喜欢谁?id传递了正确的参数

任何帮助或示例代码都将不胜感激

我已经包括了可以工作的脚本和可以工作的php,我只是不确定如何将这两个文件结合起来

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script>
$(document).ready(function(){

$(".button").hover(function () {
    $.ajax({
        url: "who_likes.php?id='$id'",
        success: function (result) {
            $("#div1").html(result);
        }
    });
}, function () {
    $("#div1").html("");
});


});
 </script>

 <style type="text/css">
 #div1 {
background:#666;
color:#fff;
width:300px;
position:absolute;
}
 </style>

</head>

<body>
<a href="none.html" class="button">none</a>
<button>Button!</button>
<div id="div1"></div>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
</body>

</html>

$(文档).ready(函数(){
$(“.button”).hover(函数(){
$.ajax({
url:“谁喜欢.php?id=“$id”,
成功:功能(结果){
$(“#div1”).html(结果);
}
});
},函数(){
$(“#div1”).html(“”);
});
});
#第一组{
背景:#666;
颜色:#fff;
宽度:300px;
位置:绝对位置;
}
按钮
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。
然后是这个文件

<?php
include './authenticate.php';
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery-1.9.1.js"></script>

<?php
$user_type = $_SESSION["user"]["user_level"];
$user_id   = $_SESSION["user"]["user_id"]; //displays 1008
?>

<h1>Public TipsM/h1>
<?php include 'connection.php';

if ($mysqli->connect_error) {
         die('Connect Error: ' . $mysqli->connect_error);
        }

$liker_id = $_SESSION['user']['username'];
$mysqli = new mysqli('connection here');

if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}

$query = "SELECT * FROM public_tips ORDER by date"; 
$result = $mysqli->query($query);
while($row = $result->fetch_array()) 
{ $rows[] = $row; }

foreach($rows as $row) 
{ 
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$id = $row['id'];
$booky = $row['booky'];
$sp = $row['sp'];
$placed = $row['placed'];
// if ($sp === 'yes') {$start = ' SP';

$query2 = "select count(distinct liker_id) as counted from likes where id = $id";
$result2 = $mysqli->query($query2);
while($row2 = $result2->fetch_array()) { 
//$likes = $row2['count(liker_id)'];
$likes = $row2['counted'];
}

echo  
'<div style="float:left; width:700px; margin-bottom:10px; margin-top: 10px; margin-left:150px; padding:10px; border: solid 1px grey;">
  <h5>
        ('.$date.") " .
         $row['time'] . " " . 
         $row['course'] . " - " . 
         $row['horse'] . " " . 
         $row['odds1'] . "/" . $row['odds2'] . $row['sp'] . "-" . $row['place'] .
         '</h4><br>'. 
         $row['description'] . '<br/>' . $likes . ' likes - 
         <a href="process_likes.php?table_id='.$table_id.'&id='.$id.'&liker_id='.$liker_id.'" />LIKE</a> | 

//
//I need to add script below here so that each who_likes.php has the right parameter??           
//

         <a href="who_likes.php?id='.$id.'"  class="button" target="_blank">who likes?</a> // script here???



         <FORM METHOD="LINK" ACTION="'.$booky.'"><br/><INPUT TYPE="submit" VALUE="Best Odds"></FORM><br/><span style="font-size:10px">Added ' . 

         $row['date_added'] . ' at ' . $row['time_added'] . ' by ' . $row['username'].  '</span></div>' ;

         $horse = $row['horse'];
         $date = $row['date']; 

        echo '</div><div style="clear:both"/>';

}
$result->close(); 
$result2->close();
$mysqli->close(); 

?>

</div>

公共TipsM/h1>

正如Moob已经说过的,看起来你的链接中已经有了正确的URL。要访问它,您可以执行以下操作:

$(“.button”).hover(函数(evt){
var url=$(evt.target).attr(“href”);
$.ajax({
url:url,
成功:功能(结果){
$(“#div1”).html(结果);
}
});
},函数(){
$(“#div1”).html(“”);
});

id
参数已在
a href
中。就用这个:

$(".wholikesbutton").hover(function () {
    var dataSource = $(this).attr("href");
    var dataDisplay = $(this).siblings(".wholikesdata");
    dataDisplay.text("fetching data from "+dataSource+"...");
    $.ajax({
        url: dataSource,
        cache: false
    })
    .done(function (html) {
        dataDisplay.html(html);
    })
    .fail(function (html) {
        dataDisplay.html("something went wrong (not suprisingly as the data source url doenst exist in this example)");
    });
}, function () {
    //seems a bit silly to actually remove the data when you could just hide it but if that's what you want...
    var dataDisplay = $(this).siblings(".wholikesdata");
    dataDisplay.text("");
});

我做了个模型。FWIW我不会每次用户悬停时都获取这些数据,除非数据可能会经常更改。

看起来您的链接中已经有了正确的id<代码>您的脚本不能使用它吗?您听说过javascript模板吗?您从PHP返回JSON,并将其嵌套在JS template.Moob中-如何将脚本包含到我的foreach循环中?每次我返回新行时都必须回显脚本吗?pbibergal-不,我没有,你有什么好的参考资料吗?@EdWright没有,你应该只有一个脚本对所有人都有效。它可以使用href中提供的url。不清楚的是,返回的“谁喜欢”数据是在单个通用元素中显示,还是在每个实例的单独元素中显示。好吧,公平点,但我的数据肯定会在每次不同的悬停时发生变化,因为我会在返回的每一行上传递不同的参数?是的,您的数据是不同的,但正如您将在每个实例中看到的那样,它们都在自己的实例中。