I have a difficult problem, we are currently using PHP on our server and I am a PHP programmer. I have built a site connected to our PHP site that is completely client side HTML 5 and Javascript. I can pass variables from HTML 5 to PHP but I want to import some of the data from the PHP to the HTML pages such as session ...etc. how can I pass PHP sessions to Javascript on another page or access the session data to be used on HTML 5 and Javascript?
The main reason for this is for users with low bandwidth and for the ability of HTML 5 to use local storage and offline capabilities if their line is disconnected so they can post the data at a later time to the PHP pages.
Help would be great?
I hope I am not comepletely wrong here, but what you are essentially trying to do is writing data from php to javascript. You could use AJAX as akluth suggested, but I think it is easier to just do the following in the javascript;
data = <?php echo $foo; ?>;
You should however be careful when saving the session in localstorage since it can be modified by the client. Probably double check once the user attempts to post the data later. See this SO answer for more details on that.
If you want to get session data in PHP you can use following segment
<script>var $_SESSION = <?= json_encode($_SERVER) ?></script>
If some data to be maintained on different pages and not require sever data, better you use Session Storage of HTML5 [http://www.xul.fr/en/html5/sessionstorage.php]
This will reduce server load + data transfer and increase speed of your application
HTML5 local storage is not fully supported so some thing that can work anywhere can be do in following way:
First have your PHP Session into JS var:
<script type="text/javascript">
var sessionData= <?php echo json_encode($_SESSION); ?>
</script>
Then you will have session data in sessionData variable in JSON format. then if you want to use it on different places then simply set and fetch it into cookies via Javascript.
Please tell if some thing is still unclear.
Related
I am new to jQuery,Ajax and all, I don't know to pass a value of html file to php file.
For Eg:
my html file as
<html>
<script>
$(document).ready(function(){
$(".teachers_but").click(function(){
//alert('teacher');
var id=0;
alert(id);
});
$(".students_but").click(function(){
//alert('student');
var id=1;
});
});
</script>
<div class="teachers_but">TEACHER SIGNUP</div>
<div class="students_but" style="text-align: center; margin-top: 8px;">STUDENT SIGNUP</div>
</html>
How to pass id value in html file to php file and set cookie for that value.
I think this question comes up somewhat often, and the comment answers are correct; but you also have to understand what's happening during the request. I'll try to keep it kind of simple.
On the SERVER, PHP processes all the PHP tags of your file, and turns it into a file consisting of pure HTML, Javascript, and CSS. The server sends this to the CLIENT (the person with the web browser hundreds of miles away). That client doesn't even know what PHP is, and the server will actually then shut down that little PHP environment it had created; all variables are destroyed, and it waits for the next client request to process a page again.
Using a specially-coded in-page request, often referred to as AJAX (Asynchronous Javascript and XML, the XML part being a misnomer) you can start new requests against the PHP server to save information, request new information, etc, without loading a new webpage.
HOWEVER, your specific question has a unique aspect to it; you want to set a cookie. Although you normally only see cookies when working in PHP, the cookies are actually stored on the client's computer, and are sent to the server on each request. Javascript is able to access them on its own, and some libraries can help with that.
I haven't followed this tutorial myself, but a quick Bing search found this, which might help you: http://www.electrictoolbox.com/jquery-cookies/
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I want to pass a variable javascript into variable php. I can't use $_GET or $_POST because is all loaded by the method load in jquery and I don't have any form or possbility to implement it.
This is my code:
<script type="text/javascript">
var id= "test";
</script>
<?php $_SESSION['my_page'] =?> + id;
How can I resolve It?
2 solutions
you use AJAX to send your value, see jQuery.
you set that session variable from a submitted form as a value and process it on your script.
Eighter way you CANNOT mix javascript with PHP like that. Javascript is client side, PHP is server side.
for jQuery:
<script type="text/javascript">
var id = "test";
$.get('yourScript.php?id='+id);
</script>
and in yourScript.php
<?php
session_start();
$_SESSION['my_page'] = (int)$_GET['id'];
?>
The truth is you can't really pass them. Javascript is client-side meaning it happens on each individual computer. PHP is server-side so it happens on the server. You would have to somehow pass that variable from Javascript into the server and you could do that AJAX.
Another idea to consider is using the standard way of passing variables into different PHP documents: forms. You can make a form and use javascript to insert what you want to be submitted into the PHP server and from there you can use the $_GET or $_POST. Hope this helps.
Consider using AJAX. Here is a link to some tutorials although if you are using JQuery, it can handle most of the coding for you.
http://www.w3schools.com/ajax/default.asp
From JavaScript, you can call the PHP script after the page has loaded asynchronously (meaning that it will open a connection in the background rather than changing the page). Once the PHP script has finished executing, it will return a status code that you can check for through the JavaScript. You can access the data resulting from the PHP scripts execution in the JavaScript.
If you are coding a mobile website, be aware that not all mobile phones support AJAX.
EDIT: If you would rather use JQuery, then here is a link to a good tutorial http://www.sitepoint.com/ajax-jquery/
I need to display information from an SQL database which resides on a server, to a remote webworks mobile device. I am extremely new to passing information from a server so bear with me. My normal understanding is that I would have an HTML file that accesses a php script which then itself connects to the database and displays the information.
However, in webworks the HTML/Javascript files reside on the device and are separated from any php file so I need a method to communicate to get the data from the database. I have looked through JSON and read all the tutorials on w3schools and I understand the syntax but I don't understand how to use it. How could it connect to a database? My aim is to simply display the table entries on a mobile device app running HTML5 webworks. Again I am very new to this so any explanation would be very helpful.
Chances are, you should get a book. This is not something that can be explained in detail in a short answer on this site.
in summary however, you can either
1) send requests to your php script by submitting a form on an html page, which will load a new page filled with whatever PHP sends back. in this case you do not need to use JSON at all as PHP would be returning a full html page.
2) you can use AJAX. AJAX is a javascript method of sending requests to the server (PHP), and getting a response without ever loading a new page. you would use AJAX to send a request to the php page, the php page would access the database and send back a response, the javascript would then take the response and do whatever it needs with it. the response data is usually formatted in a JSON format, because PHP can easily create JSON, and javascript can easily decode JSON once it receives it as a response. to make using AJAX simpler, you may want to look in to using jQuery, a javascript library which can simplify the process.
I have a php code page that pull data from a database. The data then should go into javascript, which is a separate .js file (and i need to use Jquery too). I want to separate my .js and my main html page too. How would you do it, which one should be included in which?
And if you also use Smarty, how will that change the structure?
Thanks!
Whenever I'm generating Javascript data on the server side, I try to keep it entirely separate from the rest of the Javascript and HTML code. The cleanest way to do this is often to implement a basic API for your data: Create a PHP page that serves up pure JSON data from the database based on the URL and/or querystring, then use $.getJSON() to load it from the server. This approach avoids the cruft of generating Javascript with PHP, and allows for asynchronous loading of data, which may improve your UI.
If you don't want to deal with an asynchronous load, you can generate a file with just enough Javascript to define a variable:
echo 'var data = ' . json_encode($data) . ';';
and then refer to data in your subsequent Javascript.
Because this keeps the data wholly separate from your HTML and other Javascript files, it shouldn't have any influence at all on how you set up your templates.
I'm trying to speed up response times in my ajax web application by doing the following:
Say the user requests a page whose contents don't change (e.g a web form). When the user makes a different request, I 'cache' the form by putting it in a hidden div. Before displaying the new information. So the form is basically still loaded in the browser but not visible to the user. If the user requests the same form again, it gets loaded from the hidden div. That's notably faster than doing a round-trip to the server for the form.
I do realise doing so with lots of data will probably degrade performance as the browser gets to keep a lot in memory. But I will place a limit on how much gets "cached" this way.
Now, I came up with this on my own which is why I'd like to know if there is a better/established way of doing this. It works as expected but I don't know what the possible drawbacks are (security-related perhaps?).
I would appreciate any suggestions.
Many thanks.
I've done this before. It can be a useful technique. Just make sure the data is accurate and that you support JS disabled user agents.
EDIT: And that there is no better solution.
Storing the HTML code for your form in a JS variable is probably more efficient than having a hidden div with the interpretation of this HTML code (form + form fields + various markup).
If your form code is generated at the same time as the page, simply print it in a JS variable :
<script language="javascript">
var myFormCode = '<? echo $myFormCode; ?>';
</script>
(That's if you use PHP...other languages shouldn't be far from that)
If your form code is generated later, you can send it as text via JSON :
<?php
echo json_encode($myFormCode);
?>
...and then build your form when needed, with something like that on the client side :
<script language="javascript">
myRealFormDiv.innerHTML = eval(myJSONEncodedTextIGotViaAJAX);
</script>
JS code is obviously not exactly what you need to type in, but you probably see my point.
This should work and is the best solution I can think of. Whether there are any security implications really depends on your forms and how they work - nobody will be able to diagnose this without actual code.
What about use APC or Memcached ?
They'll allow you to keep the html markup clean, with not 'hidden tricks' that could potentially create problems (user dont have css enabled? use IE6? what about accessibility?)
Depends on your needs, but in general way the page must contain just what it must containt, nothing else.
Another way of doing this is to set the "Expires" or "Cache-Control" HTTP headers for the form.
If you set an "Expires" header 1 hour in the future for url http://example.com/form.html, then the next time within an hour that the user navigates to that form the HTML will be loaded without touching the server.
If you properly version your images/CSS/JS and give them far-future "Expires" headers as well, then there will be no server roundtrip, plus you'll help the performance the rest of your pages.