Jump to content

PHP Form + JavaScript = Weather Script


shawn10642

Recommended Posts

I need help with one little code in javascript and a very little in php if some could help?

 

Shawn

 

This is the Form

 

<!-- Forecast Quick Link -->					
<form id="quickLinkForm" class="hidden" action="POST">
<fieldset id="quickLink">
<legend>Forecast Quick Link</legend>

<label class="hidden" for="provinces">Province/Territory names</label>
<select onchange="updatecities(this,'en')" size="1" id="provinces">

<option value="--">Select a Region</option>
<option value="AB">Alberta</option>
<option value="BC">British Columbia</option>
<option value="MB">Manitoba</option>
<option value="NB">New Brunswick</option>
<option value="NF">Newfoundland and Labrador</option>
<option value="NT">Northwest Territories</option>
<option value="NS">Nova Scotia</option>
<option value="NU">Nunavut</option>
<option value="ON">Ontario</option>
<option value="PE">Prince Edward Island</option>
<option value="QC">Quebec</option>
<option value="SK">Saskatchewan</option>
<option value="YK">Yukon</option>
</select>   

<label class="hidden" for="cities">City/Town names</label>
<select size="1" id="cities">
<option selected="selected">Select a Location</option>
</select>
<input type="button" value="Go" onclick="jumpToCity(cities, provinces, 'metric', 'e')"/>
</fieldset>
</form>
<script type="text/javascript" src="eventLibrary.js"></script>
<script type="text/javascript" src="forecastQuickLinkArraysEn.js"></script>
<script type="text/javascript" src="forecastQuickLink.js"></script>

 

and this is the section of the javascript

function jumpToCity(cityObj, provinceObj, units, lang) {
var citycode = cityObj.options[cityObj.selectedIndex].value;
var provincecode = provinceObj.options[provinceObj.selectedIndex].value;

if (provincecode == "--") {
	// This means that no province has been selected, so display error message
	if (document.getElementById('errorMessage')==null) { // only need to display it once
		var quicklinkObj = document.getElementById('quickLink');
		var errorLabel = document.createElement('div');
		errorLabel.setAttribute('id', 'errorMessage');
		errorLabel.className = 'error';
		if (lang == "e") { // display message based on language
			errorLabel.appendChild(document.createTextNode("Please select a region"));
		} else {
			errorLabel.appendChild(document.createTextNode("S.V.P. Choisissez une région"));
		}
		quicklinkObj.appendChild(errorLabel);
	}
} else if (lang .selectedIndex == 0 || citycode == 'OLP') {
	location.href = "/forecast/canada/summary_" + lang + ".html?" + citycode; // display Olympic summary
} else {
	if (units == 'metric') {
		location.href = ""

	} else {
		location.href = "/forecast/city_" + lang + ".html?" + citycode.toLowerCase() + "&unit=i"; // imperial city page
	}
}
}

 

1) You pick ex.  Ontario

2) pick Toronto

3) then it will pick a code from an array something like ON-123

4) it then goes to http://www.weatheroffice.gc.ca and gets infomation and posts it on my site

 

This is the script that does that:

 

<table bgcolor="#000000 align="center" width="100%">
  <tr>
    <td bgcolor="#000000" align="center">
    <?php
    include_once("weather.class.php");
    $weather = new getWeather();
    $weather->dbUser="root";
    $weather->dbPass="";
    $weather->dbName="db_name";
    $weather->cityCode="$city";
    echo "<table cellspacing=0 cellpadding=3>";
    if ($weather->fetchData()){
    echo "<tr><td colspan=3 align=center>\n\n".$weather->showWeather()."\n</td></tr>";
    }
    $weather->cityCode="$city";
    $weather->dispFormat=1;
    if ($weather->fetchData()){
    echo "<tr valign=top><td>\n\n".$weather->showWeather()."\n</td>";
    }
    $weather->cityCode="$city";
    $weather->dispFormat=2;
    $weather->longText=true;
    if ($weather->fetchData()){
    echo "<td>\n\n".$weather->showWeather()."\n</td>";
    }
    $weather->cityCode="$city";
    if ($weather->fetchData()){
    echo "<td>\n\n".$weather->showCurrent()."\n</td></tr>\n";
    }
    echo "</table>";
    ?>
    </td>
  </tr>
</table>

Link to comment
Share on other sites

and what i need help on is on the JavaScript this is the original of the 2nd code:

	} else if (cityObj.selectedIndex == 0 || citycode == 'OLP') {
	location.href = "/forecast/canada/summary_" + lang + ".html?" + citycode; // display Olympic summary
} else {
	if (units == 'metric') {
		location.href = "/city/pages/" + citycode.toLowerCase() + "_" + units + "_" + lang + ".html"; // metric city page
	} else {
		location.href = "/forecast/city_" + lang + ".html?" + citycode.toLowerCase() + "&unit=i"; // imperial city page
	}
}
}

and this little part is where my troubles are:

		if (units == 'metric') {
		location.href = "/city/pages/" + citycode.toLowerCase() + "_" + units + "_" + lang + ".html"; // metric city page

 

this is the little the little code

citycode.toLowerCase()

that gets the array code and i need it to do a $_POST to the weather page

 

Hope Someone Can Help :(

Link to comment
Share on other sites

I'm not real sure what your problem is... but I'm going to try:

 

1) You pick ex.  Ontario

2) pick Toronto

3) then it will pick a code from an array something like ON-123

4) it then goes to http://www.weatheroffice.gc.ca and gets infomation and posts it on my site

 

So, this would be more of an AJAX question - since you want to retrieve data from the other website and display it on yours, without refreshing the page. Correct?

 

If so, have ever done any AJAX requests before?

If not, maybe you can help me out by explaining the question/problem again ;)

Link to comment
Share on other sites

  • 1 month later...

Sorry this is my old topic but, i still cant figure this out, if anyone can help that would be great.

 

or maybe if someone can help with this problem but make another array.

 

if i can make another array and make more simple, this script is only for canada so it shouldn't be too hard. if anyone can help please reply

Link to comment
Share on other sites

I'm not real sure what your problem is... but I'm going to try:

 

1) You pick ex.  Ontario

2) pick Toronto

3) then it will pick a code from an array something like ON-123

4) it then goes to http://www.weatheroffice.gc.ca and gets infomation and posts it on my site

 

So, this would be more of an AJAX question - since you want to retrieve data from the other website and display it on yours, without refreshing the page. Correct?

 

If so, have ever done any AJAX requests before?

If not, maybe you can help me out by explaining the question/problem again ;)

 

Read that, and answer the 2 questions :)

Link to comment
Share on other sites

Answer: Nope.

 

Ok what this script is supposed to do it

1) enter weather.php <--main weather page

2) there two drop-down forms the first is province and the second is city. Pick your province (in this case Ontario), and then the second form Auto-Fills. Drop the second form and pick your city (in this case Toronto).

3) When you press the Go/Send button, it then goes though that javascript coding (About Posts) and is fetches the City Code from an array (In this case on-123) which is citycode.toLowerCase() from this clips...

      if (units == 'metric') {
         location.href = "/city/pages/" + citycode.toLowerCase() + "_" + units + "_" + lang + ".html"; // metric city page

 

4) In stead of location.href i need it to $_POST to another script (haven't posted the script in the form), which need that on-123 code?

 

 

any ideas?

Link to comment
Share on other sites

Okay, for the first one - it's kinda what I thought it would be. You can find some info on this:

http://www.phpfreaks.com/forums/index.php/topic,148399.0.html

or

http://forums.devshed.com/javascript-development-115/how-to-fill-a-select-menu-from-an-javascript-array-28438.html

 

I don't have any code handy, since I've never actually done it - but i know the concept. That'll take care of 1 & 2. For 3 + 4, take a look at ajax forms. There are tons of examples out there :)

 

Link to comment
Share on other sites

:-( i've looked at many....Many forms and still cant do it... i like my php more then ajax/js would you know anyother way of doing this and heres the array sample

var citiesEn = new Object();
citiesEn['--']=['Select a Location|'];
citiesEn['AB']=["Airdrie|AB-12","Athabasca|AB-10","Banff|AB-49","Barrhead|AB-2","Beaverlodge|AB-57","Bow Island|AB-43","Bow Valley (Provincial Park)|AB-42","Breton|AB-36","Brooks|AB-58","Calgary|AB-52","Calgary (Olympic Park)|AB-61","Camrose|AB-18","Canmore|AB-3",

 

and it goes on like that. This is killing my brain haha

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.