acctman Posted July 9, 2009 Share Posted July 9, 2009 the alert box shows fine but i think my code where it checks the option value maybe work. <?php session_start(); ?> <script language="JavaScript"> <!-- function changeBack(form){ for (var i = 0; i < form.mySelect.options.length; i++) { if (form.mySelect.options[i].selected){ alert("Photo will be saved to your " + form.mySelect.options[i].text + " Folder"); if (form.mySelect.options[i].value = "1") { <?php ($_SESSION[phfolder] = 1); ?> } else if (form.mySelect.options[i].value = "2") { <?php ($_SESSION[phfolder] = 2); ?> } else if (form.mySelect.options[i].value = "3") { <?php ($_SESSION[phfolder] = 3); ?> } } } } --> </script> [code] Quote Link to comment Share on other sites More sharing options...
corbin Posted July 10, 2009 Share Posted July 10, 2009 Errrrr..... JS and PHP don't work like that. At all.... PHP is executed before anything is sent to the client, and JS is executed by the client. So, that won't work. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 You can use AJAX to call a php script that sets your session variable. Quote Link to comment Share on other sites More sharing options...
acctman Posted July 11, 2009 Author Share Posted July 11, 2009 You can use AJAX to call a php script that sets your session variable. i was using this example that was written for me by another helpful forum member. it works fine as a standalone but when i add the code to my smarty tpl file it does not work. my guess is that it has something to do with this line var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; i think the easiest fix would be to put the code below in an iframe, to stop an conflict my template file may have with it. <?php session_start(); if(!empty($_GET['session'])) { $_SESSION['phfolder'] = $_GET['session']; exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Set session</title> </head> <body> <script language="javascript"> function ajaxFunction(session) { var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; var xmlHttp; try { xmlHttp=new XMLHttpRequest(); }catch(e){ try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.open("GET", loaderphp+"?session="+session,true); xmlHttp.send(null); } </script> <form action="" method="post" enctype="multipart/form-data"> <select name="folder" onchange="javascript:ajaxFunction(this.value);"> <option value="" selected="selected">Select</option> <option value="1">Public</option> <option value="2">Mobile</option> <option value="3">Private</option> </select> Folder </form> <?php echo $_SESSION['phfolder']; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 it works fine as a standalone but when i add the code to my smarty tpl file it does not work. my guess is that it has something to do with this line var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; It has nothing to do with that at all. It's because you are trying to set php sessions with javascript - you can't do that. Javascript doesn't have direct access to php, you need to do a behind the scenes call to a php script. This is called AJAX, as Crayon Violent mentioned. Quote Link to comment Share on other sites More sharing options...
acctman Posted July 11, 2009 Author Share Posted July 11, 2009 it works fine as a standalone but when i add the code to my smarty tpl file it does not work. my guess is that it has something to do with this line var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; It has nothing to do with that at all. It's because you are trying to set php sessions with javascript - you can't do that. Javascript doesn't have direct access to php, you need to do a behind the scenes call to a php script. This is called AJAX, as Crayon Violent mentioned. the first code i post and my second post are two different codes. the second code is ajax, which works fine as a standalone example but when i put the ajax code into my smarty template it no longer works. Quote Link to comment Share on other sites More sharing options...
haku Posted July 12, 2009 Share Posted July 12, 2009 Ah, I didn't see that the 2nd code was different. I don't know much about the smarty templating system, what do you see when you do a print source on the page? Is the javascript in there? What is the value for loader? Quote Link to comment Share on other sites More sharing options...
acctman Posted July 13, 2009 Author Share Posted July 13, 2009 Ah, I didn't see that the 2nd code was different. I don't know much about the smarty templating system, what do you see when you do a print source on the page? Is the javascript in there? What is the value for loader? this is what the html source shows var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; the php code wasn't processed. Quote Link to comment Share on other sites More sharing options...
haku Posted July 13, 2009 Share Posted July 13, 2009 That's your problem then! I seem to recall one time reading that you had to enclose php in curly braces with smarty. So try turning this: <?php echo $_SERVER['PHP_SELF']; ?> to this {<?php echo $_SERVER['PHP_SELF']; ?>} and see if that helps. Quote Link to comment Share on other sites More sharing options...
acctman Posted July 13, 2009 Author Share Posted July 13, 2009 That's your problem then! I seem to recall one time reading that you had to enclose php in curly braces with smarty. So try turning this: <?php echo $_SERVER['PHP_SELF']; ?> to this {<?php echo $_SERVER['PHP_SELF']; ?>} and see if that helps. no luck Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted July 13, 2009 Share Posted July 13, 2009 for javascript code, such as ajax calls in smarty you have to enclose them with literal tags: {literal} <script type=text/javscript> function ... </script> {/literal} and for php, use php tags: {php} if(blah==blah) etc etc {/php} like so Quote Link to comment Share on other sites More sharing options...
haku Posted July 13, 2009 Share Posted July 13, 2009 Ahh, I thought I had heard something about curly tags, I just didn't know the proper syntax. Not used smarty myself. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted July 14, 2009 Share Posted July 14, 2009 yea we dabbled for a while, its great if the design of a site is changing a lot, but the content stays the same. so, since im a programmer, i hated it because it was more work for me 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.