Welcome Guest, Not a member yet? Register   Sign In
How to get the jSON data value and pass it to a Conroller?
#1

[eluser]rochellecanale[/eluser]
just want to ask about my problem. I am a beginner in using jquery. My problem is I want to get the json string from my jquery ajax request but I don't know how. Because I want to check if the json string values are correct.

Here's my code in jquery ajax:

Code:
$('#search-btn').on('click',function(){

            var query = $("#keyword").val();

            var image = "<?php echo base_url()."/resources/loading/loading43.gif"; ?>";

            $('#loading').html("<img src='"+image+"' class='loeader' align='center' />");

            var query_url = "&lt;?php echo site_url('item_controller/searchItem'); ?&gt;";

            $.ajax({

                type:'POST',
                url: query_url,
                data:{query: $("#keyword").val(), data_filter: $("#keyword").attr("data-selection")},    //how can I get the value of data_filter? and pass it to the controller?
                dataType:'json',
                async: false,
                success:function(d){
                      //some codes for success. . .

             },
});




$("#selection_filter").on('change',function(){

    var filter = $("#selection_filter").val();

    $("#keyword").attr("data-selection",filter);
});

This is the code for the events.

Code:
<table border="1"  100%; align: left" cellpadding="10" cellspacing="10" align="left">
            <tr>
                <td colspan="3">
                    <h5>SEARCH ITEM</h5>
                </td>
            </tr>
            <tr>
                <td  15%">
                    <label>Choose search filter: </label>
                </td>
                <td  25%">
                    <select id="selection_filter">
                        <option value="code">ITEM CODE</option>
                        <option value="itemname">ITEM NAME</option>
                    </select>
                </td>
                <td>
                    &lt;input type="text" name="keyword" id="keyword"  80%" data-selecti /&gt; &lt;input type="button" value="SEARCH" id="search-btn" class="k-button"  12px" /&gt;
                </td>
            </tr>
        </table>

This is my code for accessing the controller

Code:
public function searchItem(){

            $query = $this->input->post('query');
            $filter = $this->input->post('data_filter');

            if($filter == "code"){
                $querySearch = "SELECT item_code,item_name from items WHERE item_code LIKE '%".$query."%' GROUP BY item_code";
            }else{
                $querySearch = "SELECT item_code,item_name from items WHERE item_name LIKE '%".$query."%' GROUP BY item_code";
            }

            $resultSearch = $this->db->query($querySearch);
            $count = $resultSearch->num_rows();

            echo json_encode($resultSearch->result_array());
            //echo json_encode($querySearch);

        }

What I want to get is:

In my ajax. There is a data that contains the value of keyword and data filter. Now What I need to do is get the data_filter value and pass it to the controller.
#2

[eluser]stefenw[/eluser]
Quote:Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

i use this

returning json from controller
Code:
$x = $this->input->post('x');
echo json_encode(array("datacallback" => array("1" => $x)));

if result as row
Code:
$.ajax({
                        type: "POST",
                        url: //url target,
                        data: {x  : "abc"},//to add more ada
                        dataType: "json"
                    }).done(function(data){
var x = data.datacallback.1;
}

x will contain abc

if you need to loop trough returned json use this

Code:
$.each(data.datacallback, function(k, item) {
                    alert (item.arraykeyname)
                });




Theme © iAndrew 2016 - Forum software by © MyBB