| | |
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
![]() |
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
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:
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:
PHP Syntax (Toggle Plain Text)
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
•
•
•
•
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:
PHP Syntax (Toggle Plain Text)
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
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)
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)
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:
PHP Syntax (Toggle Plain Text)
<?php foreach($subcat as $key => $val) { //inner foreach $subcat_id = $val['id']; $sub_category = $val['subcat']; $img_url = $val['img'] ; // is this image url? ?> <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 $img_url; ?>')"><?php echo $sub_category; ?></a> </td> </tr> <?php } //end foreach ?> <script language="Javascript1.2" type="text/javascript"> function changeSrc_subcat1(src) { document.getElementById("cat_subcat_img").src= src; } </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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- How to call a PHP function from Javascript and return the results back into Javascrip (PHP)
- Pass Javascript variable to PHP code (PHP)
- Problem with javascript variable and hidden input (ASP.NET)
- javascript variable in jsp session (JSP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How to get full usernames in Active User Today scripts on forums
- Next Thread: Passing URLs to javascript
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child close column createrange() css cursor decimal dependent design disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms google gwt gxt hiddenvalue highlightedword hint html htmlform ie8 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl listbox math media microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php player post problem progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n






