繁体   English   中英

如何为每个循环将我的JavaScript添加到我的php中?

[英]How can I add my javascript to my php for each loop?

如何为每个循环将以下脚本添加到我的php中? 以便who_likes?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>

然后这个文件

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

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我不会在每次用户徘徊时都获取此数据,除非数据可能频繁地变化。

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

$(".button").hover(function (evt) {
    var url = $(evt.target).attr("href");

    $.ajax({
        url: url,
        success: function (result) {
            $("#div1").html(result);
        }
    });
}, function () {
    $("#div1").html("");
});

暂无
暂无

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

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