-1

I want to define a variable ($_SESSION['name']) when a option is selected (without posting) When select a option, it should define the the variable without posting or refreshing the page. For example something like this, maybe with jQuery or something. Can someone help me out?

   <select name="location">
        <option <?php $_SESSION['name'] = "US";?>>US</option>
        <option <?php $_SESSION['name'] = "GB";?>>GB</option>
        <option <?php $_SESSION['name'] = "DE";?>>DE</option>
    </select>
derd1199
  • 31
  • 6

2 Answers2

2

ok, do this:

<select name="location" onChange="thingChanged(this)">
    <option value="US">US</option>
    <option value="GB">GB</option>
    <option value="DE">DE</option>
</select>

<script type="text/javascript">
    function thingChanged(thing){
        $.post('pageMarkingSessionVar.php',{
                sel:$(thing).val();
               });
    }
</script>

then on pageMarkingSessionVar.php do:

<?php
  session_start();
  if(isset($_POST["sel"])){
    $_SESSION["name"] = $_POST["sel"];
  }
?>

quick and dirty but you get the idea.

edit: oh yeh, make sure jQuery is loaded

NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
1

I'm not sure about what you want exactly, but maybe it can help:

var freq = 1000;

function getLocation(){
  var location = document.getElementsByName("location").value
  var ajax_req = new XMLHttpRequest();
  if(ajax_req.readyState==0 || ajax_req.readyState==4){
    ajax_req.open('POST','yourwebsite.io',true);
    ajax_req.onload=handleResponse;
    ajax_req.send("loc="+location);
   }
   setTimeout('getLocation()', freq);
}

function handleResponse(){
  //Handle response
}

getLocation();
   <select name="location">
        <option value="US">US</option>
        <option value="GB">GB</option>
        <option value="DE">DE</option>
    </select>

This is HTML/JS, you just have to do $_SESSION['name'] = $_POST['loc'] in yourwebsite.io's php file. Make sure you've enabled session by adding session_start(); before ANY HTML code (more info here).

BuzzRage
  • 184
  • 1
  • 12