-1

How to get the html Select value in php?

i have made this Select in html

<select id="year" onchange="filterYear();">

</select>

Function to fill in the Select:

var n = 22;

function selectOne() {
      var select = document.getElementById('year');
      for (var i=5; i<n; i++) {
         select.options[select.options.length] = new Option(i+1, i);
      } 
    }

but i need to read the current selected item from the select.

i tried this (javascript):

function filterYear() {
    var e = document.getElementById('year');
    var value = e.value;
    var text = e.options[e.selectedIndex].text;
    alert(text);
}   

php:

 $text = $_GET['text'];
 echo $text;

but this did not work any ideas on how to fix this

i hope someone can push me in the right direction

  • 1
    Add a `name` attribute to your ` – brombeer Dec 06 '22 at 08:55
  • How are you sending the data to PHP? Are you using a form? Where does `text` come from? – M. Eriksson Dec 06 '22 at 08:58
  • i do not have a form. do you suggest me making one? –  Dec 06 '22 at 08:59
  • How are you expecting PHP to have access to your JS values if you don't send it to PHP somehow? – M. Eriksson Dec 06 '22 at 09:00
  • @M.Eriksson ```text``` comes from function filterYear() var text –  Dec 06 '22 at 09:00
  • @M.Eriksson first time really trying to understand php. so i expected php to be able to acces my JS values easily –  Dec 06 '22 at 09:01
  • PHP is run on the server, JS is run on the client. You'd need to submit the form from your client to your server or use AJAX for PHP to have access to client-side values – brombeer Dec 06 '22 at 09:05
  • alright,i will change it up. thanks –  Dec 06 '22 at 09:06
  • The PHP manual has a page on [Dealing with Forms](https://www.php.net/manual/en/tutorial.forms.php) that might help – brombeer Dec 06 '22 at 09:08
  • https://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html – HashtagForgotName Dec 06 '22 at 09:10
  • I would recommend you to read through [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). While reading it, have in mind that JS is client side and PHP is server side. – M. Eriksson Dec 06 '22 at 09:16
  • i will do so, thanks @M.Eriksson –  Dec 06 '22 at 09:22
  • As pointed by others to get the data into php and available in a S_GET you would usually need a form. However it can be done without a form by 'reading' the values required in the javascript and using ajax to send the data to the server and process that way. It is however simpler with a form. – ThurstonLevi Dec 06 '22 at 13:53

1 Answers1

0

Without submitting a form and thus reloading the page you can easily send an ajax request that can be processed by the PHP server script. The response from the server is then used in the callback to, usually, perform further DOM manipulation or whatever.

var n = 22;

function selectOne() {
  var select = document.getElementById('year');
  for( var i = 5; i < n; i++ ) {
    select.appendChild( new Option( i + 1, i ) );
  }
}
// build the menu
selectOne();


// bind event listener externally
document.getElementById('year').addEventListener('change',e=>{

  // a callback to process response from PHP script
  let callback=(r)=>{
    alert(r)
  };
     
  // send a GET ajax request and process response
  fetch( 'path/to/php/script.php?text='+e.target.value )
    .then(r=>r.text())
    .then(callback)
    .catch(alert)
})
<select id="year" name='year'>

</select>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46