[nycphp-talk] Passing JAVASCRIPT variables to PHP

Peter Sawczynec ps at
Mon Mar 31 17:23:52 EDT 2008

To complicate this little ole back to basics essential item.
Here lies crude but doable intermingling of php into js on the fly as a
web page is being created. 
So you are passing php vars to js, you just do all of it 100% in advance
before you even output a single drop 
of final scripts or html. 

...Happy coding.

Pseudo code of sorts follows: 


turn on output buffering

php does database stuff, push results into master php arrays, create
handy chunks of custom html, whatever...
   $user_name = get_uname();
   $user-status = get_ustat();   
   $allowed_dates_array = get_allowed_date_range();
   $allowed_currences_array = get_allowed_currencies();
   $memeber_types_array = get_allowed_member_types();

echo initial html headers...

//start creating javascript dynamically on the fly,
//creating js arrays, validation script, whatever...  
//using the info and arrays created in pure php above...
echo "<script>\n";
echo "var allowed_dates[] = "; 

   for each $allowed_dates_array
	echo $allowed_dates array item . ", \n"
        ... ... ... ...

echo "function validate_dates_with_js(date_to_check){\n"
echo "  if (date_to_check in allowed_dates){ "
echo "   //then do stuff"
echo "   }\n"
echo "}\n"
        ... ... ... ...

//in further html to echo down below create links, drop down lists or
sets of needed check boxes or radio buttons 
//by iterating through the same above master php arrays, e.g.:

echo "<input type=select name=allowed_dates_select
echo "<option> ---- </option>\n";

for each $allowed_dates_array
	echo "<option>".$allowed_dates array item . "</option>\n";

echo "</select>";

//so now the javascript arrays and validation functions generated above
are totally coordinated with 
//the web page html items that need to be validated or linked or
associated in harmony

//You can use the same logic to create custom links, custom
announcements, whatever... that is exactly 
//coordinated to this user, e.g:

echo "<a
m2=".encrypt_stuff($user_status).\">Go get your data, ".$user_name."

turn off output buffering
output all the content created above 


-----Original Message-----
From: talk-bounces at [mailto:talk-bounces at]
On Behalf Of tedd
Sent: Monday, March 31, 2008 9:28 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Passing JAVASCRIPT variables to PHP

At 6:07 PM -0400 3/30/08, Daniel Convissor wrote:
>Hi Paul:
>On Sun, Mar 30, 2008 at 09:18:10PM +0100, PaulCheung wrote:
>>  The problem is I cannot transfer the Javascript variable needed in
to PHP.
>JavaScript is client side.  PHP is server side.  This has been
>on this list a couple times, and on the web too many times to count.
>You either need to submit the JS data to the server as part of a form
>via an AJAX request.


And don't forget this way:  :-)

var a = 1;
(new Image()).src = '/myscript.php?a='+ a;



New York PHP Community Talk Mailing List

NYPHPCon 2006 Presentations Online

Show Your Participation in New York PHP

More information about the talk mailing list