PHP-AJAX:如何通过Ajax传递URL变量 [英] PHP-AJAX: How to pass URL variable through Ajax

查看:74
本文介绍了PHP-AJAX:如何通过Ajax传递URL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为分页设置了Ajax调用.我需要再传递一个存储在URL中的空缺

I have set an Ajax call for Pagination. I need to pass one more vaiable which is stored in the URL

URL

http://thisite.com/pagetitl/?id=12  **// where 'id=' is a variable I want to pass.**

Ajax呼叫

function page(page) {
    var dataString = '&page=' + page; // pagination with ajax
    pag.ajax({
        type: "GET",
        url: "cmn_pg.php",
        data: dataString,
        success: function (ccc) {
            pag("#search_results").html(ccc);
        }
    });
}

我试图在PHP文件$id=$_GET[id]中获取它,但无法正常工作.

I have tried to GET it in PHP file $id=$_GET[id], but wont work.

我问如何通过AJAX传递它,因为我对AJAX还是很陌生.

I ask how to pass it with AJAX because I'm quite new to AJAX.

推荐答案

如果要手动构建查询字符串,则:

If you are building your query string manually then:

dataString = 'page=' + encodeURIComponent(page);

但是您使用的是jQuery,因此请不要手动进行构建:

but you are using jQuery, so don't build it manually:

url: "cmn_pg.php",
data: {
    "page": page
},
success: function (ccc) {

(您还需要在PHP中为其使用正确的名称:<?php $id = $_GET['page'] ?>)

(You also need to use the right name for it in PHP: <?php $id = $_GET['page'] ?>)

这篇关于PHP-AJAX:如何通过Ajax传递URL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆