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
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
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
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
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?

Link to comment
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
Share on other sites

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.

Link to comment
Share on other sites

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

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.