簡體   English   中英

如何使用Javascript AJAX將值傳遞給第二頁

[英]How to pass value to the second page using Javascript AJAX

我的頁面上有一個html選擇

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

選項來自數據庫。 在此之后,我有一個ajax腳本

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

我希望它將html select中的選定值發送到頁面result.php

試試這個:

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

        }
        echo "";

和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>

如果您需要POST的示例,請訪問使用XMLHttpRequest發送POST數據

另一種使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM