【问题标题】:php mysql and javascript conflict in loadingphp mysql和javascript加载冲突
【发布时间】:2013-05-18 06:59:15
【问题描述】:

当我加载使用 javascript 的地图 api 时,我无法使用 php 代码从我的 phpMyAdmin 数据库中检索。

它只加载地图 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>

【问题讨论】:

    标签: php javascript mysql map conflict


    【解决方案1】:

    您的 html 代码无效,您在单元格 &lt;td&gt;&lt;h2&gt;...&lt;/h2&gt;&lt;ol&gt; 内打开有序列表 &lt;ol&gt;,但在表 &lt;/table&gt;&lt;/ol&gt; 外关闭它,并且您在行 &lt;/tr&gt;&lt;/td&gt; 之后关闭单元格。您的输出代码当前如下所示 -

    <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)) 重复

    【讨论】:

    • 您好。感谢您的回复。我已删除 $data_fetch=mysql_fetch_assoc($query_for_result);但它仍然不能按我想要的方式工作(它只加载地图 api)。但它不会从我的数据库中查询以列出结果。
    • 我怀疑这是加载 onSubmit 而不是 method="get"
    • 由于你在onSubmit中使用了return false;,所以页面不会重新加载,所以php代码也不会被执行。您要么需要提交页面,然后更改调用 getDirections() 的方式,要么使用 ajax 执行 php 代码并插入到页面中。
    • 感谢肖恩的建议。但那是建议的api。更改其中的某些内容后,它只是无法加载...知道如何更改它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多