Jump to content

getting option value


acctman

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/#findComment-873538
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/#findComment-873605
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/#findComment-873710
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/#findComment-874376
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/165391-getting-option-value/#findComment-874638
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.