Page 1 of 1

Passing javascript variable to PHP

Posted: Sat Jan 09, 2010 11:59 am
by trickydicky
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??

Re: Passing javascript variable to PHP

Posted: Sat Jan 09, 2010 12:29 pm
by califdon
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

Re: Passing javascript variable to PHP

Posted: Sun Jan 10, 2010 7:02 am
by jayshields
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.

Re: Passing javascript variable to PHP

Posted: Mon Jan 11, 2010 2:09 pm
by trickydicky
Thanks guys, im gonna have a little look on the site and put my thinking cap on...

Re: Passing javascript variable to PHP

Posted: Mon Jan 11, 2010 10:12 pm
by SimpleManWeb
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

Posted: Mon Jan 11, 2010 10:59 pm
by califdon
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.
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.

Re: Passing javascript variable to PHP

Posted: Tue Jan 12, 2010 12:56 pm
by trickydicky
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???

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>
and the experiment.php file has...

Code: Select all

<?php
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
echo $year;
?>
At work it said 2010. Now nudda!

Any ideas??

Re: Passing javascript variable to PHP

Posted: Tue Jan 12, 2010 6:31 pm
by califdon
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.

Re: Passing javascript variable to PHP

Posted: Wed Jan 13, 2010 12:25 pm
by trickydicky
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

Posted: Sat Oct 30, 2010 3:15 pm
by alacka
Maybe find something useful here Passing javascript variable to PHP