Passing an Ajax variable to Codeigniter

I think it is simple.

I have a Codeigniter function that takes input from a form and inserts it into the database. I want the Ajaxify process. At the moment, the first line of the function receives the id field from the form - I need to change this to get the id field from the Ajax message (which refers to a hidden field in the form containing the desired value). How should I do it?

My Codeigniter Controller Function

function add()
{
    $product = $this->products_model->get($this->input->post('id'));

    $insert = array(
            'id' => $this->input->post('id'),
            'qty' => 1,
            'price' => $product->price,
            'size' => $product->size,
            'name' => $product->name
        );

    $this->cart->insert($insert);
    redirect('home');
}

And jQuery Ajax Function

$("#form").submit(function(){
        var dataString = $("input#id") 
        //alert (dataString);return false;  
        $.ajax({  
            type: "POST",  
            url: "/home/add",  
            data: dataString,  
            success: function() {

            }  
        });
        return false;
    });

As always, thank you very much in advance.

+3
source share
2 answers
   $("#form").submit(function(){
        var dataString = $("input#id") 
        //alert (dataString);return false;  
        $.ajax({  
            type: "POST",  
            url: "/home/add",  
            data: {id: $("input#id").val()},  
            success: function() {

            }  
        });
        return false;
    });

Note the data parameter in the ajax method. Now you can use $this->input->post('id')how you do it in the controller method.

+5

url: "/home/add",

like

url: "/home/add/" + $("input#id").val(),

, codeigniter ...?

+3

Source: https://habr.com/ru/post/1744284/


All Articles