2016-09-02 3 views
0

を使用して、2ページ目に値を渡すためにはどのようなオプションがデータベースから来ているのJavascript AJAX

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

HTMLが私のページで選択しています。私はそれがHTMLで選択した値を送りたい私はアヤックススクリプト

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

を持って、この後のページresult.phpに選択

+0

あなたがここでの問題を持って、異なる年のすべての月は 'SELECT'で同じ値になります。 – jeroen

答えて

0

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

これを試してみてください:

$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の例が必要な場合は、Send POST data using XMLHttpRequest

にアクセスしてください。