doulin6088 2012-06-27 12:18
浏览 70
已采纳

通过jQuery-AJAX load()函数将PHP变量作为变量传递给JavaScript

I have 2 files, main.html and search.php.

Within the main.html page, there is this piece of code :

$("#results").load("search.php",queryString,function(response,status,xhr){
... some code here
})

Within the search.phppage, there is this piece of code :

$image_id=mysql_insert_id() // getting the last ID of the last query - works perfectly

What I want to do is to echo this variable to the main.html file and store it within a JS variable called im_id that I can call through the main.html page.

I tried to do this

echo "<script type=\"text/javascript\">$im_id=".mysql_insert_id()."</script>";

but it doesn't work at all.

I found a workaround which is to store the content within a textbox like this

echo "<script type=\"text/javascript\">$(\"#textbox\").val(".$image_id.")</script>";

and then in the JScript, use

$im_id=$("#textbox").val();

and many other methods using an "intermediary" but isn't there a straight way to set the variables directly ?

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dp9599 2012-06-27 12:21
    关注

    Its not working because you output $im_id=1, you should output var im_id = 1; instead.

    In your PHP:

    echo "<script type=\"text/javascript\">var im_id = ".mysql_insert_id().";</script>";
    

    If it still doesnt work look at the generated code, not the PHP source. Just go to view source in your browser, and use a debugger to find out whats wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿