0

I get a value from combobox using javascript and I need it to make a query and fill another combobox from data fron the database.

This is how I get the value:

<select name="state" onchange="getValue(this)">
                <option value="0">Provincia</option>
                <option value="1">San José</option>
                <option value="2">Alajuela</option>
                <option value="3">Cartago</option>
                <option value="4">Heredia</option>
                <option value="5">Guanacaste</option>
                <option value="6">Puntarenas</option>
                <option value="7">Limón</option>
              </select>

              <script>
                function getValue(obj){
                   //Pass value to php
                    alert(obj.value);
                }

I want to put the value within a variable without refreshing nor redirecting to another page

1
  • take a look here Commented Feb 11, 2019 at 20:51

1 Answer 1

1

You need to use AJAX to send your data from JavaScript to PHP. AJAX stands for Asynchronous JavaScript and XML and basically lets you request server side code asynchronously. You can also send data along with your asynchronous request:

let url; //your filepath to your php script
let param; //the value you want to send along
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    //do something with the request
  }
};
xhttp.open("POST", url, true);
xhttp.send(param);
Sign up to request clarification or add additional context in comments.

3 Comments

I cannot redirect to another page or the same page beacuse the form gets closed
Could you try and explain your problem better?
the form I have is like a pop up windows and if I try to reload the page the form gets closed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.