Jump to content

Embed PHP in javascript


jwhite68

Recommended Posts

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?

Link to comment
Share on other sites

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';

}

 

 

 

?>

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.