doumeitang572461 2010-11-24 12:37
浏览 56

Javascript PHP同步问题

I want to call a javascript function with parametars that are stored in MySQL. All this is happening on an onClick event.

Here is the javascript code:

    function getFile() {
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("POST", "gmap.php", false);                             
     AJAX.send("searchField=" + searchField.value);
     return load(AJAX.responseText);   

  } else {
     return false;
  }                                             
}

So, the gmap.php is echoing the parameters for the javascript load function. But it doesn't load the parameter because the function is called before the MySQL query in gmap.php is executed. I've tried sync and async AJAX. If I try to call the javascript function from PHP, it doesn't get executed, because it is called on a onClick event, and this is inside a div. Please help me, I'm doing this over a week now. I've tried everything.

Here is the php code with the MySQL query:

<?php
header( 'Content-Type: text/html; charset=UTF-8' );
mb_internal_encoding( 'UTF-8' );
$a = $_POST['searchField'];
$dbhost = "localhost";
$dbuser = "*******";
$dbpass = "*******";
$dbname = "citydb";



    //connect sql
mysql_connect($dbhost, $dbuser, $dbpass);

    //select db
mysql_select_db($dbname) or die(mysql_error());
    //retrieve data
//$city=$_GET['city'];
    //escape user input to help prevent SQL injection
//$city=mysql_real_escape_string($city);
    //query
mysql_query('SET CHARACTER SET utf8');
$result=mysql_query("SELECT citystart, cityend FROM cityids WHERE city='$a' ");
if(!result) {

    die("Database query failed: " . myql_error());
    }

while($row=mysql_fetch_array($result)) {


    $lat=$row['citystart'];
    $lng=$row['cityend'];


}


    echo $lat;
    echo ", ";
    echo $lng;



?>
  • 写回答

3条回答 默认 最新

  • dpikoto468637 2010-11-24 12:51
    关注

    pass the php url together with the variable to search.

    if (AJAX) {
         var url = "gmap.php?searchField="+ searchField.value;
         AJAX.open("POST", "url", false);                             
         AJAX.send(true);
         return load(AJAX.responseText);   
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料