954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pass a javascript variable to PHP in the same function

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:

foreach($subcat as $key => $val) {   //inner foreach
  $subcat_id = $val['id'];
  $sub_category = $val['subcat']; 
	  
  $subcat1 = array( );
  $subcat_img1[] = $val['img'] ; ?>
	 
 <script language="Javascript1.2"  type="text/javascript"> 
    function changeSrc_subcat1(key1) {
       document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";
		  }
	  </script>
													
	 <tr valign="top">
		 <td valign="top" align="left" nowrap="nowrap">
		  <!-- LINK to display products of the current sub category -->
		  <a href="shw_subcat_prod.php?subcatID=<?php echo $subcat_id; ?>" class="subcat" onMouseOver="changeSrc_subcat1(<?php echo $key; ?>)"><?php echo $sub_category; ?></a>
		  </td>
	 </tr>
	 
	 <?php
	} //end foreach
david555
Newbie Poster
2 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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:

foreach($subcat as $key => $val) {   //inner foreach
  $subcat_id = $val['id'];
  $sub_category = $val['subcat']; 
	  
  $subcat1 = array( );
  $subcat_img1[] = $val['img'] ; ?>
	 
 <script language="Javascript1.2"  type="text/javascript"> 
    function changeSrc_subcat1(key1) {
       document.getElementById("cat_subcat_img").src="<?php echo IMG_SUBCAT_DIR.$subcat_img1["key1"]; ?>";
		  }
	  </script>
													
	 <tr valign="top">
		 <td valign="top" align="left" nowrap="nowrap">
		  <!-- LINK to display products of the current sub category -->
		  <a href="shw_subcat_prod.php?subcatID=<?php echo $subcat_id; ?>" class="subcat" onMouseOver="changeSrc_subcat1(<?php echo $key; ?>)"><?php echo $sub_category; ?></a>
		  </td>
	 </tr>
	 
	 <?php
	} //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

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

Thank you very much that workes !!! :)

david555
Newbie Poster
2 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 
Thank you very much that workes !!! :)

you're welcome... :)

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You