0

I have an html select on my page

<pre>
$query = mysql_query("select * from results");
echo "<select id='date' onchange='showdata()' class='form-control'>";
while ($arr = mysql_fetch_array($query)) {
echo "<option value=".$arr['month'].">".$arr['month']." / ".$arr['year']. "</option>" ;   
}
echo "</select>";
</pre>

the options are coming from database. After this I have ajax script

<script>

function showdata() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };

  xhttp.open("GET", "result.php", true);
  xhttp.send();
}

</script>

I want it to send the selected value in the html select to the page result.php

jeroen
  • 88,615
  • 21
  • 107
  • 128
Shair
  • 13
  • 5
  • You have a problem here, all months of different years will result in the same value for the `select`. – jeroen Sep 02 '16 at 07:08

2 Answers2

0

Try with this:

$someVariable = $_GET["someVariable"];
$query = mysql_query("select * from results");
        echo "";
        while ($arr = mysql_fetch_array($query)) {
        echo "".$arr['month']." / ".$arr['year']. "" ;

        }
        echo "";

And JS:

<script>

function showdata() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };

  xhttp.open("GET", "result.php?someVariable=test", true);
  xhttp.send();
}

</script>

If you need example for POST, visit Send POST data using XMLHttpRequest

Community
  • 1
  • 1
Robert
  • 2,783
  • 4
  • 24
  • 45
0

another way of doing the same thing using jquery ajax ...

<select id="item" onchange="send_item(this.value)">
 <?php $someVariable = $_GET["someVariable"];
       $query = mysql_query("select * from results");
       echo "";
     while ($arr = mysql_fetch_array($query)) {?>
<option value="<?php echo your value?>"><?php echo your value?></option>
   <?php }?>
</select>



  <script>
  function send_item(str){
    $.ajax({
        url: '',
        type: "post",
        data: {
           'str':str,
        },
        success: function(data) {

        },
    });
 }
    </script>
Vivek Singh
  • 2,435
  • 1
  • 12
  • 24