Pass a javascript variable to PHP in the same function

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Aug 2007
Posts: 2
Reputation: david555 is an unknown quantity at this point 
Solved Threads: 0
david555 david555 is offline Offline
Newbie Poster

Pass a javascript variable to PHP in the same function

 
0
  #1
Aug 22nd, 2007
hi,
my purpose of my code is to get an array of subcategory details
and format it and when when mouse over some subcategory
- an image will change (dynamically).
this code is PHP with Javascript


My problem is how can i get the variable 'key1' from the javascript
funtion to the PHP which echoes the .src
here: (key1 is the a key number is an array of images)
document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";



my full code:
  1. foreach($subcat as $key => $val) { //inner foreach
  2. $subcat_id = $val['id'];
  3. $sub_category = $val['subcat'];
  4.  
  5. $subcat1 = array( );
  6. $subcat_img1[] = $val['img'] ; ?>
  7.  
  8. <script language="Javascript1.2" type="text/javascript">
  9. function changeSrc_subcat1(key1) {
  10. document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";
  11. }
  12. </script>
  13.  
  14. <tr valign="top">
  15. <td valign="top" align="left" nowrap="nowrap">
  16. <!-- LINK to display products of the current sub category -->
  17. <a href="shw_subcat_prod.php?subcatID=<?php echo $subcat_id; ?>" class="subcat" onMouseOver="changeSrc_subcat1(<?php echo $key; ?>)"><?php echo $sub_category; ?></a>
  18. </td>
  19. </tr>
  20.  
  21. <?php
  22. } //end foreach
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Pass a javascript variable to PHP in the same function

 
0
  #2
Aug 23rd, 2007
Originally Posted by david555 View Post
hi,
my purpose of my code is to get an array of subcategory details
and format it and when when mouse over some subcategory
- an image will change (dynamically).
this code is PHP with Javascript


My problem is how can i get the variable 'key1' from the javascript
funtion to the PHP which echoes the .src
here: (key1 is the a key number is an array of images)
document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";



my full code:
  1. foreach($subcat as $key => $val) { //inner foreach
  2. $subcat_id = $val['id'];
  3. $sub_category = $val['subcat'];
  4.  
  5. $subcat1 = array( );
  6. $subcat_img1[] = $val['img'] ; ?>
  7.  
  8. <script language="Javascript1.2" type="text/javascript">
  9. function changeSrc_subcat1(key1) {
  10. document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";
  11. }
  12. </script>
  13.  
  14. <tr valign="top">
  15. <td valign="top" align="left" nowrap="nowrap">
  16. <!-- LINK to display products of the current sub category -->
  17. <a href="shw_subcat_prod.php?subcatID=<?php echo $subcat_id; ?>" class="subcat" onMouseOver="changeSrc_subcat1(<?php echo $key; ?>)"><?php echo $sub_category; ?></a>
  18. </td>
  19. </tr>
  20.  
  21. <?php
  22. } //end foreach
You cannot pass a variable from JavaScript to PHP on the server, or pass PHP to javascript on the client.
JavaScript executes on the client (usually a browser) and PHP on the web server.

The only cases where passing new params from client to/from server this is needed, is if you want the browser to retrieve new information from the server. For this, the Client will make a new HTTP Request to the PHP page, and retrieve variables passed as strings in the HTTP Response. (Eg: XMLHttpRequest, Iframe Remoting, On Demand JS etc.)

In the example you have, all the information needed exists on one execution of a PHP file/page on the Server.

I believe what you want to do is write these PHP variables into an HTML page so they can be used by JavaScript.

To do this, you'll need to output the PHP variables as JavaScript variables. These variables can be written as JavaScript literals between the <script> tags, or placed in the HTML event handlers.

In the example below, the PHP variable, $img_url; , is placed directly in the event handler.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. onMouseOver="changeSrc_subcat1('<?php echo $img_url; ?>')"

When the HTML is printed out, it will look like this example:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. onMouseOver="changeSrc_subcat1('http://example.com/image.jpg')"

As you can see, the $img_url variable in PHP is printed out in HTML as a JavaScript String Literal.

Heres the example code:


  1. <?php
  2.  
  3. foreach($subcat as $key => $val) { //inner foreach
  4. $subcat_id = $val['id'];
  5. $sub_category = $val['subcat'];
  6. $img_url = $val['img'] ; // is this image url?
  7.  
  8. ?>
  9.  
  10. <tr valign="top">
  11. <td valign="top" align="left" nowrap="nowrap">
  12. <!-- LINK to display products of the current sub category -->
  13. <a href="shw_subcat_prod.php?subcatID=<?php echo $subcat_id; ?>" class="subcat" onMouseOver="changeSrc_subcat1('<?php echo $img_url; ?>')"><?php echo $sub_category; ?></a>
  14. </td>
  15. </tr>
  16.  
  17. <?php
  18. } //end foreach
  19.  
  20. ?>
  21.  
  22. <script language="Javascript1.2" type="text/javascript">
  23. function changeSrc_subcat1(src) {
  24. document.getElementById("cat_subcat_img").src= src;
  25. }
  26. </script>
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 2
Reputation: david555 is an unknown quantity at this point 
Solved Threads: 0
david555 david555 is offline Offline
Newbie Poster

Re: Pass a javascript variable to PHP in the same function

 
0
  #3
Aug 23rd, 2007
Thank you very much that workes !!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Pass a javascript variable to PHP in the same function

 
0
  #4
Aug 23rd, 2007
Originally Posted by david555 View Post
Thank you very much that workes !!!
you're welcome...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC