0

我想通过 jQuery 将 ID 传递给 urlstring 而不刷新 jQuery 和#标签,以便我可以通过 php$_get['ID']函数获取 ID。

但问题是

  1. 如果我使用#,页面没有得到刷新,但是 PHP 确实从 url 中选择了 ID。
  2. 如果我不使用#,那么 php 会选择 ID,但页面会刷新。

我想在不刷新页面的情况下将 id 传递给 php。

//javascript
function com(id) {
    location.href = '#?ID=id'; //by this way way,page doesn't refresh, ID appears in the url string but ID is not picked by  PHP as # is there
    // location.href = '?ID=id';   this way way, ID appears in the url string , ID is also  picked by  PHP as # is there.But page get refreshed.
    $.ajax({
        type: "Post",
        url: "getdata.php",
        async: true,
        cache: false,
        success: function(data) {},
    });
}​

 

//php for getdata.php
<?php  echo $_GET['ID'];    ?>
4

1 回答 1

2

您需要了解服务器端与客户端操作。Javascript是客户端。除非您发回一些信息,否则服务器(在您的情况下运行 PHP)对 javascript 正在做什么一无所知。这可以通过页面刷新或通过 ajax(简单地说)来完成。

您想要的是 ajax,它是一个返回到服务器的异步请求。然后服务器可以处理它并选择将信息传递回页面。查看 jQuery 的ajax

根据您更新的评论进行更新:

function com(id) {
    //forget about appending the id. This isn't doing anything.
    //You can use it for informational purposes or so that if someone cuts and pastes
    //the link you can handle it server side appropriately.
    location.href = '#?ID=id';

    //instead focus on this call. append your query string to the url    
    $.ajax({
        type: "POST",
        url: "getdata.php?ID=12345",
        async: true,
        cache: false,
        success: function(data) {
            alert(data); //should alert the id processed by php
        },
    });
}​
于 2012-12-01T06:46:47.380 回答