[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 = "<?php echo site_url('item_controller/searchItem'); ?>";
$.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>
<input type="text" name="keyword" id="keyword" 80%" data-selecti /> <input type="button" value="SEARCH" id="search-btn" class="k-button" 12px" />
</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.