I'm able to get jQuery AJAX live search working on an input field and it returns the desired result that I want.
for example : Search for my word returns the desired result .
But Now I want to do a compound search on two fields.
for example :
Search for the word + today
or
Search for the word + tomorrow
This is what I have but it is not working or does not returning any errors.
index.php
<form>
<input placeholder="Search the area" class="input-large search-query" type="text" id="key">
<input type="radio" name="b_selected" class="radio" value="1" > Today
<input type="radio" name="b_selected" class="radio" value="2" > Tomorrow<br>
<div class="result"><div class="loading"></div></div>
</form>
quicksearch.php
include('config.php');
$count= 0;
if(isset($_POST["b_selected"]))
{ $b_selected = $_POST['b_selected']; }
$key = $_POST['key'];
$b_selected = $_POST['b_selected'];
$key = addslashes($key);
$sql = mysql_query("select * from account WHERE b_today='1' and title LIKE '%$key%' ") or die(mysql_error());
While($row = mysql_fetch_array($sql)) {
$count++;
$id= $row['id'];
$title=$row['title'];
if($count<= 10){
search.js
/*quick search*/
$(document).ready( function() {
$(".result").hide();
$("#key").keyup( function(event){
var key = $("#key").val();
if( key != 0){
$.ajax({
type: "POST",
data:{ key: key },
url:"quicksearch.php",
success: function(response) {
$(".result").slideDown().html(response);
}
})
}else{
$(".result").slideUp();
$(".result").val("");
}
})
})
/* Search for work plan for today or Tomorrow*/
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var b_selected = $(this).val();
$.ajax({
url:"quicksearch.php",
method:"POST",
data:{b_selected:b_selected},
success:function(data){
$('#result').html(data);
}
});
});
});
You should POST both variables - the word plus the value of the radio-button. If you POST only the word or only the radio-button - you won't get what you want:
$(document).ready( function()
{
$(".result").hide();
/*quick search*/
$("#key").keyup( function()
{
var key = $(this).val();
var b_selected = $('input[type="radio"]').val();
$.ajax({
method: "POST",
url: "quicksearch.php",
data:
{
key: key,
b_selected: b_selected
},
success: function(response)
{
$(".result").slideDown().html(response);
}
});
});
/* Search for work plan for today or Tomorrow*/
$('input[type="radio"]').click(function()
{
var key = $('#key').val();
var b_selected = $(this).val();
$.ajax({
method: "POST",
url: "quicksearch.php",
data:
{
key: key,
b_selected: b_selected
},
success: function(response)
{
$('#result').slideDown().html(response);
}
});
});
})
Related
I wanna create array from ajax response. I took array from ajax and i have to create dynamic data for chips
materializecss autocomplete
response from ajax
var tag = '';
$(document).on('keyup', '.tags', function() {
var _this = $(this).val();
if(_this.length > 1) {
$.ajax({
url: '/story/searchTags',
type: 'POST',
dataType: 'JSON',
data: {
tag: _this
},
success: function(tags) {
tag = tags;
}
});
}
});
$('.chips-placeholder').chips({
placeholder: lang_('Your tag'),
secondaryPlaceholder: '+ tag',
autocompleteOptions: {
// I need data like this from response
data: {
'Google', null
'Googol', null
},
limit: 5,
minLength: 2
}
});
<div class="chips chips-placeholder chips-autocomplete input-field" style="background:#fff">
<input class="input tags" placeholder="tag" autocomplete="off" />
</div>
that's my code
Finally, thanks to #Vel to find right answer.
jsfiddle - work code
You need to do like this.
var tag = '';
$(document).on('keyup', '.tags', function() {
var _this = $(this).val();
if(_this.length > 1) {
/*$.ajax({
url: '/story/searchTags',
type: 'POST',
dataType: 'JSON',
data: {
tag: _this
},
success: function(tags) {
tag = tags;
}
});*/
// ['google', 'googol'] <- this array I take from ajax
tag = ['google', 'googol'];
var obj = {};
$(tag).each(function (index, value) {
obj[value] = null;
});
$('.chips').chips({
secondaryPlaceholder: '+ tag',
autocompleteOptions: {
data:obj,
}
});
}
});
fiddle link
I have a loop:
do {
// show items
<input type="hidden" id="contestant_id" value="'.$row_contestants['contestant_id'].'">
<input type="hidden" id="user_id" value="'.$userinfo['user_id'].'">
<button class="vote" onclick="vote()">vote</button>
<div id="result"></div>
} while ($row....);
And my function:
function vote () {
var val1 = $('#contestant_id').val();
var val2 = $('#user_id').val();
$.ajax({
url:"vote.php",
type: "POST",
data: { contestant_id: val1, user_id: val2 },
success: function(response) {
$('#result').html(response);
}
});
}
What I want to do is have the "vote" text from here:
<button class="vote" onclick="vote()">vote</button>
change to something else like "voted!" when I get the response. I sort of had it but of course it's showing here:
<div id="result"></div>
And it's only showing under the 1st result of my loop.
Any ideas?
Thanks.
You can pass this from html to function and handle in the script
<button class="vote" onclick="vote(this)">vote</button>
function vote (el) {
var val1 = $('#contestant_id').val();
var val2 = $('#user_id').val();
$.ajax({
url:"vote.php",
type: "POST",
data: { contestant_id: val1, user_id: val2 },
success: function(response) {
$('#result').html(response);
if (response == 'Yes') {
var text = el.text();
var values = text.split(' ');
var count = 1;
if (values.length == 2) {
count = values[1].parseInt + 1;
}
el.text('Voted ' + count);
}
}
});
}
I have a page with list of buttons, when each button is clicked, it's value is captured and ajax call in made. PHP does DB updates on ajax call. I want to return data to ajax call. The data is obtained from DB. But I'm unable to point out what's the error in below code.
Here is PHP code:
if (isset($_GET['val']))
{
$chapter_id=$_GET['val'];
$sql= "SELECT file_name,edit_link,name,email FROM `chapter_list` where chapter_id='$chapter_id'";
$result = mysql_query($sql,$rst);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$vol_name = $row["name"];
$vol_email= $row["email"];
$vol_link= $row["edit_link"];
}
$update=mysql_query("UPDATE `chapter_list` SET `status` = '$update_status' WHERE `chapter_list`.`chapter_id` = '$chapter_id';");
header('Content-Type: application/json');
echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));
}
Here is the AJAX request
$(document).ready(function(){
$('.btn').click(function(){
var clickBtnValue = $(this).val();
$.ajax ({
url: '',
data: { val : clickBtnValue},
dataType:'JSON',
success: function(res) {
alert(res.name);
}
});
});
});
I'm not getting the alert!
Try like this.
Maybe response data is null.check your php code(query lines).
Here My php code is :
if (isset($_GET['val'])) {
$vol_name = 'dummy_name';
$vol_email = 'dummy_email';
$vol_link = 'dummy link';
header('Content-Type: application/json');
echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));
exit;
}
My javascriptcode is :
<input type="text" class="btn" value="test" />
<script type="text/javascript">
if('addEventListener' in document){
document.addEventListener("DOMContentLoaded", function(e){
//dom loaded
$(document).on("click",".btn",function(e){
e.preventDefault()
var e_val = $(this).val();
console.log('my value is :' + e_val);
if(e_val){
$.ajax({
type: "get",
dataType: 'json',
url: 'here your url or slash',
data: { // request e_val
val : e_val,
}
}).done(function(xhr) {
// console.log(xhr);
if(xhr.name){
alert('response data is '+ xhr.name);
}
})
}
})
},false)
}
</script>
try this..
while($row = mysql_fetch_assoc($result))
{
$vol_name = $row["name"];
$vol_email= $row["email"];
$vol_link= $row["edit_link"];
$ret[$vol_name]= array(
'email'=>$vol_email,
'link'=>$vol_link
);
}
then use in the return statement..
echo json_encode($ret);
You can send parameters in HTML
<button class="btn" atribute_id="21543">Button</button>
$(document).ready(function() {
$('.btn').click(function() {
var Value_of_Btn= $(this).attr("atribute_id"); <-------
$.ajax({
url: '',
data: {
val: clickBtnValue
},
dataType: 'JSON',
success: function(res) {
alert(res.name);
}
});
});
});
I am using the jquery plugin DATATABLE.
I want to get a single value from a selected row in my datatable (the ID) but i dont how to do so. The value should be saved and given to a textbox.
Here is my Code:
var oTable = $('#dataTable').dataTable();
$.ajax({
url: 'process.php?method=fetchdata',
dataType: 'json',
success: function(s){
console.log(s);
oTable.fnClearTable();
for(var i = 0; i < s.length; i++) {
oTable.fnAddData([
s[i][0],
s[i][1],
s[i][2],
s[i][3],
s[i][4],
]);
}
},
error: function(e){
console.log(e.responseText);
}
});
$('#dataTable tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
oTable.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
Hopefully someone can help me!
in php:
$id = $_POST['id'];
$row = mysql_query("select * from _yourtable_ where id='$id'");
...
in the js:
You must send id to the file process.php by POST or GET method:
id = $('#textBoxSelector').val();
$.ajax({
url: 'process.php?method=fetchdata&id='+id,
method: 'post',
dataType: 'json',
success: function(s){
console.log(s);
...
}
});
I am developing a small applicatopn using php with ajax to get place when user enter a pincode. Now I'm shore to my aim, but now I am getting some unwanted results but incluing the actual result.
This is my code...my html code is given below
<label for="pincode">Pin-Code:</label>
<input name="pincode" type="text" class="text" id="pincode" /><div id="section1"></div>
and my javascript code is
<script>
$(document).ready(function() {
$('#pincode').keyup(function() {
//ajax request
$.ajax({
url: "pincode_check.php",
data: {
'pincode' : $('#pincode').val()
},
dataType: 'json',
success: function(data) { <!--console.log(data.success);-->
if(data.success){
$.each(data.results[0].address_components, function(index, val){
console.log(index+"::"+val.long_name);
/*alert(index+"::"+val.long_name); */
$('#section1').append( val.long_name);
});
}
},
});
});
});
</script>
in pincode_check.php
<?php
$pincode=$_REQUEST['pincode'];
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$pincode.'&sensor=false');
$response= json_decode($geocode); //Store values in variable
$lat = $response->results[0]->geometry->location->lat; //Returns Latitude
$long = $response->results[0]->geometry->location->lng; // Returns Longitude
$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$lat.','.$long.'&sensor=false');
$data= json_decode($geocode);
if($data==true)
{ // Check if address is available or not
$data->results[0]->formatted_address ;
$data->success=true;
echo json_encode($data);
}
else {
$data->success= false;
echo json_encode($data);
}
?>
When i enter a pincode , for eg: 690561
The output is
Les JumeauxCourzieuRhĂ´neRhone-AlpesFrance6967015Heilige HuisjesZevenaarZevenaarGelderlandThe Netherlands6905 AAAnayadi Edakkad RdThottuvaPallickalKollamKeralaIndia690561Yitzhak Rabin HighwayIsraelYitzhak Rabin HighwayIsrael328BoulevardAndersonAnderson CountySouth CarolinaUnited States29621164Lenina avenueOrdzhonikidzevs'kyi districtZaporizhiaZaporiz'ka city councilZaporiz'ka oblastUkraine
But I need only AAAnayadi Edakkad . Please help me to filter out this output.
Kindly check this your pin code and press enter key
<script>
$(document).ready(function() {
$('#pincode').keyup(function (e) {
if (e.keyCode == 13) {
//ajax request
$.ajax({
url: "pin_request.php",
data: {
'pincode' : $('#pincode').val()
},
dataType: 'json',
success: function(data) { <!--console.log(data.success);-->
if(data.success){
//console.log(data.results[0].formatted_address.split(','))
var long_address=data.results[0].formatted_address.split(',');
console.log(long_address[0]);
$('#section1').append(long_address[0]);
}
}
});
}
});
});
</script>