Current location: Hot Scripts Forums » Programming Languages » PHP » Passing Javascript Variable to PHP


Passing Javascript Variable to PHP

Reply
  #1 (permalink)  
Old 06-23-05, 05:59 PM
watadude watadude is offline
New Member
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Passing Javascript Variable to PHP

Attempting: Building HTML code for a 2-level menu. An PHP array is created from a MySQL query for both levels. A javascript function handles the 'onmouseover' event to populate the 2nd level of the menu.

I had everthing working until I began passing anchor tags to the js function.

So instead of passing preformated HTML text (entire 2nd menu level) to the js function, I decided to pass only the required index to the PHP array.

Problem: Converting the passed js argument (index) to a PHP variable. Quite simple I imagine, but I have researched this and everything I tried to plug in - fails. Here's the line I'm trying to get right:
PHP Code:

    $indx = echo '<script type="text/JavaScript"> echo index; </script>'
Here's the js function with embedded PHP code:

PHP Code:

function show_text(index,whichdiv){

  <?php
//  $indx = 2;  // Hardcode Works - but index actually varies with ea. call
//  Now try passing JS variable:  does not work
    
$indx = echo '<script type="text/JavaScript"> echo index; </script>';  

    
$arrCmds   array_values($arrButtons[$indx]['cat2']);
    
$arrRefs   array_values($arrButtons[$indx]['ref2']);

    
$txtSubMenu '';
    for (
$c=0; ($c count($arrCmds)); $c++) {
      if ((
$c+1) < count($arrCmds)) {
        
$txtSubMenu .= '<a href='.$arrRefs[$c].'>'.$arrCmds[$c].'</a>  |  ';
      }
      else {
        
$txtSubMenu .= '<a href='.$arrRefs[$c].'>'.$arrCmds[$c].'</a>';
      }
    }
    echo 
"thetext = '$txtSubMenu';\n  ";
  
?>

  var text = '<font size="2" face="Arial" color=<?php echo $colDarkGray;?>>'+
                 thetext+'</font>';
    
  if (ie) eval("document.all."+whichdiv).innerHTML=text
  else if (ns6) document.getElementById(whichdiv).innerHTML=text
}
Thanks,
Steve
Reply With Quote
  #2 (permalink)  
Old 06-23-05, 09:22 PM
sarat.pediredla sarat.pediredla is offline
Newbie Coder
 
Join Date: Jun 2005
Location: Newcastle upon Tyne UK
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Steve,

Javascript and PHP ARE NOT THE SAME. You're thinking, duh! obviously but I mean Javascript is a "client side" technology while PHP is a "server side" technology.

Your Javascript can read from PHP variables only because the .php page is being processes on the server "first" and the output returned. Hence any variables will be output into the JS.

However, the reverse is NOT true. Your Javascript does not have access to any PHP information as it resides on the server. The only way you can make JS variables accessible to PHP is to make POST or GET back to the server (call the .php again) with the required variables+values.
__________________

--------------------------------
Sarat Pediredla
[Writing a lot about nothing]
Blog : Space & Beyond


<a href="http://www.spreadfirefox.com/?q=affiliates&amp;id=108865&amp;t=68"><img border="0" alt="Get Firefox!" title="Get Firefox!" src="http://sfx-images.mozilla.org/affiliates/Buttons/88x31/take.gif"/></a>
Reply With Quote
  #3 (permalink)  
Old 06-23-05, 11:50 PM
watadude watadude is offline
New Member
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, one (or two) more questions.

Sarat

Thanks, I'm new at this. As I understand, the PHP code is parsed first on the server, all executions/substitutions are made and HTML code is the result.

Now in the following code, the 'onmouseover' code is rendered as "show_text('1','Words')" for button #1 and this is stored as part of the HTML code.

Once the mouse hovers over it on the client side, then the JS "show_text()" is actually called and executed. The PHP array no longer exists. Right so far?

Quote:
The only way you can make JS variables accessible to PHP is to make POST or GET back to the server (call the .php again) with the required variables+values.
Can you give an example of calling the same php with var=value? Would this refresh (repaint the browser) the same page everytime it is called?

Could I give the table cell (<td id="value1,value2">) an ID with the (2) values I want to recall in my JS show_text() function?

Thanks again,
Steve


PHP Code:

<table>

  <tr>
<?php
  
for ($j=0; ($j count($arrButtons)); $j++) {
    
$txtButton $arrButtons[$j]['cat1'];
    
$txtColor  $arrButtons[$j]['col1'];
    
$tblID     "tbl".$txtButton;
?>
   <td>
      <table id=<?=$tblID;?> >
        <tr>
          <td bgcolor=<?=$colMidGray;?> 
              onclick="dspSubMenu.php" 
              onmouseover="show_text('<?=$j;?>','Words')" 
              onmouseout="reset('Words')" 
              style="cursor:pointer; cursor:hand">
                            
              <p align="center"><font face="Arial" color=<?=$txtColor;?> ><b>
              <?=$txtButton;?>
              </b></font></p>
          </td>
        </tr>
      </table>
    </td>
<?php
    
}
?>
  </tr>
</table>
    
<table>    
    <tr height="30">
        <td align="center" height="30">
        <span id="Words">
        <font size="2" face="Arial" color=<?=$colDarkGray;?> >
        Select A Category Above
        </font>
        </span>
        </td>
    </tr>
</table>
Reply With Quote
  #4 (permalink)  
Old 06-24-05, 02:06 AM
watadude watadude is offline
New Member
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Here's some more code that doesn't work. help!

Ok, here's my new attempt:

The first code frag converts a PHP 2D array ($arrButtons[]) to javascript array ($js_menu()):

PHP Code:

<?php

$js_menu 
'var arrButts = new Array(';
for (
$x=0$x count($arrButtons); $x++) {
  
$js_menu .= '"'.$arrButtons[$x]['cat2'].'"';
  if (
$x < (count($arrButtons)-1)) {
    
$js_menu .= ',';
  }
}
$js_menu .= ')';
?>
This 2nd set uses the prev created JS array to display on the page:

PHP Code:

<?php

  
echo<<<END
  <script language="JavaScript">
  <!-- Begin
  var ns6=document.getElementById&&!document.all
  var ie=document.all
  
$js_menu        // var arrButts=new Array(,,,)
  function show_text(index,whichdiv){
    thetext = arrButts(index)
    var text = '<font size="2" face="Arial" color="#FFFFFF" >'+thetext+'</font>'
        
    if (ie) eval("document.all."+whichdiv).innerHTML=text
    else if (ns6) document.getElementById(whichdiv).innerHTML=text
  }
  //  End -->
  </script>
  END;
?>
Most of this code was extracted from various gurus in this forum - thanks.

Somewhere in here is an error that prevents the whole page from loading. There's a nickel for the first persons who finds it, so I can get some sleep!

Thanks,
Steve
Reply With Quote
  #5 (permalink)  
Old 01-15-11, 06:44 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by watadude View Post
Ok, here's my new attempt:

The first code frag converts a PHP 2D array ($arrButtons[]) to javascript array ($js_menu()):

PHP Code:

<?php
$js_menu 
'var arrButts = new Array(';
for (
$x=0$x count($arrButtons); $x++) {
  
$js_menu .= '"'.$arrButtons[$x]['cat2'].'"';
  if (
$x < (count($arrButtons)-1)) {
    
$js_menu .= ',';
  }
}
$js_menu .= ')';
?>
This 2nd set uses the prev created JS array to display on the page:

PHP Code:

<?php
  
echo<<<END
  <script language="JavaScript">
  <!-- Begin
  var ns6=document.getElementById&&!document.all
  var ie=document.all
  
$js_menu        // var arrButts=new Array(,,,)
  function show_text(index,whichdiv){
    thetext = arrButts(index)
    var text = '<font size="2" face="Arial" color="#FFFFFF" >'+thetext+'</font>'
        
    if (ie) eval("document.all."+whichdiv).innerHTML=text
    else if (ns6) document.getElementById(whichdiv).innerHTML=text
  }
  //  End -->
  </script>
  END;
?>
Most of this code was extracted from various gurus in this forum - thanks.

Somewhere in here is an error that prevents the whole page from loading. There's a nickel for the first persons who finds it, so I can get some sleep!

Thanks,
Steve
You need to use AJAX.

Setup your main page to look the way you want when it first loads.
Then use the body elements onload event to invoke an Ajax routine that calls a PHP page that sends back the info you need to populate your JavaScript array.
Now you can use JavaScript to grab values from the array to use in your HTML.
Any time you need new data for your JavaScript array, you just invoke the Ajax routine to get the requested data.

You have to remember that you can not mix PHP and JavaScript at the server.
PHP has to be requested from the server and JavaScript can then use it at the client.
__________________
Jerry Broughton

Last edited by job0107; 01-15-11 at 06:50 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Include PHP via javascript bkbenson JavaScript 4 02-19-05 12:40 AM
Passing a PHP variable in Javascript??? todayscoffee JavaScript 2 01-08-05 09:45 AM
Passing a PHP variable in Javascript??? todayscoffee PHP 2 01-04-05 01:56 PM
javascript multiple select menu for php? isaacmlee JavaScript 1 10-15-04 09:53 AM
javascript variable passed to JSP workaround! peejay Everything Java 0 08-05-04 12:41 AM


All times are GMT -5. The time now is 06:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.