如何在AJAX URL中添加变量

时间:2014-04-03 03:10:33

标签: javascript php ajax codeigniter

我在php中有一个需要id的函数,我需要在我的ajax url中添加一个变量id

PHP代码:

function get_json_selected($purpose) 
    {
        //echo $this->input->post("ids");
        $ids = explode(",", $this->input->post("ids"));
        $site_url = site_url($this->router->class);
        if ($purpose == "EQUIPEMENT"){
            $this->db->select(
                               'a.id,
                                a.manufacturer,
                                a.description,
                                a.serial_no,
                                a.part_no,
                                a.status, 
                                a.availability,
                                getReturnStatus(a.id) as return_status',
                                FALSE
                            );

            $this->db->where_in('a.id', array_unique($ids));

            $result = $this->db->get("equipments a")->result_array();
            echo json_encode(array("spares" => $result));
        } else {
            $this->db->select(
                               'a.id,
                                a.manufacturer,
                                a.description,
                                a.serial_no,
                                a.part_no,
                                a.status, 
                                a.availability,
                                getReturnStatus(a.id) as return_status',
                                FALSE
                            );

            $this->db->where_in('a.id', array_unique($ids));

            $result = $this->db->get($this->active_table." a")->result_array();
            echo json_encode(array("spares" => $result));
        }


    }   

Ajax代码:

这只是id变量的示例。

$purpose = "EQUIPMENT"; // how can i add this php variable to ajax url

url: "<?=site_url('equip_request/get_json_selected');?>", // this is the current code how can i add id in this url

或者这个代码是对的吗?

url: "<?=site_url('equip_request/get_json_selected/'.$purpose);?>"

1 个答案:

答案 0 :(得分:0)

var purpose = '<?php echo json_encode($purpose); ?>';

url: 'example.php?=' + purpose;

+是javascript中的连接符。希望这可以帮助。花费大量时间并添加足够的安全性来将var回应到javascript中。否则你会发现自己对xss的看法。