pass php value to js

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

pass php value to js

Post by Weirdan »

Seems people just won't stop asking for this =)
Ok. Here is the basic example on how to pass some php value to javascript. Currently it works only with strings and arrays (but arrays can have arbitrary number of dimensions). Use it like I did in testing section.

Code: Select all

//------------------------------------------------//
 // PHP2JS example                                 //
 //                                                //
 // Author: Weirdan                                //
 //                                                //
 // You can use this example for whatever purpose. //
 // Feel free to modify and improve it in any way. //
 // NO WARRANTIES, of course =)                    //
 // It would be nice to mention my name if you use //
 // this snippet.                                  //
 //------------------------------------------------// 

 /**
  * php2js - convert php value to JavaScript code string
  *
  * @param   mixed   $php      value to convert
  * @param   string  $js_name  how we should name the variable 
  *                            in js output
  *
  * @return  string            js code. You can echo it to
  *                            browser.
  */
 function php2js($php,$js_name='') {
     $js = '';
     $top_level = (bool) strlen($js_name);
     
     if($top_level) {
        $js = 'var ' . $js_name . ' = ';
     }
     
     switch ( strtolower( gettype($php) ) ) {
         case 'array':
             $js .= 'new Array( ';
                     
             foreach(array_values($php) as $pos => $php_val) {
                 if($pos > 0)
                    $js .= ', ';
                 $js .= php2js($php_val);
             }
             
             $js .= ' ) ';
             break;
             
         default: // treat all other types as strings
            $js .= '''' . strval($php) . '''';
            break;
     }
     
     return $js . ($top_level ? ';' : '');
 }     

// testing
 $array = array(
         "something" => array("something else", "someone else"),
         "asd"
         );
 echo php2js($array,'some_array');
Your comments are welcome.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

How about doing a reverse

Post by n00b Saibot »

Hey I read ur article .
was goooood enuf .
but my problem is just the reverse.
i want to input some values form user thru js
by using prompts,confirms etc. and want to use those in PHP

Can ya help me on this!
:?:
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

PHP processes everything server-side. JavaScript process everything client-side. So anything you do in JavaScript must be posted back to the server so that PHP can process that data and return back to the client.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How about doing a reverse

Post by Weirdan »

n00b Saibot wrote: but my problem is just the reverse.
i want to input some values form user thru js
by using prompts,confirms etc. and want to use those in PHP
Basically, there's two methods for js code to communicate with php:
  1. Simple: assign your js values to hidden fields of a form. When user submits the form, the values will get transmitted (either as POST or GET vars) to your php script. Alternatively you could construct the link with all the vars set as you wish and then force the user's browser to visit the link by assigning it to document.location.href.
  2. Advanced, web-service like: check out JPSPAN docs.
Last edited by Weirdan on Tue Jul 18, 2006 10:45 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

nowadays the hype is to use xmlhttprequest in javascript ;)

for example http://jpspan.sourceforge.net offers some handy scripts ;)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

thanx to ya 4 ur guidance. all of ya :P

That clears a lot.

there's one question (more of a bit like poll question, he he): D'ya think that if we look at features provided in JS and PHP and compare them, we find that features-wise JS is may be equally/more powerful than PHP.
Again, if that is a silly thing 2 ask please 4give me :P
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

n00b Saibot wrote: Again, if that is a silly thing 2 ask please 4give me
You're forgiven :lol:
Last edited by Weirdan on Tue Jul 18, 2006 10:46 am, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

He he he :D :D :D
Post Reply