2011年10月20日木曜日

PHP(10) フォーム(POST)

PHP(9) フォーム(GET)で作成したサンプルを,POSTメソッドを使ったものに変更する.

1.ユーザーが文字列を入力する画面(post_form.html)を用意.
2.画面に配置された送信ボタンを押すことで,postメソッドにより文字列を送信.
3.送信された文字列をphpファイル(post_xmlParse2.php)で受け取り.
4.受け取った文字列を用いて,マッチする座標を検索~表示.

表示例

LINK

post_form.html
<html>
<head>
 <title>form sample</title>
</head>
<body>
 <form action="post_xmlParse2.php" method="post">
  座標を調べたい文字列 : <input type="text" name="word" />
  <input type="hidden" name="sensor" value="true" />
  <input type="submit" />
 </form>
</body>
</html>

post_xmlParse2.php
<?php
 $word = htmlspecialchars($_POST['word']);
 $sensor = htmlspecialchars($_POST['sensor']);
 $url = "http://maps.google.com/maps/api/geocode/xml";
 $url .= "?address=".$word."&sensor=".$sensor;
 $article = simplexml_load_file($url);

 $lat = $article->result->geometry->location->lat;
 $lng = $article->result->geometry->location->lng;

 echo "<html>\n";
 echo "<head><title>xml test</title></head>\n";
 echo "<body>\n";
 echo $lat;
 echo ", ";
 echo $lng;
 echo "</body>\n";
 echo "</html>\n";
?>

0 件のコメント: