ユーザにテキストフィールドに値を入力させたい。その後、テキストフィールドに入力されたものに基づいて、入力された特定の状態の簡単な説明を生成する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()関数を呼び出します。
あなたは正確に何が動作していないのか記述できますか? – shofstetter
@shofstetter switch文に対応する入力値を取得する方法...つまり、カリフォルニアに入るとカリフォルニアの情報が表示されます – Ryan
'case 1:' '' case ''の代わりにカリフォルニア ': 'などのように続きます – eeetee