简体   繁体   English

如何在AJAX URL中添加变量

[英]How can i add a variable in AJAX URL

I have a function in php that need an id and i need to add a variable in my ajax url the id 我在php中有一个需要ID的函数,我需要在我的Ajax URL中添加一个变量ID

PHP Code: 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 Code: Ajax代码:

this is just example of the variable of id. 这只是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

or is this code right? 还是这个代码正确吗?

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

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

+ is the concatenator in javascript. +是javascript中的串联器。 hope this helps. 希望这可以帮助。 spend plenty of time and add plenty of security to echoing that var into javascript. 花费大量时间并添加大量安全性,以将var回显到javascript中。 else you could find yourself viction of xss. 否则,您会发现自己对xss很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM