I am trying to make an HTML5 and PHP based web application.The objective of the application would be to fetch the data from another PHP based website and use that data within my application.How do I do it?
-
3This question is very broad. Please go do some more research and familiarize yourself with the basics of the techniques used. One of your keywords is `JSONP`. – CBroe Feb 24 '14 at 20:37
2 Answers
The above answer is partially incorrect; PHP doesn't have access to localStorage. However, you can use cookies to achieve what you want - although you CAN use JSON with AJAX, but I think cookies are easier. Since you're very new to JavaScript, I'll show you an example.
Let's assume I'm making a very insecure login system. Let's say:
$name = "John"; //set the name to John.
Now, to let JavaScript access this name variable, we'd have to use cookies. You can use them like so:
$username = "John"; //set the name to John.
setcookie("username", $username, time()+86400);
The setcookie method take the paramaters:
setcookie(name, value, expire, path, domain);
So in our case, it sets the name to username, the value to the user's name, the expiry date from 1 hour from now (60*60*24). This cookie will expire in one day.
Now, once this is done, make sure this function is added in your scripts.
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
Please credit the original author at this topic for the code above.
Then, to get the cookie, simply do:
name = getCookie(username);
I hope I helped!
-
Ok, I understand all of the php. However I do not understand where I am supposed to put the url location of the php file. Sorry I must seem rather stupid asking this question. – checked01 Feb 24 '14 at 21:12
-
Just a wording issue: note that the other answer may appear above or below your answer, depending on their relative score. – halfer Feb 24 '14 at 21:32
-
@checked01, if you want it to be asynchronous, you'll have to use AJAX. I'm not that familiar with it, so you'll have to Google it :) – Riftes Feb 25 '14 at 16:56
I think you can use "cookies" or "localStorage"

- 1,238
- 8
- 8
-
1There are other sources better than W3Schools. Why? [w3fools](http://w3fools.com) – Tomas Ramirez Sarduy Feb 24 '14 at 20:40
-
have these too and anything you put on google and search. http://www.quirksmode.org/js/cookies.html and http://www.quirksmode.org/blog/archives/2009/06/html5_storage_t.html – Paulo Lima Feb 24 '14 at 20:43