简体   繁体   中英

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 Code:

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:

this is just example of the variable of 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. hope this helps. spend plenty of time and add plenty of security to echoing that var into javascript. else you could find yourself viction of xss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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