Php Jquery将错误的值传递到页面
我已经为此挣扎了好几天了。这可以工作,但不会将正确的变量传递到testMap.php页面。我使用数据库中的数据,将鼠标悬停在链接上确实会在URL中显示正确的变量,但不管出于什么原因,jquery总是捕获循环中的第一个变量。有什么建议吗Php Jquery将错误的值传递到页面,php,jquery,Php,Jquery,我已经为此挣扎了好几天了。这可以工作,但不会将正确的变量传递到testMap.php页面。我使用数据库中的数据,将鼠标悬停在链接上确实会在URL中显示正确的变量,但不管出于什么原因,jquery总是捕获循环中的第一个变量。有什么建议吗 <?php $myname = $_SESSION['username']; global $database; $stmt = $database->connection->query("SELECT * FROM ".
<?php
$myname = $_SESSION['username'];
global $database;
$stmt = $database->connection->query("SELECT * FROM ".TBL_FLIGHTS." WHERE username='$myname'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$date = $row['date'];
$starting = $row['starting'];
$ending = $row['ending'];
$route = $row['route'];
echo "<a class=\"route\" href=\"start=$starting&end=$ending\"><p class=\"pBlue\">$date - $starting - $ending - $route</p></a>";
?>
<div id="start" style="visibility:hidden"><?php echo $starting; ?></div>
<div id="end" style="visibility:hidden"><?php echo $ending; ?></div>
<?
}
?>
<script type="text/javascript">
$(document).ready(function() {
var start = $('#start').text();
var end = $('#end').text();
$(function() {
$(".route").click(function(evt) {
$("#mymap_canvas").load("testMap.php?start="+start+"&end="+end )
evt.preventDefault();
})
})
});
</script>
的确;您需要捕获单击事件中的开始值和结束值,并选择单击的路线附近的开始值和结束值 可能是你想要的:
$(".route").click(function(evt) {
var start = $(this).siblings(".start").text();
var end = $(this).siblings(".end").text();
$("#mymap_canvas").load("testMap.php?start="+start+"&end="+end )
evt.preventDefault();
}); // I believe you are missing a semicolon here and at the end, too
但是,除非我遗漏了一些东西,否则您可以像以前一样通过php构建href,只需使用完整路径和查询字符串完成它,然后像这样执行它:
$(".route").click(function(evt) {
// Load the href of the route anchor into 'mymap_canvas'
$("#mymap_canvas").load($(this).attr("href"));
event.preventDefault();
return false; // Prevent the normal click behavior
});
<a class=\"route\" href=\"testMap.php?start={$starting}&end={$ending}\">...</a>
没有更多隐藏的开始和结束!但请确保将原始锚定标记更新为如下所示:
$(".route").click(function(evt) {
// Load the href of the route anchor into 'mymap_canvas'
$("#mymap_canvas").load($(this).attr("href"));
event.preventDefault();
return false; // Prevent the normal click behavior
});
<a class=\"route\" href=\"testMap.php?start={$starting}&end={$ending}\">...</a>
ID必须是唯一的。该循环告诉我,您正在创建多个ID为start和end的元素,这是错误的。例如,变量$starting all value:$starting=khiiklaskord start应该仅等于khii。谢谢你的帮助:你想要什么,请简单介绍一下我需要$开始显示该查询的结果,以及$结束显示结果。实际情况是,jquery只传递数据库查询的第一个结果。@MohitBumb;我不认为这是应该做的;$i在php for循环中向上变化,该循环不围绕底部的jQuery部分$i在document.ready中只是最后一个元素,而不是第一个元素。好的,在开始时,我写start1,它返回第一个值,在结束时,我写$i,它返回循环结束时的值:p