2013-05-18 30 views
0

我無法使用php代碼從我的phpMyAdmin數據庫中檢索,同時我加載使用javascript的map api。加載php mysql和javascript衝突

它只加載地圖的JavaScript。我有一些數據存儲在我的數據庫中。任何幫助表示讚賞。謝謝!

最終結果必須都是(我的數據庫數據&地圖api)能夠同時加載到屏幕上。

<!DOCTYPE html> 
<html> 
<body> 
<table border="1"> 
    <tr> 
    <th>Routes Available</th> 
    <th>Map Direction</th> 
    <th>Preferred Routes</th> 
    </tr> 
    <tr> 
    <td><div id="sidebar"> 
     <form name="form1" method="get" action="" onSubmit = "getDirections(this.from.value, this.to.value, this.mode.value); return false;"> 
     <p> 
      <label style="width:50px;display:inline-block;">From:</label> 
      <input type="text" size="34" name="from" value=""/> 
     </p> 
     <p> 
      <label style="width:50px;display:inline-block;">To:</label> 
      <input type="text" size="34" name="to" value=""/> 
     </p> 
     <p> 
      <label style="width:50px;display:inline-block;">Via:</label> 
      <select disabled name="mode" disabled> 
      <option value="c">Car</option> 
      </select> 
      <input type="submit" name="submit" value="Get Directions" style="margin-left:25px"/> 
     </p> 
     </form> 
     <?php 
    //PHP CODE STARTS HERE 
    if(isset($_GET['submit'])){ 
    $db_name="carpool"; 

    $con = mysql_connect("localhost","root",""); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db($db_name, $con); 

    $from=mysql_real_escape_string($_GET['from']); 
    $to=mysql_real_escape_string($_GET['to']); 

    $query_for_result=mysql_query(" 
    SELECT * 
    FROM routedetails 
    WHERE `from` LIKE '%".$from."%' 
    AND `to` LIKE '%".$to."%' 
    "); 

    echo "<table border=1 width=510>"; 
    echo "<tr>"; 
    echo "<td>"; 
    echo "<h2>From: Search Results</h2><ol>"; 
    $data_fetch=mysql_fetch_assoc($query_for_result); 
    while($data_fetch=mysql_fetch_assoc($query_for_result)) 
    { 
     echo "<li>"; 
     foreach($data_fetch as $row => $value){ 
      echo "$row - $value<br />"; 
       } 
     echo "</li><hr/>"; 
    } 
    echo "</tr>"; 
    echo "</td>"; 
    echo "</table>"; 

    echo "</ol>"; 

    mysql_close(); 
    } 

    ?> 
     <div></td> 
    <td><div id="map" style="width:600px;height:600px;margin-right:20px;float:left;"></div></td> 
    <td><div id="panel"></div> 
     <script type="text/javascript" src="http://gothere.sg/jsapi?sensor=false"></script> 
     <script type="text/javascript"> 
      gothere.load("maps"); 
      //var directions; 
      function initialize() { 
       if (GBrowserIsCompatible()) { 
        // Create the Gothere map object. 
        var map = new GMap2(document.getElementById("map")); 
        // Set the center of the map. 
        map.setCenter(new GLatLng(1.362083, 103.819836), 11); 
        // Add zoom controls on the top left of the map. 
        map.addControl(new GSmallMapControl()); 
        // Add a scale bar at the bottom left of the map. 
        map.addControl(new GScaleControl()); 


        // directions.load("from: Changi Airport to: Orchard Road"); 
        } 
        directions = new GDirections(map, document.getElementById("panel")); 
      } 

      function getDirections(from, to, mode) { 
       var travelMode; 

       directions.load("from: " + from + " to: " + to, {travelMode: travelMode}); 
      } 

      gothere.setOnLoadCallback(initialize); 
     </script></td> 
    </tr> 
</table> 
</body> 
</html> 

回答

1

您有無效的HTML代碼,你打開你的有序列表<ol>細胞<td><h2>...</h2><ol>裏面,但關閉它外面的表</table></ol>,而你的</tr></td>行後,關閉你的。你的輸出代碼目前看起來是這樣的 -

<table border=1 width=510> 
    <tr> 
     <td> 
     <h2>From: Search Results</h2> 
      <ol> 
      <li>$row - $value<br /></li> 
      <hr/> 
    </tr> 
     </td> 
</table> 
     </ol> 

此外,嘗試刪除$data_fetch=mysql_fetch_assoc($query_for_result);,因爲它是下一行while($data_fetch=mysql_fetch_assoc($query_for_result))

+0

您好的副本。感謝您的回覆。我已經刪除了$ data_fetch = mysql_fetch_assoc($ query_for_result);但它仍然不按我想要的方式工作(它只加載地圖api)。但它不會從我的數據庫中查詢以列出結果。 –

+0

我懷疑這是它加載onSubmit而不是方法=「get」的部分。 value,this.to.value,this.mode.value); return false;「> –

+0

因爲您在'onSubmit'中使用'return false;',頁面將不會重新加載,所以php代碼不會被執行。你需要提交頁面,並改變你如何調用'getDirections()',或者使用ajax來執行php代碼並插入到頁面中。 – Sean