Chocolate570 Posted April 24, 2006 Share Posted April 24, 2006 Hello,1. I'm making a script to retrieve the weather from a specific site. To do this, I need a way to access the innerHTML of that site while on my current one, therefore, I decided on an iFrame. However, how on earth do I access the innerHTML of the body of the document of the iframe in a cross browser way?2. How to pass a variable to a PHP script? I was thinking about setting an form value to the variable, submitting the form, and sending the contents through POST to get to the PHP. Will this work?Thank you so much in advance.Choco Link to comment Share on other sites More sharing options...
raimo Posted April 24, 2006 Share Posted April 24, 2006 Hello,1. I'm making a script to retrieve the weather from a specific site. To do this, I need a way to access the innerHTML of that site while on my current one, therefore, I decided on an iFrame. However, how on earth do I access the innerHTML of the body of the document of the iframe in a cross browser way?2. How to pass a variable to a PHP script? I was thinking about setting an form value to the variable, submitting the form, and sending the contents through POST to get to the PHP. Will this work?Thank you so much in advance.Choco<{POST_SNAPBACK}> If You are using NOAA there is FTP anonymous server :)Get weather from there with PHP and FTP.URLs: ftp://weather.noaa.gov/data/observations/metar/stations/ftp://weather.noaa.gov/data/observations/metar/decoded/where XXXX.TXT as EETN.TXT is Estonia TallinnThis one may not help in Your case, but do not use iframe, use div! :)Here is example how You can do file include with Javascript to div or any other element:Live server is needed to operate with this beauty, and You can include only from Your own domain level.function ajaxInclude(file){var request = false; try{ request = new XMLHttpRequest(); //All Gecko's and Opera } catch(error){} try{ request = new ActiveXObject('Msxml2.XMLHTTP'); //MSIE } catch(error){} try{ request = new ActiveXObject('Microsoft.XMLHTTP'); //MSIE } catch(error){}request.open('GET', file, false); request.send(null); if (request.status == 200) return request.responseText else return 'Error: ' + request.status;}function Open(obj){var e = document.getElementById('ajax'); if (!e.firstChild){ var baby = document.createTextNode(' '); e.appendChild(baby); } e.firstChild.nodeValue = ajaxInclude(obj.value);obj.selectedIndex = 0;} <div id="ajax"></div>No Microsoft innerHTML used! :blink:This one is used with select element like:<select onchange="Open(this)"><option value="data/default.txt">Select data</option><option value="data/a/file1.txt">1</option><option value="data/a/file2.txt">2</option></select>I will add POST example, at shortly, when get my nicotine-level back to normal Link to comment Share on other sites More sharing options...
raimo Posted April 24, 2006 Share Posted April 24, 2006 Here we go, POST with Javascript! function ajaxRequest(url, post){var request = false;try{request = new XMLHttpRequest(); //All Gecko's and Opera} catch(error){}try{request = new ActiveXObject('Msxml2.XMLHTTP'); //MSIE} catch(error){}try{request = new ActiveXObject('Microsoft.XMLHTTP'); //MSIE} catch(error){}request.onreadystatechange = function(){ if (request.readyState == 4){ document.getElementById('ajax').innerHTML = request.responseText; return true; }}request.open('POST', url, true);request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.setRequestHeader("Content-length", post.length); request.setRequestHeader("Connection", "close"); request.send(post);return true;} There is still innerHTML, You can change it to DOM as my 1st post was done.XHTML: <div id="ajax"><!-- request.responseText will come here after POST --></div><button onclick="ajaxRequest('yourphp.php', 'data1=Hello world!&data2=AJAX rocks!')">Send POST</button> AJAX rocks! Link to comment Share on other sites More sharing options...
Chocolate570 Posted April 24, 2006 Author Share Posted April 24, 2006 See, the problem is, I can't use AJAX with this because I need a site that's not on my domain. So I was hoping to use an iframe.But thank you very much for that post script! :)Choco Link to comment Share on other sites More sharing options...
aspnetguy Posted April 24, 2006 Share Posted April 24, 2006 You can't read the innerHTML of an iframe if the src domain is not your own. YOu will get and Access Denied error.You can use AJAX and let the PHP or ASP "Screen scrap" the src page then return the contents tot he client.That is the only way you can do it. Well, you could just do it all in PHP or ASP and forget the AJAX but that is up to you. :)If you want to read an iframe that is your own domain you can like this: document.frames['iframename'].document.body.innerHTML or document.frames['iframename'].document.getElementsByTagName('body')[0].innerHTML Link to comment Share on other sites More sharing options...
Chocolate570 Posted April 24, 2006 Author Share Posted April 24, 2006 Can I make a PHP page on my server, include the website I need, and then use that in the iframe? Link to comment Share on other sites More sharing options...
aspnetguy Posted April 24, 2006 Share Posted April 24, 2006 take alook at this.http://www.troywolf.com/articles/php/class_http/Basically you have to use php to gather the html from the page first then you can echo it to any part of your page and you don't have to use the iframe. Link to comment Share on other sites More sharing options...
Chocolate570 Posted April 24, 2006 Author Share Posted April 24, 2006 Thank you so much aspnet! After a bit of adding on to that, it worked beautifully. It does exactly what I wanted it to do, and I don't have to do any work passing the stuff to the php from javascript. Thanks again!Choco(By the way, if anyone's interested, the code's beneath this line. Basically, at the end of it, $curTemp has the temperature in whatever city you chose. Just set this to whatever you want.)$h->fetch("http://weatherreports.com/United_States/STATE INITIALS/CITY"); <?php/* Name: Retrieve Weather Script;Creator: Chocolate570;Last Update: Monday April 4th, 2006 at 8:36 AM;Description: Use class_http.php to screenscrape weatherreports.com;Special Thanks To: [url="http://www.troywolf.com/articles/php/class_http/;"]http://www.troywolf.com/articles/php/class_http/;[/url]Special Thanks To: administrador(ensaimada)sphoera(punt)com's note on php.netCont: on how to retrieve part of a string based on its surroundings<start>*///Start Functionfunction get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len);}//Retrieve class filerequire_once('./class_http.php'); //New HTTP object$h = new http(); //Cache Directory$h->dir = "./cache/"; //Retrieve Weather Site$h->fetch("http://weatherreports.com/United_States/STATE INITIALS/CITY");//Save the temperature into a variable$curTemp = get_string_between($h->body,'<div class="currentTemperature">','°F</div>');/*Directions of Use: Do whatever you want with $curTemp.Cont: It contains something like "60". That's the tempCont: In farenhight. Have fun.</start>*/?> Link to comment Share on other sites More sharing options...
aspnetguy Posted April 24, 2006 Share Posted April 24, 2006 glad you got it working Link to comment Share on other sites More sharing options...
maple Posted September 25, 2006 Share Posted September 25, 2006 hi Chocolate570 Can u plz give me a demo how to run ur program , iam very new to this scripting and would like to know about your weather report script.waiting for ur reply , thanks Link to comment Share on other sites More sharing options...
Chocolate570 Posted September 25, 2006 Author Share Posted September 25, 2006 What would you like to do with the script? Just fetch the weather? Then use the following PHP script on a .php page anywhere you want the weather to be. :)In the script, just edit the variables labled "state" and "city" and then the script will go procure the temperature of that place. <?php/*Name: Retrieve Weather Script;Creator: Chocolate570;Last Update: Monday April 4th, 2006 at 8:36 AM;Description: Use class_http.php to screenscrape weatherreports.com;Special Thanks To: http://www.troywolf.com/articles/php/class_http/;Special Thanks To: administrador(ensaimada)sphoera(punt)com's note on php.netCont: on how to retrieve part of a string based on its surroundings<start>*///Start Functionfunction get_string_between($string, $start, $end){$string = " ".$string;$ini = strpos($string,$start);if ($ini == 0) return "";$ini += strlen($start);$len = strpos($string,$end,$ini) - $ini;return substr($string,$ini,$len);}//Retrieve class filerequire_once('./class_http.php');//New HTTP object$h = new http();//Cache Directory$h->dir = "./cache/";//Retrieve Weather Site$state="put state here, initials!"$city="put city here"$h->fetch("http://weatherreports.com/United_States/".$state."/".$city);//Save the temperature into a variable$curTemp = get_string_between($h->body,'<div class="currentTemperature">','°F</div>');/*Directions of Use: Do whatever you want with $curTemp.Cont: It contains something like "60". That's the tempCont: In farenhight. Have fun.</start>*/echo $curTemp;?> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now