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
Related
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 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
}
}
I have some Ajax code on one of my webpages:
<script type="text/javascript">
function update(obj) {
$.ajax({
url: 'php/process.php',
method: 'POST',
data: {
orderID: obj.id
},
success: function (data) {
console.log(data);
}
});
}
</script>
I then have a page called "process.php" where I have this simple code:
<?php
$orderID = $_POST['orderID'];
echo $orderID;
?>
I have it so that when a button is pressed, the update() function is run using "onclick()". When I do click the button, i get the correct output in the browser console, but the page does not redirect to the process.php page. Any help would be appreciated :)
You need to use type, not method, to set a POST request. The default is GET, hence why your PHP receives no data. Try this:
$.ajax({
url: 'php/process.php',
type: 'POST',
data: {
orderID: obj.id
},
success: function (data) {
console.log(data);
}
});
When I do click the button, i get the correct output in the browser console, but the page does not redirect to the process.php page
That's the whole point of AJAX...
I want to send an array of strings on my JavaScript to a MySQL table.
What is the best way and how can I do it?
You can use AJAX POST like so:
function sendArray()
{
var myArray = ["one","two","three"];
$.ajax({
url : 'YOUR_URL',
method : 'POST',
data :{
arrayData:myArray
},
success : function(response)
{
alert("data sent response is "+response);
},
error : function(e)
{
alert("data not sent")
}
});
}
</script>
<button onclick="sendArray();">Send Array</button>
here myArray is the string array you want.
on the PHP backend you can get this data as:
<?php
$arrayData = $_REQUEST['arrayData'];
foreach($arrayData as $data)
{
echo $data;
}
First of all understand the difference. Javacript is client side scripting language. It will not interact on my MySql on its own you will need to makean ajax call and then use server side scripting language like PHP to fulfil your purpose
you can use jquery to create an ajax call like
$.ajax({
type: "POST",
url: "somefile.php",
data: dataUwanttoinsert,
success: success,
dataType: dataType
});
then add your SQL query to the php file and when you make this ajax call it will do the query you have written
How I'd do it is by sending the string as a HTTP request payload using AJAX to a server that is connected to your MySQL database. Once the data is received by the server, it can process it and insert it to the database.
Here are some docs to help you get started:
Intro to ajax
Inserting data to MySQL using PHP
$.ajax({
url: 'url',
data: {'a': a, 'm': m},
type: 'POST',
cache: false ,
success: function (data, textStatus, jqXHR) {
alert(data);
}
});
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']