How to send current URL in JavaScript to a second PHP page? - javascript

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'];

Related

How to intercept POST data with Javascript

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);
?>

javascript variable to php NULL

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
}
}

Display page only if ajax call succeeds?

I have a subdomain protected by a service similar to cloudflare. It checks if a visitor is human or not. To save bandwidth usage I want only one file to be loaded from this subdomain as indicator if the visitor is a bot or not. If it is not loaded, the service has blocked it as bot, and nothing should be shown.
I already thought of an ajax request before the page loads to check this.
var success = 0;
function myCall() {
var request = $.ajax({
url: "sub.example.com/hello.php",
type: "GET",
dataType: "html"
});
request.done(function() {
// Load page, because the request was successfull
$.ajax({
url: 'content.php', //This is the current file
type: "POST",
dataType:'json', // add json datatype to get json
data: ({load: true}),
success: function(data){
console.log(data);
}
})
});
Is there a better way to do this?
You may try this:
var success = 0;
function myCall() {
var request = $.ajax({
url: "sub.example.com/hello.php",
type: "GET",
dataType: "html",
success: function(){
$.ajax({
url: 'content.php', //This is the current file
type: "POST",
data: dat,
success: function(data){
console.log(data);
},
error: function(data){
console.log('Error!');
}
});
},
error: function(data){
console.log('Error!');
}
});
}

Jquery AJAX posting variable to server file

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)

jQuery on success, reload the page and then append?

I have the following code:
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
location.reload();
$('#messageContent').append('<p>success</p>');
}
});
What happens right now is that it appends and then reloads real quick so it loses the message.
Now, I could take out the reload and it will append but in order for my changes to take affect, the page needs to be reloaded.
Is there anything else I can do? I want to show a success message after reloading the page.
Thanks!
location.reload() will reload your page, which creates a new request to your server and the browser then loads the response. You could add a parameter to the query string for the message, e.g.
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
window.location = "?message=success";
}
});
Then in server side code, you could display the message if the parameter exists. In ASP.NET MVC, it would look like this:
#if(Request["message"] == "success")
{
<p>success</p>
}
If you don't have server side scripting available, you can also parse the querystring with javascript.
You can have some delay before you reload the page. Try this
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
$('#messageContent').append('<p>success</p>');
setTimeout(function(){
location.reload();
}, 500);
}
});
Send it to a location but with a hash in the url to indicate a success:
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
window.location = window.location + "#success";
}
});
Now when the page loads, check for the hash:
$(document).ready(function(){
if(window.location.hash) {
$("#messageContent").append("<p>success</p>");
}

Categories

Resources