jwhite68 Posted June 26, 2007 Share Posted June 26, 2007 I know that javascript is executed client side, and that the PHP is processed first, server side - but wondered if anyone knows of a way to achieve the following. I have a javascript function: function requestProd{ document.imgProd.src = 'elements/products1.gif'; document.imgServ.src = 'elements/services0.gif'; document.imgEven.src = 'elements/events0.gif'; } When I invoke this javascript function, I want to be able to set a session variable in PHP - eg. <? $_SESSION['pressed'] = 'prod'; ?> ie. I effectively want to execute this, whenever the above function is executed. This is because I have Onclick events in javascript which execute this - and I have similar functions that I want to set the same session variable to different values. This is to allow a PHP script I run later to know what option the user pressed. Anyone got any suggestions? Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 26, 2007 Share Posted June 26, 2007 you want to pass this <? $_SESSION['pressed'] = 'prod'; ?> through javascript? Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted June 26, 2007 Author Share Posted June 26, 2007 Thats right. In the function I quoted. Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 26, 2007 Share Posted June 26, 2007 im not sure try this <?php $id=$_SESSION['pressed'] ; function requestProd{ document.imgProd.src = 'elements/products1.gif?id=<?=$id?>'; document.imgServ.src = 'elements/services0.gif'; document.imgEven.src = 'elements/events0.gif'; } ?> Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted June 26, 2007 Author Share Posted June 26, 2007 If I do this, surely it wont recognise requestProd as a javascript function? Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 26, 2007 Share Posted June 26, 2007 could you post the code ?.so that i will try to solve it Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 When I invoke this javascript function, I want to be able to set a session variable in PHP - eg. Javascript cannot execute scripts on the server without making another http request. Whether that be simply calling another page or through the use of the XMLHttpRequest object (aka Ajax). Quote Link to comment Share on other sites More sharing options...
TreeNode Posted June 26, 2007 Share Posted June 26, 2007 You'll need to use cookies. This is what I have for one of my websites that hides a layer when clicked it is clicked on: function toggleLayer(whichLayer){ if (document.getElementById){ // this is the way the standards work var style2 = document.getElementById(whichLayer).style; style2.display = style2.display? "":"none"; } else if (document.all){ // this is the way old msie versions work var style2 = document.all[whichLayer].style; style2.display = style2.display? "":"none"; } else if (document.layers){ // this is the way nn4 works var style2 = document.layers[whichLayer].style; style2.display = style2.display? "":"none"; } } function componentMinMax(the_layer){ toggleLayer(the_layer); if (getCookie(the_layer) == "no") setCookie(the_layer, "yes", 365); else setCookie(the_layer, "no", 365); } function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1){ c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; } function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); } My button or anything really will have an onclick event such as this: <a href="javascript:void(0);" onClick="javascript:componentMinMax('browsecategories');" >click here</a> And my hidden div: <?php $component_display = ""; if ($_COOKIE["browsecategories"] == "no") $component_display = 'style="display: none"'; ?> <div id="browse" <?=$component_display?> >sometimes hidden, sometimes shown</div> Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted June 27, 2007 Author Share Posted June 27, 2007 Thanks for the advice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.