jquery
$("#go").click(function() {
$.post("i/go.php", $("#form").serialize());
var code= '';
$("section").empty().html(code);
});
go.php accepts POST data, then displays some text in <section>.
How to get <section> after sending POST data and display it on this page?
You are not handling the response at all, just sending the request:
$.post("i/go.php", $("#form").serialize(),
function(data) {
// do something with data
$("#some_div").html(data);
}
);
Related
I want to send a Hindi message from my custom php page. For this I have used a ajax request.
How to encode it in JavaScript before sending in ajax and decode it in php function?
EDIT
Here is my code :
...
var message = ''; // here I am getting content from form field dynamically
var ajaxBlockUrl = '/my.php?action=custom&mobile=999999999&message='+message;
Ajax.Request(ajaxBlockUrl,
{
parameters: {isAjax: 'true', form_key: FORM_KEY},
onSuccess: function(response)
{
//
}
});
...
I am using the below code to load a div content from Rezults2.php page into my page.
$("#hotel-list").load("Rezults2.php #hotel-list> *");
How can i send the same $_GET cookies received in my page to Rezults2.php also ?
Or how can i insert the link attributes into my code
$("#hotel-list").load("Rezults2.php?var=variable&etc=etc #hotel-list> *");
You have to do Ajax Requests to achieve the required purpose for sending and getting information.
$.ajax({
type : 'GET',
url : 'page.php',
dataType : 'json',
data: {
variable : $('#id').val()
},
success: function(data){
$.each(data.variable, function() {
//do things here.... with return data
})
}
})
page.php
<?
//Code to do php things
?>
$("#hotel-list").load("Rezults2.php #hotel-list>",{'path': 'scotland'});
Rezults2.php
<div id="hotel-list">
<?php
if(!empty($_REQUEST['path'])) {
// i used $_REQUEST because it receives the data from POST or GET.
$path = $_REQUEST['path'];
echo $path;
}
?>
</div>
Use Jquery load
http://serverA.com/list.php:
html:
<form id="myform">
<input id="inputfield" name="view">
</form>
js:
var inputdata = $('#inputfield').val('ocean-view');
$('#myform').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: 'http://serverB.com/detail.php',
data: inputdata,
dataType: 'jsonp'
});
});
http://serverB.com/detail.php
php:
<?php
$view = $_GET['callback'].'('.json_encode(name) .')';
?>
html:
<h4><?php echo $view; ?></h4>
what the code does is:
from serverA, assign a value "ocean-view" to an input field, submit this form to serverB, and display this value in a h4 tag.
I couldn't quite figure out how to write the server-side code to output the value, even though I have found the following posts.
this and this.
Any kind of help is appreciated.
UPDATE:
I used YQL to help to see the jsonp callback response, here is the json structure:
callback({
"query": {
"count": 1,
"created": "2013-07-29T13:01:12Z",
"lang": "en-US",
"results": {
"h3": {
"class": "mytitle",
"content": "Example"
}
}
}
});
Actually You are very close to success... just read these points.
Whenever you are making an ajax request the browser sends a hit on ajax URL with respected parameters and details. On respected URL PHP code executes. It can return data in some format. It can not return data in directly PHP variable.
Formats are:
text/html
json
xml
......
Mainly for cross domain requests we use JSONP.
And it means PHP script will return data in JSON.
So you will just echo your json_encode in directly script. That will be the response of your ajax request.
Now when you have got the data in ajax function, then jQuery uses success: function(response){ } where you response will come.
So variable response will contain JSON.
You can access JSON and put data in any tag by using jQuery selector.
$().html(response.);
These thing can be analyzed in any browser in debug console. That what is requested and what is returned.
even you can use Firebug in Mozilla to inspect ajax request.
So you will do three changes.
In Ajax function write a success function:
var inputdata = $('#inputfield').val('ocean-view');
$('#myform').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: 'http://serverB.com/detail.php',
data: "inputdata="+inputdata,
dataType: 'jsonp',
success:function(response){
// response will be json type so access it
// print ur json in html tag like resposne is {"data":23}
$(<jquery selector>).html(reponse.data);
}
});
});
<?php
// echo this
$inputdata = $_GET['inputdata'];
// perform you logic ,
// make an array to encode it in json
echo $_GET['callback'].'('.json_encode($responseArr) .')';
?>
and remove PHP code from h4 tag.
I have a page (page1)that calls another page (page2) via AJAX.
How do I include parameters to page2 response and parse them in success : function(data)?
is there any way to add parameters to http response object and then access those parameters?
Reason:
page2 generates some HTML. It could be good or bad html message. If it is a good message, I need to add parameters (hidden) and then add that good message to Success div. if message is an error, I need to add this message to 'Error div'.
I can do it bad way: In Page2 I can add hidden element; then in page1 I can create some temp hidden div and add response to that div. Then access hidden element in hidden div and get it's value. Then get message from that div and paste it to designated div. But this seems to be too unprofessional.
PAGE1:
function registeruser(){
jQuery(function($) {
$.ajax( {
url : "page2.php",
type : "POST",
data: {
registerFname : document.getElementById('registerFname').value,
registerLname : document.getElementById('registerLname').value,
registerLEmail : document.getElementById('registerEmail').value,
registerPassword : document.getElementById('registerPassword').value,
ts : (new Date().getTime())
},
success : function(data) {
//need to apply logic here based on the return parameters
//if (SOME_PARAMETER === 'success'){
// document.getElementById('registerPresentation').innerHTML = data;
//}
//else {
// document.getElementById('registerPresentationErrorDIV').innerHTML = data;
//}
//need to get rid of item below
document.getElementById('registerPresentation').innerHTML = data;
}
});
});
}
page2.php
$a=array('err'=>0,html=>'dfhsxdfhbcfvyhdgfr');
json_encode($a);
page1.php
success : function(data) {
if (data.err=='0'){...}else{...}
}
I need to send some data to an external php page and that page has to send the required data back to jQuery. My question is how can I send the data from the external page back to jQuery on the page that send it.
This is the jQuery code that sends the data to the external page:
function LoadImageData(url)
{
$.ajax({
url: 'get_image_data.php',
dataType: 'json',
data: {'url': url },
success: SetTag()
});
}
This is the PHP code htat receives the data and is required to send some data back:
<?php
require_once('FaceRestClient.php');
$apiKey = '**********************';
$apiSecret = '**********************';
$api = new FaceRestClient($apiKey, $apiSecret);
$active_url = $_POST['url'];
$photos = $api->faces_detect($active_url);
return $photos;
?>
So my problem is, how can I send the data backto jQuery. Just a simple return does not seem to work.
Thanks in Advance,
Mark
You need to echo the resulting JSON:
echo $photos;
If $photos is not already JSON, use json_encode:
echo json_encode( $photos);
One would think the REST API would give you JSON, but you need to check if it's valid JSON (JSONP is not valid here) ?
You could just drop the dataType in your Ajax function and let jQuery figure it out, that way atleast you'll get something back if it's not valid JSON.
Try this:
$.ajax({
url: 'get_image_data.php',
type: 'POST',
data: {'url': url }
}).done(function(data) {
console.log(data);
}).fail(function() {
console.log('Your ajax just failed');
});
Open the console, and see what is printed
At the end of a PHP function I tend to do :
exit(json_encode($someData));
To return the data as JSON, but anything that prints the data is ok.
try this
echo json_encode( $photos);
you need to echo
echo $photos;
and as metntoned by #nickb if $photo is not already a json then convert it into json first and then echo.
echo json_encode($photos)
in jQuery if you want to fetch the data
onSuccess: function(data, status) {
//var data contains the returned json.
}