-2

I would like to get the option value from a HTML select tag and pass it into a PHP variable.

I am able to do this in a JavaScript function, however, I would like to use this variable elsewhere in the script.

This is the select tag code:

   <select id="languageselector" onChange='showSelected(this.value)'>
    <option value="">Pick A language</option>
    @foreach($reviewForm_language as $lang)
     <option value="{{$lang->id}}">{{$lang->name}}</option>
    @endforeach
  </select>

This is the JavaScript code to get the selected value;

<script type="text/javascript">
  function showSelected(val)
    {
       document.getElementById('selectedResult').innerHTML =  val;
       return val
    }
</script>

To display the selected value I call and works well as required.

<div id='selectedResult'></div> 

I would like to pass this answer <div id='selectedResult'></div> to a PHP variable in the same page without reloading.

Anyone assist, thank you.

Tik
  • 23
  • 4
  • you should use Ajax to communicate with the server (PHP) and not have your page reload. – jibsteroos Jan 29 '19 at 07:43
  • Where's the rest of your form? If you POST the form you can retrieve the value on the next page with `` – tshimkus Jan 29 '19 at 07:50
  • 1
    Possible duplicate of https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – 04FS Jan 29 '19 at 07:54
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – apokryfos Jan 29 '19 at 08:34

1 Answers1

0

I think you need ajax() for that

javascript:

function updateTab(value){
   $.ajax({
      url  : "<php page>",
      type : "post",
      data : {data : value},
      success : function(){
        alert("Sent!");
     }
  });
}

PHP:

$getValue = $_POST["data"];
// do something here....
Jan Lois
  • 177
  • 9