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
}
}
Related
I want to sent current URL from Page1 to a php page called upload_picture.php.
I look at another question and this is an option:
On Page1
$.ajax({
type: 'POST',
url: '/uas_tools/visualization_generator/V2/Resources/PHP/upload_picture.php',
data: 'url=' + window.location.toString()
});
If using this option, on upload_picture.php, how to get the URL sent from Page1?
Your code to call the ajax will be as this.
$.ajax({
type: 'POST',
url: '/uas_tools/visualization_generator/V2/Resources/PHP/upload_picture.php',
data: { url: location.href },
success: function(response) {
// your success callback
},
error: function () {
// your error callback
}
});
And on the php page you can retrieve the data as
<?php
$page_url = $_POST['url'];
I want to know how I can pass data from my script javascript to my php code to use the data into a query
I tried many things but it didn't work for me
So this is my script to upload files from input type: file then i get the url in downloadURL variable
var downloadURL;
...
uploadTask.on('state_changed',function(snapshot){
},function(error){
},function(){
downloadURL=uploadTask.snapshot.downloadURL;
alert(downloadURL);
});
Now I want to pass downloadURL to my php so I can use it .
I also tried Ajax to do this task but it didn't work or the code that I used is false
Ajax code :
$.ajax({
type: "POST",
url: '', //same page
data: downloadURL ,
success: function(data)
{
//alert(data);
}
});
EDIT
Php code :
<?php
$user=$_POST['downloadURL'];
echo $user;
?>
Just a normal echo to test if data is Posted or not
Structure the data of your $.ajax request in a name-value pair manner.
Change this:
$.ajax({
type: "POST",
url: '', //same page
data: downloadURL ,
success: function(data)
{
//alert(data);
}
});
To this:
$.ajax({
type: "POST",
data: {"downloadURL":downloadURL} ,
success: function(data)
{
//alert(data);
}
});
I also removed url from your $.ajax request because by default url is set to the current page.
With the above modifications, your PHP code will remain unchanged (e.g., $user=$_POST['downloadURL'];).
Okay change your php with that code:
if(isset($_POST['downloadURL']) {
$response = array(
'user' => $_POST['downloadURL']
);
echo json_decode($response);
exit;
}
Because you are making ajax request you must return json that why we parse our Array to json(object) and then in your javascript ajax request inside success function write
console.log(data);
And after data
...
data: downloadUrl,
Add this
dataType: 'json'
This mean we are telling on our ajax request that we are expecting json response
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);
?>
I have a javascript file that I want to send data from. I tried using $.ajax and $.post but neither seem to be sending the data to my index.php file.
script.js
$.post( "index.php", { over: 'yes' } ).done(function( data ) { alert("success"); });
//AND
$.ajax({
url: "index.php",
type: "POST",
data: {over: "yes"},
success: function(data){ alert("success"); },
});
Both methods receive the success message but neither are echo'ed in my index.php file
echo $_POST['over'];
What am I doing wrong? Thank you
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']