2016-09-02 2 views
0

를 사용하여 두 번째 페이지로 값을 전달하는 방법이 옵션은 데이터베이스에서 오는자바 스크립트 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 내 페이지를 선택합니다.

$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", true); 
    xhttp.send(); 
} 

</script> 

내가

+0

당신은 여기에 문제가있다, 다른 년의 모든 달은'select'에 대해 같은 값 발생합니다. – jeroen

답변

0

JQuery와 아약스를 사용하여 같은 일을하고있는 또 다른 방법 ...

<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

이 함께 시도 페이지 result.php로 선택는 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?someVariable=test", true); 
    xhttp.send(); 
} 

</script> 

POST의 예가 필요한 경우 Send POST data using XMLHttpRequest

을 방문하십시오.