I am using this javascript function to send data to my php script (via POST) I am then using this information to retrieve data from my database and I would like to reuse the information retrieved in my JavaScript code.
Here is my code :
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function() {
// HERE I would like to inctercept the data that my php script put in myFunctions.php produces and use it to generate content;
}
});
});
So basically, when clicking on the button #validerChoixDeCours, my code sends some data to myFunctions.php which generates a response stored in a php variable and I want to use that response in my JS code.
Thank you in advance for your help :)
Its actually quite easy!
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(e) {
e contains the response from the php script
}
});
});
The first parameter of success contains the response from the request. so, in this case, the 'e' variable contains the output which you can use.
Add data variable
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(data) {
alert(data);
}
});
});
make sure your server side code look like this
<?php
$output = "Any String or Array";
echo json_encode($output);
?>
Related
I use simple AJAX request to pass JavaScript variable to PHP via AJAX.
This my AJAX request:
var image_url = "example";
$.ajax({
type: "post",
//url: 'index.php', // the same file
data: { image_url : image_url },
success: function(data) {
alert here works!
}
In the same file, on the first line i have
<?php var_dump($_POST['image_url']); ?>
On my page I see always NULL
Can someone help me?
Note that all the server-side PHP code on has already run once you're interacting with the page client-side, so you won't be able to access the $_POST['image_url'] using PHP as you appear to be attempting.
That being said, any resultant data from your AJAX request will be in the data variable of your success function.
$.ajax({
type: "post",
//url: 'index.php', // the same file
data: { image_url : image_url },
success: function(data)
{
console.log(data); // this is where your data is
}
}
Maybe this is a duplicate but I can not understand why mo code does not work. I am trying to get multiple results via Ajax/php.
This is from my php file:
$result11 = 'test1'
$result22 = 'test2';
echo json_encode(array("data1" => $result11, "data2" => $result22));
Ajax call:
$(document.body).on('submit','#sendmessage',function() {
$.ajax({
type: "POST",
url: "/send.php",
data: {par:par,kid:kid,ha:ha,sform:sform,editors:editors},
cache: false,
dataType:'json',
success: function(datax) {
alert(datax.data1);
}
});
return false;
});
Problem:
When I submit a form, the page refreshes instead of sending ajax request.
At the same time this works but I can't get multiple results from Php file:
$(document.body).on('submit','#sendmessagex',function() {
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "/send.php",
data:str,
success: function(data) {
alert(data);
}
});
return false;
});
Add a preventDefault() call to your script
$(document.body).on('submit','#sendmessagex',function(event) {
//----------------------------------------------------^^^^^
event.preventDefault();
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "/send.php",
data:str,
success: function(data) {
alert(data);
}
});
return false;
});
You have to use preventDefault() ofcourse to prevent page refresh. Then you can use the second code without datatype json. But in that case at first you have to parse the json like this way:
success: function(data){
var datax = JSON.parse(data);
//now you have object and can access like this: datax.data1, datax.data2
}
Incase you want to use first code, in php you have to set php header to define it as proper json output.
header('Content-Type: application/json');
I have a variable which contains data, i'm then using a ajax function to send this variable data to this php file. I'm slightly unsure I can store this variable into php and echo it out. This is the code that I currently have...
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: data,
success: function(data) {
alert ( data );
}
});
This is my php code
$noteone = $_POST['data'];
echo $noteone;
Any help would greatly be appreciated
data of your ajax call should be like below. Hope it will solve your problem.
data: { "data": data }
You also need to set the data type:
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: { "data": data },
dataType: "json", // <---- THIS ONE
success: function(data) {
alert ( data );
}
});
Try this :
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: JSON.stringify(data),
contentType: "application/json",
success: function(data) {
alert ( data );
}
});
PHP requires data to be submitted in key=value format when building $_POST/$_GET. You didn't do that. You only submitted value, so PHP has no key to populate $_POST with. You need to have:
data: { "whatever_you_want": data }
which becomes
$_POST['whatever_you_want']
Using Jquery AJAX to read a number from a server file, increment the number, and write the number back to the server file.
I can read the file fine, I just can't post to the file.
<script>
var counter = -1;
$.ajax({
type: "GET",
url: "counter.txt",
success: function(text) {
counter = text;
counter++;
$("#count").html(counter);
},
error: function() {
$("#count").html("Error!");
}
});
$.ajax({
type: "POST",
url: "counter.txt",
data: counter,
success: function() {
},
});
</script>
Ajax being a client-side method can't write to a file on the server.
You would need some middleware in PHP, ASP, Python etc to take the amends as posted data and write to the file.
Since Ajax is asynchronous, this piece of code
$.ajax({
type: "POST",
url: "counter.txt",
data: counter,
success: function() {
},
});
will be executed before you will get first number from server. You need to put this code inside "success" function
$.ajax(
type: "GET",
url: "counter.txt",
success: function(text) {
counter = text;
counter++;
$("#count").html(counter);
$.ajax({
type: "POST",
url: "counter.txt",
data: counter,
success: function() {
},
});
},
error: function() {
$("#count").html("Error!");
}
});
Also, you should just call some server side php method through Ajax, since you can't set anything through JavaScript on server.
So "post" Ajax URL should looks like
url: 'http://www.server.ca/set_vars?var=var_title&value=' + encodeURIcomponent(counter)
HIi Folks i am having one issue please help me i want to send some data to my controller using ajax call i have written all code but for data field
<?php echo $this->Js->get('.menu')->event('click',$this->Js->request(array('controller' => 'restaurants', 'action' => 'getItem'),array('async' => true,'method'=>'POST','update' => '#HFNames','data'=>'$(this).attr(id)')),false); ?>
when ajax call hits it take "$(this).attr(id)" as a params but i need the value of cureent click
js helper genrate this
if we remove that double quote from this genrated script then its working
data: "$(this).attr(id)",
why this get quoted
<script type="text/javascript">
$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: "$(this).attr(id)",
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "\/foodkingkong\/restaurants\/getItem"
});
return false;
});
try this instead.
echo $this->Js->buffer('$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: $(this).attr(id),
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "/foodkingkong/restaurants/getItem"
});
return false;
});'
);
Hi Himanshu we had planned to do the same directly using jquery but wanted to do the same via cakephp bind function only.
What will be the difference if we pass the js in buffer.