duanlianyun0462 2011-07-30 22:07
浏览 116
已采纳

如何通过AJAX在URL中发送变量?

I'm sending the variable like this:

xmlhttp.open("GET","insert-karma.php?pid=<? echo $pid; ?>",true);

The AJAX part doesn't seem to be the problem, if I check the Sript the PHP is echoing the integer correctly. (It displays the url as: insert-karma.php?pid=5). However the PHP isn't getting the variable. (I tried echoing and it does't show anthing)

This is the PHP file:

// Connect to db;

$pid = $_POST['pid'];

$sql="UPDATE Poems SET Karma = Karma + 1 WHERE Pid = '$pid'";

// Disconnect form database;

What am I doing wrong? How do I manage to send $pid to the PHP update-karma.php?

  • 写回答

4条回答 默认 最新

  • douhuigang9550 2011-07-30 23:06
    关注

    first you should not use PHP in you ajax requests it's just make things more complicated and PHP is for server side scripting in the first place secound , you should use xmlhttp.open("POST","insert-karma....) if u plain to use POST Third the only important difference (not the only but the important) between POST and Get is :

    GET requests can be cached

    GET requests can remain in the browser history

    GET requests can be bookmarked

    GET requests can be distributed & shared

    GET requests can be hacked lool

    so u cant use Get For unsecured and dangerous action like LOGIN OR ...

    POST Can handel too long Data

    Post is more secured Cuz it's not gonna be cached or saved in history or bookmarked

    u can clearly notice that POST's dont display in the browsers address bar but Get do 'www.anything.com/index.php?bla=bhe'

    i hope that i am helping here !! :)

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

报告相同问题?