开发者

Set SimpleXML path from a searchbox/input

I am retrieving Google Weather XML as such:

<?php
$xml = simplexml_load开发者_如何学C_file('http://www.google.com/ig/api?weather=LONDON&hl=en-GB');
?>

Is it possible to set the city from an input? As a search box. So you can search which city you are pulling plus the language. I don't mind you search the language as en-GB, US and such instead of english, french etc because I will be adding a selectbox later on.

For example: http://jsfiddle.net/7WFGz/

Thanks alot


The simplest solution:

Your form from jsfiddle

<form action="weather.php" method="get">
    <input type="search" placeholder="City" name="city">
    <input type="search" placeholder="Language" name="lang">
    <input type="button" value="search" style="width:100px">
</form>

weather.php file

<?php
$default_city = "LONDON";
$default_lang = "en-GB";
$city = (iseet($_GET['city']) AND strlen($_GET['city']) > 0) ? $_GET['city'] : $default_city;
$lang = (iseet($_GET['lang']) AND strlen($_GET['lang']) > 0) ? $_GET['lang'] : $default_lang;
// also you can (must) check input data like a preg_match('/([a-z]{2})|([a-z]{2})-([a-z]{2})/i')
$xml = simplexml_load_file('http://www.google.com/ig/api?weather='.$city.'&hl='.$lang);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜