last day (14 days later) » 

12:54 AM
0
A: POST request to PHP from javascript

Mikhail FurlenderHere is a quick answer to your question using jQuery: $.post( "index.php", { myInput: "myValue", myOtherInput: "myOtherValue" } ); Since you are posting to the same page, you can omit the url value: $.post( "", { myInput: "myValue", myOtherInput: "myOtherValue" } ); However, this method use...

 
Thanks for the reply. I believe i need to use the a way that reloads the page like a form would as to be usable in the PHP. I dont really understand what you've done though. I want to eliminate using a html form so on page load, it essentially does what a physical form would do, and use it to send a variable value. name = 'john' for example. Thanks.
 
Well, first of all you don't need a full-page postback for the variable to be usable in PHP. But assuming that you do need a full-page postback for some other reason, you would need a <form> element. You're going to have a submit button right? Well, put a form element around it.
 
No, I was wanting it to fire on page load. Ive got it working now as a form and setting my value as a hidden input element and submitting it with javascript on page load, but because it essentially reloads and then fires the onsubmit again its just looping constantly. Any way around that? I did document.forms['myForm'].submit()
 
Why would you want it to fire on page load? That doesn't make much sense. The only way around that is to have your PHP script include the javascript into the page only when $_POST['myValue'] is not set.
 
my index is a php file that grabs some values from a database and i'm wanting to do it so the grab them according to this javascript variable. So if i post this first to the PHP it can then run the query using the variable. Probably completely backwards but i'm pretty new to PHP.
 
12:54 AM
You want to grab them according to the javascript variable, but you're setting the javascript variable on the page onload event? What information can this variable provide that you don't already know?
Hey - I opened a chat cause I think this would be more helpful for you
 
Hey, thanks for you time its much appreciated
 
No problem. Give me a brief overview of what this script is actually supposed to do.
Information that you send to the PHP end is typically user input.. if it is not user input then why can't you just get this information straight in PHP?
 
Basically, I have a login with facebook page, that returns the users email address. I then pass that over to a profile page that is profile.php. I want to be able to get this email variable that is set in javascript, and use it in the php on that page, to grab data from a database based on that persons email address.
Posting the form achieves everything i need, but i want all this to happen on page load.
 
Ok, so you already have the value you need in your login.php page?
(in the javascript end)
 
yeah and ive passed it over to my profile page using session storage
 
1:00 AM
I see. You're relying on the client's browser for shuttling this data between pages, when you should be relying on PHP itself
When a user logins in through the facebook page, and you've received the email address, how do you get the person over to profile.php?
 
ah right. i guess ideally i should send the email variable when they login
to the php
 
yup
How are they getting to profile.php now? are you using window.location.href in javascript or something?
 
ah i see. yeah i check their credientials, set the username into storage then redirect
 
redirect in javascript? Instead of redirecting in javascript, create a form right on login.php, add to it a hidden field with the value of the email address (and username and whatever), and submit a POST request to profile.php
 
my profile page also has some elements where i need the username as a javascript variable along with some others, profile picture url variable etc
whats the best way to pass these for use in the profile page
ah right okay thanks
 
1:06 AM
so right after you have the variables you need, you can do this:
`var $form = $('<form method="POST"></form>');
$form.submit(function () {
var input = $("<input>")
.attr("type", "hidden")
.attr("name", "username").val(username);
$(this).append(input);
$input = $("<input>")
.attr("type", "hidden")
.attr("name", "email").val(email);
$(this).append(input);
return true;
});
$form.submit();
});`
 
ah that makes sense
thanks
would these make them available in javascript on the following page
on the profile page
 
no, so if you want to do that you can either
1) use session storage as well (before you submit the form)
or
2) export them into javascript variables in the PHP page
 
got it, i think ill export them from the php
thats great, thanks very much for your help
 
no problem. if i helped you out id appreciate an upvote to my answer in your post ;-)
 
sure will do :)
 
1:14 AM
thanks :) Have a good one
 

last day (14 days later) »