Passing javascript variable to PHP
Moderator: General Moderators
-
trickydicky
- Forum Newbie
- Posts: 6
- Joined: Wed Dec 30, 2009 12:58 pm
Passing javascript variable to PHP
hey,
let me start by saying that im totally new to PHP, so there might be an easy solution to this. I have a javascript calendar that i want users to be able to click. The exact date they pick gets saved as a variable called "FolderVariable". I need to pass this to PHP so it can open the Date as a folder structure so it can read a file.
My confusion is that, obviously PHP is server side and java is client-side, so im not sure how its possible to pass it. Id like to avoid refreshing the page as the calendar resets to the current day, and id like to keep the date thats been picked.
Any ideas, links, suggestions anyone??
let me start by saying that im totally new to PHP, so there might be an easy solution to this. I have a javascript calendar that i want users to be able to click. The exact date they pick gets saved as a variable called "FolderVariable". I need to pass this to PHP so it can open the Date as a folder structure so it can read a file.
My confusion is that, obviously PHP is server side and java is client-side, so im not sure how its possible to pass it. Id like to avoid refreshing the page as the calendar resets to the current day, and id like to keep the date thats been picked.
Any ideas, links, suggestions anyone??
Re: Passing javascript variable to PHP
You've got the right idea. The 3 ways to do it are to pass the date info as POST data in a form, as GET data as part of the URL calling the PHP script (both of which result in refreshing the page, which you do not want to do), or using AJAX (Asynchronous Javascript And XML), with which you can call a PHP script and receive the resulting data without sending a new page to the browser. Obviously, this is what you want to use. Don't be intimidated by AJAX. It's NOT another language to learn, it's just a technique for using a built-in object of Javascript, called the XMLHttpRequest object. Since you evidently have some knowledge of Javascript, that won't be hard to handle. And the PHP script can be extremely simple, depending on what you need to do, server side. Once the PHP script has generated the required data, it simply uses echo (or print) and the data is sent back to the browser to the Javascript function that you create to do whatever you need to do on the page, without refreshing the entire page. There's a lot of information on AJAX on the web. You might start here: http://www.w3schools.com/Ajax/ajax_intro.asp
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: Passing javascript variable to PHP
To add to what califdon said, you should use a Javascript library like jQuery to help you with AJAX - it makes things much, much easier.
-
trickydicky
- Forum Newbie
- Posts: 6
- Joined: Wed Dec 30, 2009 12:58 pm
Re: Passing javascript variable to PHP
Thanks guys, im gonna have a little look on the site and put my thinking cap on...
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: Passing javascript variable to PHP
One thing to keep in mind though is that JQuery is a fairly large library. Although it does make AJAX much easier, if the site you are trying to build is fairly simple it will probably make more sense to build the AJAX code the old fashioned way so that your site won't get slowed down. I personally love JQuery, but that's its only downside.
Re: Passing javascript variable to PHP
I'm glad you added that, I feel the same way. Like quite a few "frameworks" and libraries, you really should balance the tools with the task. Used in appropriate situations, most of them can be very valuable, but in other settings may be much more complex to use, if for no other reason that you have to learn a new syntax to get the benefits.SimpleManWeb wrote:One thing to keep in mind though is that JQuery is a fairly large library. Although it does make AJAX much easier, if the site you are trying to build is fairly simple it will probably make more sense to build the AJAX code the old fashioned way so that your site won't get slowed down. I personally love JQuery, but that's its only downside.
-
trickydicky
- Forum Newbie
- Posts: 6
- Joined: Wed Dec 30, 2009 12:58 pm
Re: Passing javascript variable to PHP
Think JQuery might have to be something to put on my list to learn. Its only the one thing i need it for.. at the moment. But i will defo be doing more php development in the future.
Anyway after your suggestions before i went off an learnt a few things about AJAX. today on my ubuntu server at work ive done some testing and got the script below working. Its a real basic script so i can understand how ajax works and i did get it working.. on my work server. This evening i got home and copied across the same code to my cousins site and it doesnt work. Ive pasted the code below, but my question is, is there any reason why not that you can think of???
and the experiment.php file has...
At work it said 2010. Now nudda!
Any ideas??
Anyway after your suggestions before i went off an learnt a few things about AJAX. today on my ubuntu server at work ive done some testing and got the script below working. Its a real basic script so i can understand how ajax works and i did get it working.. on my work server. This evening i got home and copied across the same code to my cousins site and it doesnt work. Ive pasted the code below, but my question is, is there any reason why not that you can think of???
Code: Select all
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var d = 15;
var m = 1;
var y = 2010;
var queryString = "?year=" + y + "&month=" + m + "&day=" + d;
xmlhttp.open("GET","experiment.php" + queryString,true);
xmlhttp.send(null);
document.getElementById('test').innerHTML=xmlhttp.responseText;
}
</script>
</head>
<body>
<div id="test">
<h2>Click to let AJAX change this text</h2>
</div>
<button type="button" onclick="loadXMLDoc()">Click Me</button>
</body>
</html>Code: Select all
<?php
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
echo $year;
?>Any ideas??
Re: Passing javascript variable to PHP
It looks like you've skipped some crucial lines of code in your function, specifically those that define the return function and those that test for readyState==4. I'm sure that the version that worked at work had those lines.
-
trickydicky
- Forum Newbie
- Posts: 6
- Joined: Wed Dec 30, 2009 12:58 pm
Re: Passing javascript variable to PHP
Well i looked into it today, and the code above posted did work locally, but only when i clicked the button twice. No idea why or if that makes sense, but after your suggestion i changed the code to that below and it worked like a treat! Hopefully it will help others out someday! Thanks for your help!
Code: Select all
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var d = 15;
var m = 1;
var y = 2010;
var queryString = "?year=" + y + "&month=" + m + "&day=" + d;
//xmlhttp.open("GET","experiment.php" + queryString,true);
//xmlhttp.send(null);
//document.getElementById('test').innerHTML=xmlhttp.responseText;
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{document.getElementById('test').innerHTML=xmlhttp.responseText}
}
xmlhttp.open("GET","experiment.php" + queryString,true);
xmlhttp.send(null);
}
Re: Passing javascript variable to PHP
Maybe find something useful here Passing javascript variable to PHP