get result with jquery ajax from autocomplete - php

I'm using jQuery autocomplete and want to retrieve data from php file called some.php, in which is only written
<?php
echo "Hello World";
?>
and here is javascript
<script type="text/javascript">
$(function(){
$("#key").autocomplete({
source: function(){
$.ajax({
type: "POST",
url: "./some.php",
data: {key: $("#key").val()},
success: function(result){
// what code is needed here to be placed
}
});
}
});
});
</script>
and html by the way =>
<input type="text" name="key" id="key">
I think script is written correctly , because when I'm writing in success function alert(result) it gets "Hello world", but I want it to be in drop down box , how can fix this problem , please help me , thanks :)

$("#key").autocomplete({
source: function(request, response){
$.ajax({
type: "POST",
url: "./some.php",
data: {key: request.term},
success: function(result){
response(result);
}
});
}
});
response(result) will display the autocomplete menu -- result should be an array of items (where each item is a string or an object with value or label keys).
As SJ GJ mentioned, you can simply set source: "./some.php" but then some.php needs to be modified to accept the request parameter term and return a json array of items.

try this:
JS:
<script type="text/javascript">
$(function(){
$("#key").autocomplete({
source: "./some.php"
});
});
</script>
PHP:
$result = array('1' => 'Hello World');
echo json_encode($result);

Related

Passing javascript data to jquery ajax

I have a click event under document.ready function like this
<script type= "text/javascript">
$(document).ready(function(){
var roll;
var course_code;
$('#table').on('click', 'button', function(){
var obj = JSON.parse($(this).val());
course_code = obj.course_code;
roll = obj.roll;
$.ajax({
type : "POST",
dataType: "html",
url : "./card.php",
data : {"course_code":course_code,"roll":roll},
success : function(returndata){
$("#modal-body").html(returndata);
}
});
$("#myModal").modal('show');
});
});
</script>
card.php code
<?hph
echo '<pre>';
echo htmlspecialchars(var_dump($_POST));
echo '</pre>';
?>
the output from the card.php is
C:\wamp\www\card.php:3:
array (size=0)
empty
Why i am not been able to send the data to card.php
what is the correct solution
If your version of JQuery is more than 1.9.0 you need to replace TYPE to METHOD in your Ajax request.

Updating page using AJAX

On my main page I have a page called logs.php which I have set to update every 4 seconds using jquery/ajax. This will update the details of an .xml file which is stored locally.
file_put_contents("../../scripts/xml/logs.xml", $dom) or print_r(error_get_last());
Here is the jQuery:-
$(function(){
setInterval(function(){
$.ajax({
url:'scripts/php/log.controller.php',
success:function(data){
if(data == "scripts/php/status.controller.php"){
}else{
alert(data);
}
}
});
},4000);
});
When I alert(data); it shows me the changes of the updated XML file:
<?xml version="1.0"?>
<loglist resultcount="24"><log id="0"><messagedate>20/06/2014 10:34:48</messagedate><car></car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="1"><messagedate>20/06/2014 10:34:47</messagedate><car></car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="2"><messagedate>20/06/2014 10:34:35</messagedate><car>6</car><stat>Quit</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="3"><messagedate>20/06/2014 10:34:32</messagedate><car>8</car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="4"><messagedate>20/06/2014 10:34:31</messagedate><car>10</car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="5"><messagedate>20/06/2014 10:34:30</messagedate><car>3</car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="6"><messagedate>20/06/2014 10:34:28</messagedate><car>2</car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="7"><messagedate>20/06/2014 10:24:27</messagedate><car></car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="8"><messagedate>20/06/2014 10:15:16</messagedate><car>10</car><stat>Quit</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="9"><messagedate>20/06/2014 10:14:01</messagedate><car>4</car><stat>Quit</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="10"><messagedate>19/06/2014 14:07:35</messagedate><car></car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="11"><messagedate>19/06/2014 13:43:54</messagedate><car></car><stat>Ping</stat><ping></ping><job>Operator</job><operator></operator><jobref></jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="12"><messagedate>19/06/2014 13:42:58</messagedate><car>1</car><stat>Disp</stat><ping></ping><job>100 BLYTHE ROAD</job><operator></operator><jobref>220213</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="13"><messagedate>19/06/2014 13:42:57</messagedate><car>9</car><stat>Disp</stat><ping></ping><job>4 LILESTONE STREET; LONDON</job><operator></operator><jobref>220620</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="14"><messagedate>19/06/2014 13:42:56</messagedate><car>8</car><stat>Disp</stat><ping></ping><job>4 LILESTONE STREET; LONDON</job><operator></operator><jobref>220618</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="15"><messagedate>19/06/2014 13:42:56</messagedate><car>6</car><stat>Disp</stat><ping></ping><job>3P+3HL 22 MOUNT STREET</job><operator></operator><jobref>221165</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="16"><messagedate>19/06/2014 13:42:54</messagedate><car>5</car><stat>Disp</stat><ping></ping><job>GODOLPHIN LETYMER SCHOOL , IFFLEY ROAD</job><operator></operator><jobref>220211</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="17"><messagedate>19/06/2014 13:42:53</messagedate><car>4</car><stat>Disp</stat><ping></ping><job>32 ABERDEEN PLACE</job><operator></operator><jobref>220774</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="18"><messagedate>19/06/2014 13:42:52</messagedate><car>2</car><stat>Disp</stat><ping></ping><job>45 LOCKBRIDGE COURT WOODFIELD ROAD</job><operator></operator><jobref>221121</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="19"><messagedate>19/06/2014 13:42:50</messagedate><car>89</car><stat>Disp</stat><ping></ping><job>C------ GATWICK AIRPORT NORTH___EZY 8894__MARACUS</job><operator></operator><jobref>220793</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="20"><messagedate>19/06/2014 13:42:48</messagedate><car>43</car><stat>Disp</stat><ping></ping><job>183 EDGWARE ROAD; LONDON</job><operator></operator><jobref>221193</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="21"><messagedate>19/06/2014 13:42:45</messagedate><car>32</car><stat>Disp</stat><ping></ping><job>BEST WESTBURY SHAFTESBURY HOTEL, 27 DEVONSHIRE TERRACE</job><operator></operator><jobref>220989</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="22"><messagedate>19/06/2014 13:42:43</messagedate><car>111</car><stat>Disp</stat><ping></ping><job>22/17 ST. EDMUNDS TERRACE; LONDON</job><operator></operator><jobref>220973</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log><log id="23"><messagedate>19/06/2014 13:42:42</messagedate><car>10</car><stat>Disp</stat><ping></ping><job>WESTMINSTER ACADEMY;255 HARROW ROAD</job><operator></operator><jobref>220617</jobref><gpsx></gpsx><gpsy></gpsy><carspeed></carspeed></log></loglist>
But the page logs.php is not updating as it should be.
The logs.php file is populating a list as follows:-
<ul id="loglist" class="main-logs"></ul>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: 'scripts/xml/logs.xml',
datatype: "xml",
success: function(xml) {
var select = $('#loglist');
$(xml).find('log').each(function(){
var car = $(this).find('car').text();
var stat = $(this).find('stat').text();
var job = $(this).find('job').text();
select.append("<li>"+car+' - '+stat+' - '+job+"</li>");
});
}
});
});
</script>
Anyone have any idea why this is not updating?
I tried your example on fiddle and it seems to work perfectly if xml is a string. Try to change your dataType parameter to string instead of xml and it should work.
Here is my example: http://jsfiddle.net/8gEAN/
Try this:
<html>
<body>
<!--list to update-->
<ul id="loglist" class="main-logs"></ul>
<script type="text/javascript">
$(document).ready(function(){
$(function(){
//every 4 seconds send an ajax request to update the XML file
setInterval(function(){
$.ajax({
url:'scripts/php/log.controller.php',
success:function(data){
//when the XML is updated, send another ajax request to retrieve it and update the list
$.ajax({
type: "GET",
url: 'scripts/xml/logs.xml',
datatype: "string",
success: function(xml) {
var select = $('#loglist');
$(xml).find('log').each(function(){
var car = $(this).find('car').text();
var stat = $(this).find('stat').text();
var job = $(this).find('job').text();
select.append("<li>"+car+' - '+stat+' - '+job+"</li>");
});
}
});
}
});
},4000);
});
});
</script>
</body>
Put below function in your ajax call. This may be cache issue.
$.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});
For more details

getting variable from javascript to php

I have a simple combo box whose value I can get in JavaScript.
I am doing that so I don't need to refresh the page.
Also I want to use the selected value to do some conditional branching after combo box so how do I get the value of combo box from JavaScript to my variable $change.
echo '<select id="combo_1">';
echo '<option value="2">Submative</option>';
echo '<option value="1">formative</option>';
echo '</select>';
Below is my JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$('#combo_1').change(function(){
});
});
</script>
Here I want to do $change = $(this).val(), but obviously I cant do it like this.
Any suggestions?
i want to do it on the same page without refreshing or without submitting
my url kinda look like this
http://localhost/lms/grade/report/userdef/index.php
and i want it to be on click action
cuz depending on the choice of combobox 2 very different procedures will be called
You're gonna want to use AJAX and submit the form, then you can access the returned data without ever refreshing the page.
Basically:
HTML
<select name="combo" id="combo_1">
<option value="2">Submative</option>
<option value="1">formative</option>
</select>
JavaScript
$('#combo_1').change(function() {
$.post('calcScript.php', $(this).serialize(), function(data) {
alert(data);
});
});
in PHP, you can access your combo data via $_POST['combo'].
<script type="text/javascript">
$(document).ready(function() {
$('#combo_1').change(function(){
var combo_1 = $(this).val();
$.ajax({
type: 'GET',
url: 'ajax.php',
data: {'combo_1':combo_1},
success: function(data){
alert(data)
}
});
});
});
</script>
ajax.php
if( isset($_GET['combo_1]) ) {
echo $change = $_GET['combo_1'];
}
JS:
$.ajax({
type: "POST",
url: './filename.php',
beforeSend: function(){
//If you want to do something
},
data: 'data='$('#combo_1').val(), //Optional '&data2='+value+'&datan='+value,
success: function(msg){
alert(msg);
}
});
PHP:
$val = $_POST['data'];
return 'received successfully';
This will alert 'received successfully'

Get matched data by using jQuery from a json array on the same php

I am new to jquery. In my current cakephp page url is:
http://localhost/ultimate/admin/treatments/add
I have a json array: <?php $json = $this->Js->object($matchs);?>the content has: '[{"Type":{"name":"Items 1","price":"15","duration":"10"}},{"Type":{"name":"Items 2","price":"30","duration":"25"}}]'
Now I have a name: Items 1, how can I get the price from json array by using Jquery and update to one input area.
Something like this, but I have no idea:
$(document).ready(function(){
$('#treatment_foo').change(function(){
$.ajax({
url: 'add',
data:
dataType: 'json',
cache: false,
success: function(result) {
$('#fee_foo').val($('#treatment_foo').val());
},
});
});
});
success: function(result) {
$('#fee_foo').val( result[0].Type.price );
},
You can also use getJson jQuery function.
Like
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
(function($)
{
$.getJSON(URL,
{
params1: "value",
params2: "value"
},
function(result)
{
$.each(result.Type, function(i,item)
{
console.log(item.name);
console.log(item.price);
console.log(item.duration);
if ( item.name == 'Item 1' ) return;
});
});
});
</script>
As you can see you can loop through result json and display and compare it as you need,
Cheers...
Feel Free To Ask.

pass jquery value to php variable

Am a noob so hope you can help!
I've got the following jquery code:
$("#single-home-container").html(post_id);
It then displays the value I want within the HTML on the page:
<div id="single-home-container"></div>
What I would like is to pass the value into a PHP variable to I can use the info in a MySQL query.
How do I do so? It's in WordPress so no separate files
Thanks
You can use jQuery.post() or jQuery.ajax().
Here some examples:
<script>
$.post("test.php", { "func": "getNameAndTime" },
function(data){
console.log(data.name); // John
console.log(data.time); // 2pm
}, "json");
</script>
<script>
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
</script>
You'd need to use ajax. Something like this:
JQuery:
$('.locationID').click(function(){
var post_id = $(this).attr('rel'),
$container = $("#single-home-container");
old_html = $container.html();
$container.html('loading...');
$.ajax({
url : '/path/to/php/file.php',
data:{"post_id":post_id},
type: 'post',
dataType: 'json',
success: function(data){
$container.html(data.postBody);
},
error: function(data){
// possibly notify the user of the error
$container.html(old_html);
}
});
});
That assumes you have a field in your posts table called postBody
PHP
<?php
header('Content-type: application/json');
// $query_result = do mysql query with $_POST['post_id']
echo json_encode($query_result);
exit();
?>
That assumes that you want all the fields and you're returning all the fields, including postBody - and of course you have PHP 5.2.6+ or whatever version they added json_encode().

Categories