PHP/HTML if option is selected -> define variable [duplicate]

114
January 18, 2018, at 07:15 AM

This question already has an answer here:

  • Set Session variable using javascript in PHP 8 answers

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>
Answer 1

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

Answer 2

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

READ ALSO
Get object list with ajax in Django

Get object list with ajax in Django

I'm using ajax in my django aplication, it works well except when I want to get all the registers in a tableI don't know how to send from my 'ajaxview' to the javascript code and then, how to parse the result

240
Shuffled divs not deleting properly due to array organising in specific order

Shuffled divs not deleting properly due to array organising in specific order

I am working with some playing cards which show my different web devskills (ironic that I need help when one of those skills says jQuery)

93
how to make a working delete button on a datatable

how to make a working delete button on a datatable

so i want to add a button to a DataTable, the button should delete an account from the database, im using, node js, expresjs (whit html engine) and mongoose this is my model

180
What&#39;s the difference between an object directly queried and an object querying one of its children?

What's the difference between an object directly queried and an object querying one of its children?

When I add a class on the following object, everything is fine:

133