jQuery - How to send ajax post on ajax post page - javascript

I need to send ajax post on ajax posted page.
index.php has
$.ajax({ type: "POST",datatype:"json", url: "/notification.php",
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});
notification.php has same function but posts to track.php
$.ajax({ type: "POST",datatype:"json", url: "/track.php",
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});
However, notification.php doesnt send the ajax post. How can I make it run ?

First thing you cannot run the jquery code even if you include it in your notification.php file. That is because jquery runs only in browser, not at backend. So unless you "physically" open the notification.php page in browser the jquery won't run.
So to address your issue you'll have to chain the success response from one php file to next.
eg:
Data from index.php ---> notification.php ---> index.php ---> track.php
(Although a very crude approach)
Here is the code that can achieve this.
index.php file
$.ajax({
type: "POST",
datatype:"json",
url: "/notification.php",
data: {
kime=karaakce,
tur=konuyayorum,
postid=2020
}
success: function(responseData){
$.ajax({
type: "POST",
datatype:"json",
url: "/track.php",
data: {
kime=karaakce,
tur=konuyayorum,
postid=2020
}
success: function(html){
// This is your final success
}
});
}
});
Your notification.php file should return a JSON data which you can use to send it to the next request. It will come in 'responseData' object.

Related

passing the ajax response data to another page after ajax call on success

I have ajax call and pull the data and I want to send the response to next page in ajax success and get the data in drilldown.html
$.ajax({
type: "POST",
url: "drilldown.php",
data:data,
success: function(response){
window.location.href = 'drilldown.php'; // want to post the data this in redirection
}
});
If there is no server-side involved, you can save the data using
Window.localStorage (check if browser compatibility is OK for you)
$.ajax({
type: "POST",
url: "drilldown.html",
data:data,
success: function(response){
localStorage.setItem('myData', data);
window.location.href = 'drilldown.html'; // want to post the data this in redirection
}
});
On the drilldown.html page, you can read the data with JavaScript by
var data = localStorage.getItem('myData');
There's also sessionStorage which will be cleaned up after browser restart.

Pass javascript variable to php in same file

Is it possible to pass a javascript variable to php code in the same file?
For example I did this, in my example.php:
JS
$.ajax({
url: 'example.php',
type: 'POST',
data: {var: 12345},
success: function(data) {
console.log("success");
}
});
PHP
$var = $_POST['var'];
echo $var;
But it doesn't work. Any solutions?
$.ajax({
url: 'example.php',
type: 'POST',
data: {var: 12345},
success: function(data) {
console.log("success");
}
});
Your AJAX request expect a response (you assigned it to data variable).
You should elaborate more the value returned by your PHP script, like console.log(data) to see what it is returning.
Your PHP file can't write it directly to your html file, because it is a server-side operation and your page is already loaded. I think you should add some request-type logic in you PHP too if you want to have HTML file rendering and Javascript AJAX requests both in the same file.
A solution could be to catch the AJAX[POST] request in PHP code and return some HTML or String values to attach in HTML like $(body).append(data);
Here you can learn more: https://www.w3schools.com/php/php_ajax_php.asp

jQuery post not working whereas get works with same structure

I've got an ajax call to the following php method:
public function test(){
die(json_encode(['test' => 'test1']));
}
My ajax call works when set as GET but not as POST. The GET call is:
$.ajax({
type: 'get',
url: url,
success: function(msg) {
log(msg);
},
dataType: 'json'
});
Which successfully returns the JSON element. But when I set it as POST:
$.ajax({
type: 'post',
url: url,
success: function(msg) {
log(msg);
},
dataType: 'json'
});
Returns nothing. If I removed the dataType it will return the whole webpage where it's being triggered from.
I do need to make the request as POST since I'll be sending a large amount of data.
Thanks.
Do you have CSRF protection enabled?

Ajax post issue with smarty

I have script using smarty engine.
I just tried to use ajax post to get some data from page.
The javascript function as following:
function getTime(val,id) {
$.ajax({
type: "POST",
url: '{$GLOBALS.site_url}/check_time/',
data:'GetDate='+val + '&Jobid='+id,
success: function(data){
alert(data);
}
});
}
the alert show me (3 small squares) I don't know what dose mean !

Best practice for using php file with AJAX

I'm beginning web development with php and am testing jQuery ajax calls to a php file to run functions. But I've noticed that the php file loads into the resources each time I call it with an AJAX POST method. What is the best solution to prevent this occurrence? Also, are there better coding practices to use when performing multiple function calls (what I'm used to calling web services or web methods in the c# world) from a single file in php?
test.php
<?php
if($_POST['action']=='test'){
$arr = array(
'stack'=>'overflow',
'key'=>'value'
);
echo json_encode($arr);
}
?>
scripts.js
function postTest(){
var data = {
action: 'test'
};
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: data,
success: function(result){
console.log(result)
}
});
}
Update:
I changed my code to use the data variable as an object in the ajax call.
The original question still stands however. How do I use a function inside a php file without it being loaded into the site resources in the browser for each ajax call?
Thank you.
If you're noticing that the php block is never getting executed, then you're passing in the data wrong. Try this:
function postTest(){
var data = {
'action': 'test'
};
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: data,
success: function(result){
console.log(result)
}
});
}
JSON.stringify is not working in your favor, here.
To demonstrate this, you could put print_r($_POST) in your php script and see what console.log(result) gives you.
To remedy this, you can simply put your data in the $.ajax() block:
function postTest(){
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: {
action: 'test'
},
success: function(result){
console.log(result)
}
});
}

Categories