2017-02-27 6 views
1

ユーザにテキストフィールドに値を入力させたい。その後、テキストフィールドに入力されたものに基づいて、入力された特定の状態の簡単な説明を生成するPHPファイルが要求されます。入力から値を取得し、AJAXが応答を返す方法

ここに私の内容があります。

<html> 
<head> 
<script> 
    var XMLHttpRequestObject = false; 
    if (window.XMLHttpRequest) 
    { 
     XMLHttpRequestObject = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject) 
    { 
     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    function getData(dataSource, divID) 
    { 
     if (XMLHttpRequestObject) 
     { 
      var obj = document.getElementById(divID); 
      obj.innerHTML = ""; 
      XMLHttpRequestObject.open("GET", dataSource); 
      XMLHttpRequestObject.onreadystatechange = 
       function() 
       { 
        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) 
        { 
         obj.innerHTML = XMLHttpRequestObject.responseText; 
        } 
       } 
       XMLHttpRequestObject.send(null); 
     } 
    } 
    </script> 

<form> 
     <p>Enter State:</p> 
     <input type="text" name="State"> 
     <br> 
     <br> 

     <input type="button" value="Submit" onclick="getData('k5a.php?state=2', 'targetDiv')"> 
</form> 

<div id="targetDiv"> 
    <p>The state Info will appear here.</p> 

    </div 

だから、PHPファイルは、私は、出力はユーザーが入力どんな状態での情報になりたい

<?php 
    header("Cache-Control: post-check=1, pre-check=2"); 
    header("Cache-Control: no-cache, must-revalidate"); 
    header("Pragma: no-cache"); 

    $choice = $_GET['state']; 



    switch ($choice) 
    { 
      case 1: 
       echo "California is the most populous state in the United States and the third most extensive by area. Located on the western (Pacific Ocean) coast of the U.S., California is bordered by the other U.S. states of Oregon, Nevada, and Arizona and shares an international border with the Mexican state of Baja California."; 
       break; 

      case 2: 
       echo "Florida is a state located in the southeastern region of the United States. It is bordered to the west by the Gulf of Mexico, to the north by Alabama and Georgia, to the east by the Atlantic Ocean, and to the south by the Straits of Florida and Cuba. Florida is the 22nd most extensive, the 3rd most populous, and the 8th most densely populated of the U.S. states."; 
       break; 

      case 3: 
       echo "New York is the most populous city in the United States. With an estimated 2015 population of 8,550,405 distributed over a land area of about 302.6 square miles. New York City is also the most densely populated major city in the United States. Located at the southern tip of the state of New York, the city is the center of the New York metropolitan area, one of the most populous urban agglomerations in the world."; 
       break; 

    } 
    ?> 

のように見えます。私はスイッチ文/ onclickメソッドを行う方法に立ち往生しています。

<script> 
... 
function sendData(){ 
    getData('k5.php?state='+document.getElementById("state_input").value, 'targetDiv'); 
} 
</script> 

この関数は、入力要素の値を読み取り、あなたのgetData()関数を呼び出します。

+0

あなたは正確に何が動作していないのか記述できますか? – shofstetter

+0

@shofstetter switch文に対応する入力値を取得する方法...つまり、カリフォルニアに入るとカリフォルニアの情報が表示されます – Ryan

+0

'case 1:' '' case ''の代わりにカリフォルニア ': 'などのように続きます – eeetee

答えて

0

あなたが送信ボタンのonclickイベントによってトリガーされます新しいjavascript関数sendData()を追加することができます。

<input type="text" name="State" id="state_input"> 

その後、あなたはボタンがクリックされます提出したときに、新たな機能があることを呼び出す必要があります:

<input type="button" value="Submit" onclick="sendData();"> 
0

私が使用することを好むあなたは、あなたの入力要素にIDを与える必要があり

selectユーザーが有効なオプションを選択できるようにするためのリスト。

<p>Select State:</p> 
<select id="states"> 
    <option value="1">California</option> 
    <option value="2">Florida</option> 
    <option value="3">New York</option> 
</select> 

そして、代わりにハードコードされたクエリ文字列のURLの三番目のパラメータとして選択されたオプションを渡し、他の用途のために柔軟なあなたgetData機能を維持します。一緒にすべてを置く

var select = document.getElementById(selector); 
var state = select.options[select.selectedIndex].value; 

が最後にID

XMLHttpRequestObject.open("GET", dataSource + state); 

であなたのURLのクエリ文字列を追加します:あなたの関数で

<input type="button" value="Submit" onclick="getData('k5a.php?state=', 'targetDiv', 'states')"> 

は、このような選択された値をつかむ

<html> 
<head> 
<script> 
    var XMLHttpRequestObject = false; 
    if (window.XMLHttpRequest) 
    { 
     XMLHttpRequestObject = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject) 
    { 
     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 


    // declare extra selector parameter 
    function getData(dataSource, divID, selector) 
    { 
     if (XMLHttpRequestObject) 
     { 

      // get selected option 
      var select = document.getElementById(selector); 
      var state = select.options[select.selectedIndex].value; 

      var obj = document.getElementById(divID); 
      obj.innerHTML = ""; 

      // append URL query string 
      XMLHttpRequestObject.open("GET", dataSource + state); 
      XMLHttpRequestObject.onreadystatechange = 
       function() 
       { 
        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) 
        { 
         obj.innerHTML = XMLHttpRequestObject.responseText; 
        } 
       } 
       XMLHttpRequestObject.send(null); 
     } 
    } 
</script> 
<body> 
<form> 

    <!-- use a select list --> 
    <p>Select State:</p> 
    <select id="states"> 
     <option value="1">California</option> 
     <option value="2">Florida</option> 
     <option value="3">New York</option> 
    </select> 

     <br> 
     <br> 

    <!-- pass that value into function --> 
    <input type="button" value="Submit" onclick="getData('k5a.php?state=', 'targetDiv', 'states')"> 
</form> 

<div id="targetDiv"> 
    <p>The state Info will appear here.</p> 

</div> 
</body> 
</html> 
関連する問題