简体   繁体   中英

Set SimpleXML path from a searchbox/input

I am retrieving Google Weather XML as such:

<?php
$xml = simplexml_load_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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM