2012-04-16 92 views
1

我一直在爲此掙扎好幾天。這可以工作,但不會將正確的變量傳遞給頁面(testMap.php)。我使用我的數據庫中的數據,並懸停在鏈接上顯示URL中的正確變量,但無論出於何種原因,jquery總是在循環中獲取第一個變量。有什麼建議麼?jquery將錯誤的值傳到頁面

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

ID必須是唯一的。該循環告訴我,你正在創建多個ID開始和結束的元素,這是錯誤的。 – 2012-04-16 12:36:04

回答

0

事實上,您需要捕獲點擊事件中的開始和結束值,並選擇點擊路徑附近的開始值和結束值。

也許你想要什麼:

$(".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> 
0
<?php 
    $myname = $_SESSION['username']; 
    global $database; 
    $stmt = $database->connection->query("SELECT * FROM ".TBL_FLIGHTS." WHERE username='$myname'"); 
$i=1; 
     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 class="start<?php echo $i; ?>" style="visibility:hidden"><?php echo $starting; ?></div> 
     <div class="end<?php echo $i ?>" style="visibility:hidden"><?php echo $ending; ?></div> 
     <? 
    $i++; } 

    ?> 


<script type="text/javascript"> 
$(document).ready(function() { 
    var start = $('.start1').text(); 
    var end = $('.end<?php echo $i; ?>').text(); 

    $(function() { 
     $(".route").click(function(evt) { 
     $("#mymap_canvas").load("testMap.php?start="+start+"&end="+end) 
     evt.preventDefault(); 
     }) 
    }) 

}); 
</script> 
+0

這給出變量$開始所有值:$ starting = khiiklaskord例如,開始應該等於khii。感謝您的幫助:) – azpilot 2012-04-16 12:47:10

+0

你確切想要什麼請簡單多一點 – 2012-04-16 12:48:49

+0

我需要$開始顯示該查詢的結果,$結束以顯示結果。發生的事情是,jquery只傳遞數據庫查詢的第一個結果。 – azpilot 2012-04-16 12:54:24

相關問題