Ikram Shabri
Ikram Shabri

Reputation: 567

Get value to php hyperlink in ajax response

I want to make link action to controller in codeigniter after get response from ajax. I need a variable from ajax response to put in the link to controller, and then in controller, I need to operate update process. I've tried using php link but it doesn't work. Error shows The URI you submitted has disallowed characters.

Here's the script

          $.ajax({
              type : "GET",
              url  : "<?php echo admin_url().'leads/data_status'; ?>",
              success : function(response2){
                 var data2 = JSON.parse(response2);
                 var html = '';
                 var a;

                 for(a=0; a<data2.length; a++)
                 {

                    html += '<tr>'+
                                '<td>'+data2[a].name+'</td>'+
                                '<td>'+data2[a].company+'</td>'+
                                '<td><a href="<?php echo admin_url().'leads/trash/status='?>'+data2[a].status+'&id='+data2[a].id+'">Back</a></td>'+
                             '</tr>';

                 }

              }
           })

Here's controller script

public function trash(){
    $id=$this->input->get('id');
    $status=$this->input->get('status');

    $data = array(
        'status' => $status,
        'last_status' => null
    );

    $this->db->where('id',$id);
    $this->db->update(db_prefix() . 'leads', $data);

    redirect('admin');
}

Do you know how to fix the code ?

Thanks

Upvotes: 0

Views: 226

Answers (1)

Prateik Darji
Prateik Darji

Reputation: 2317

There are concatination issues in your code, I have udpated the string can you please try with this one

html += '<tr>'+
    '<td>'+data2[a].name+'</td>'+
    '<td>'+data2[a].company+'</td>'+
    '<td><a href="<?php echo admin_url();?>leads/trash/status='+data2[a].status+'&id='+data2[a].id+'">Back</a></td>'+
'</tr>';

or with storing url in a variable like

url = "<?php echo admin_url();?>leads/trash/";
url += 'status='+data2[a].status;
url += '&id='+data2[a].id;

html += '<tr>'+
    '<td>'+data2[a].name+'</td>'+
    '<td>'+data2[a].company+'</td>'+
    '<td><a href="'+encodeURIComponent(url)+'">Back</a></td>'+
'</tr>';

don't forget to use urldecode in php if you use encodeURIComponent

$id=urldecode($this->input->get('id'));
$status=urldecode($this->input->get('status'));

Upvotes: 1

Related Questions