我正在尝试在我的 joomla 网站中实现定制的天气搜索实用程序。脚本 UI 是 Ajax。它不是用 Joomla 编写的,而是用 PHP、Ajax、Java 和 html 编写的。
它是这样工作的,首先我们需要选择一个国家,当它被选中时,我们会得到另一个 ajax 下拉输入,其中包含与该国家相对应的城市名称。It's working fine as standalone but when I try to implement it using Flexi Custom Code, first the country option is working fine but when the country is selected my home page loads in the place of city dropdown input.
这是我在 Flexi Custom Code 中输入的代码,
<html>
<body>
<?php
define('LOADED', TRUE);
include ('weather/index.php'); ?>
</body>
</html>
在我添加的天气搜索的索引页面中,
if( !defined('LOADED') )
die('You cannot access this file directly!');
请帮助我,在此先感谢。
这是 index.php,
<?php
/*
*/
// Get options
include('configs.php');
// Search zipcode
if($_GET['zipcode2'] != NULL){
$zip_code = $_GET['zipcode2'];
} else {
$zip_code = $_GET['zipcode'];
}
if($USE_DATA){
// Include database
include_once('database.php');
} else {
// Include state List
include_once('state_list.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ajax Weather</title>
<script language="javascript">
// Get HTTP Request
var http;
if (!http && typeof XMLHttpRequest != 'undefined') {
try {
http = new XMLHttpRequest();
} catch (e) {
http = false;
}
}
// Actual Ajax call
function doAjax(optionDo)
{
var imageCode = '<br /><div align="center"><img src="pleaseWait.gif"></div>';
// Doing states
if(optionDo == 1)
{
var state = document.getElementById('state').value;
if(state != '')
{
http.open("GET", 'ajax.php?state='+state, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
}
// Doing it by zipcode (via city)
if(optionDo == 2)
{
var zipcode = document.getElementById('zipcode2').value;
if(zipcode != '')
{
document.getElementById('showFinal').innerHTML = imageCode;
http.open("GET", 'ajax.php?zip_code='+zipcode, true);
http.onreadystatechange = handleHttpResponseFinal;
http.send(null);
document.getElementById('zipcode').value = zipcode;
}
}
// Doing it by zipcode
if(optionDo == 3)
{
var zipcode = document.getElementById('zipcode').value;
if(zipcode != '')
{
document.getElementById('showFinal').innerHTML = imageCode;
http.open("GET", 'ajax.php?zip_code='+zipcode, true);
http.onreadystatechange = handleHttpResponseFinal;
http.send(null);
}
}
}
// Response handler for state list
function handleHttpResponse()
{
if (http.readyState == 4)
{
var response = http.responseText;
document.getElementById('showStates').innerHTML = response;
}
}
// Response handler to show final weather information
function handleHttpResponseFinal()
{
if (http.readyState == 4)
{
var response = http.responseText;
document.getElementById('showFinal').innerHTML = response;
}
}
</script>
</head>
<body onLoad="doAjax('3');">
<form name="reload" id="reload" style="display:inline;">
<table width="790" border="1" align="center" cellpadding="3" cellspacing="2" bordercolor="#990000">
<tr>
<td bgcolor="#FFE6E6"><div align="center"><strong>Please select city: </strong></div></td>
</tr>
<tr>
<td><div align="center">
<select name="state" id="state" onChange="doAjax('1');">
<option value=""></option>
<?=$state_list_form;?>
</select>
<div id="showStates"></div>
<br/>
or Enter ZipCode:
<input name="zipcode" type="text" id="zipcode" value="<?php echo $_GET['zipcode'];?>" onBlur="doAjax('3');">
<div id="showFinal"></div>
</div></td>
</tr>
<tr>
<td><div align="right"><a href="" target="_blank"><font size="1"><strong></strong></font></a> </div></td>
</tr>
</table>
</form>
<!--
-->
</body>
</html>
这是ajax.php,
<?php
/*
*/
// Get options
include('configs.php');
if($USE_DATA){
// Include database
include_once('database.php');
} else {
// Include state List
include_once('state_list.php');
}
// Are we showing zipcode?
if($zip_code != NULL){
// Get weather info
$f = fopen('http://weather.yahooapis.com/forecastrss?p='.$state_code,'r');
while($t = fread($f,102465)){ $content .= $t; }
fclose($f);
preg_match('/<img src="(.*)"\/>/Usm',$content,$results); $image = $results[1];
preg_match('/Current Conditions:<\/b><br \/>(.*)<BR \/>/Uism',$content,$results); $cur_conditions = $results[1];
preg_match('/Forecast:<\/b><BR \/>(.*)<br \/>/ism',$content,$results); $forecast = $results[1];
unset($content);
?>
<table width="100%" border="1" cellpadding="3" cellspacing="2" bordercolor="#990000">
<tr bordercolor="#990000">
<td bgcolor="#FFE6E6"><div align="center"><strong>Weather for
<?=$cityname;?>
,
<?=$state_name;?>
<?=$zip_code;?>
: </strong></div></td>
</tr>
<tr>
<td><img src="<?=$image;?>"/><br/>
<br/>
<strong>Current Conditions:</strong><br/>
<?=$cur_conditions;?>
<br/>
<br/>
<strong>Forecast:</strong><br/>
<?=$forecast;?></td>
</tr>
</table>
<?php
return;
}
// Are we showing states
if($_GET['state'] != NULL){
?>
<select name="zipcode2" id="zipcode2" onChange="doAjax('2');">
<option value=""></option>
<?=$city_list_form;?>
</select>
<?php }
?>