Author Topic: Way advanced ajax question - I think?  (Read 3463 times)

0 Members and 1 Guest are viewing this topic.

Offline aadamson

  • Member
  • *
  • Posts: 14
    • N1IP's Live Weather
Way advanced ajax question - I think?
« on: November 10, 2007, 10:22:25 PM »
Sheesh, look what you guys have turned me into :)

Anyway, curious, if you put some php code, complete with the beginning and ending tags, in the actual ajax update data, will the php execute?  Say for example, you call (for the lack of a better word), a php script in an image tag that is send via an ajax update (I made that up, so I'm not even sure if it makes sense, but it does make the point), will the php execute, like it would on the original load of the image tag?

Alan - the crazy php/ajax programmer :)

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 6229
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Way advanced ajax question - I think?
« Reply #1 on: November 10, 2007, 10:38:12 PM »
Welcome to the 'Dark Side'  :twisted:  :lol:

The short answer is "Yes".  The PHP code encapsulated by AJAX enabled <span></span> tags is executed on the server and the resulting HTML is sent to the browser.  The browser runs the JavaScript for AJAX and replaces the PHP produced HTML with the AJAX produced HTML, and the browser shows the resulting page.

Example from my own homepage (with PHP and AJAX)
Code: [Select]
                <td align="center" valign="middle"><span style="font-size:24px"><span class="ajax" id="ajaxtemp"> <?php echo strip_units($temperature); ?></span></span>&deg;F <span class="ajax" id="ajaxtemparrow"> <?php echo gen_difference($temperaturestrip_units($temperature) -strip_units($tempchangehour), '',
 'Warmer %s&deg;F than last hour.',
 'Colder %s&deg;F than last hour.');?>
</span><br/><br/>
<?php  echo $heatcolourword?><br/><br/>
Feels like: <span class="ajax" id="ajaxfeelslike"><?php echo $feelslike?></span>&deg;F<br/><br/>
24-hr difference<br /><?php 
 echo gen_difference($temperature$temp24hoursago'&deg;F',
 'Warmer %s&deg;F than yesterday at this time.',
 'Colder %s&deg;F than yesterday at this time.'); ?>
</td>

results in the browser seeing (via a view source)
Code: [Select]
                <td align="center" valign="middle"><span style="font-size:24px"><span class="ajax" id="ajaxtemp"> 58.7</span></span>&deg;F <span class="ajax" id="ajaxtemparrow"> <!-- gendiff now='58.7F' then='58.6'
   textUP='Warmer %s&deg;F than last hour.'
   textDN='Colder %s&deg;F than last hour.'
   diff=0.1 absdiff=0.1
   diffStr='0' absdiffStr='0'
  image='<img src="/images/rising.gif" alt="Warmer 0.1&deg;F than last hour." title="Warmer 0.1&deg;F than last hour." width="7" height="8" style="border: 0; margin: 1px 3px;" />'
  --><img src="/images/rising.gif" alt="Warmer 0.1&deg;F than last hour." title="Warmer 0.1&deg;F than last hour." width="7" height="8" style="border: 0; margin: 1px 3px;" /></span><br/><br/>

Cool<br/><br/>
Feels like: <span class="ajax" id="ajaxfeelslike">59</span>&deg;F<br/><br/>
24-hr difference<br /><!-- gendiff now='58.7F' then='52.0'
   textUP='Warmer %s&deg;F than yesterday at this time.'
   textDN='Colder %s&deg;F than yesterday at this time.'
   diff=6.7 absdiff=6.7
   diffStr='7' absdiffStr='7'
  image='<img src="/images/rising.gif" alt="Warmer 6.7&deg;F than yesterday at this time." title="Warmer 6.7&deg;F than yesterday at this time." width="7" height="8" style="border: 0; margin: 1px 3px;" />'
  -->+6.7&deg;F<img src="/images/rising.gif" alt="Warmer 6.7&deg;F than yesterday at this time." title="Warmer 6.7&deg;F than yesterday at this time." width="7" height="8" style="border: 0; margin: 1px 3px;" /></td>

Note that the PHP echo/print values were replaced with actual data, and the PHP routine for the arrows has inserted HTML comments (via echo statements) into the HTML.

The JavaScript kicks in, and (with Firefox WebDeveloper add-on View Generated Source), you can see the live HTML for the page with the AJAX markups (note each AJAX field now has a lastobs="" attribute to remember when to 'Green Flash' for changes):
Code: [Select]
                <td align="center" valign="middle"><span style="font-size: 24px;"><span style="" lastobs="58.6" class="ajax" id="ajaxtemp">58.6</span></span>F <span style="" lastobs="&lt;img src=&quot;./ajax-images/falling.gif&quot; alt=&quot;Colder 0.2 than last hour.&quot; title=&quot;Colder 0.2 than last hour.&quot; width=&quot;7&quot; height=&quot;8&quot; style=&quot;border: 0; margin: 1px 3px;&quot; /&gt;" class="ajax" id="ajaxtemparrow"><img src="./ajax-images/falling.gif" alt="Colder 0.2 than last hour." title="Colder 0.2 than last hour." style="border: 0pt none ; margin: 1px 3px;" height="8" width="7"></span><br><br>
Cool<br><br>

Feels like: <span style="" lastobs="59" class="ajax" id="ajaxfeelslike">59</span>F<br><br>
24-hr difference<br><!-- gendiff now='58.7F' then='52.2'
   textUP='Warmer %s&deg;F than yesterday at this time.'
   textDN='Colder %s&deg;F than yesterday at this time.'
   diff=6.5 absdiff=6.5
   diffStr='7' absdiffStr='7'
  image='<img src="/images/rising.gif" alt="Warmer 6.5&deg;F than yesterday at this time." title="Warmer 6.5&deg;F than yesterday at this time." width="7" height="8" style="border: 0; margin: 1px 3px;" />'
  -->+6.5F<img src="/images/rising.gif" alt="Warmer 6.5F than yesterday at this time." title="Warmer 6.5F than yesterday at this time." style="border: 0pt none ; margin: 1px 3px;" height="8" width="7"></td>

To summarize:
1) PHP code is executed on the server and the output (if any) is sent embedded in the HTML page returned to the browser.
2) AJAX JavaScript works with the HTML on the page in the browser only, and dynamically modifies the page in the browser.

Hope this helps...
Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Boltek-PCI/NexStorm, microSferics ToA, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge/hub
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 6229
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Way advanced ajax question - I think?
« Reply #2 on: November 10, 2007, 10:45:35 PM »
And, on rereading your question.. I discovered I'd answered a tangential question, but not the PHP image function.

That too is a "Yes", and for the same reason I'd discussed above.

The <img> tag is executed by the browser, which doesn't care if it's a 'real' image or an image returned by a PHP script.

I use this to AJAX enable the thermometer.php on my site
Code: [Select]
                <td align="center" valign="top"><span class="ajax" id="ajaxthermometer"><?php
$tstr "Currently $temperature, Max: $maxtemp, Min: $mintemp";
?>
<img src="thermometer.php?uom=F" alt="<?php echo $tstr?>"
     title="<?php echo $tstr?>" height="170" width="54" /></span> </td>
which results in the browser seeing
Code: [Select]
                <td align="center" valign="top"><span class="ajax" id="ajaxthermometer"><img src="thermometer.php?uom=F" alt="Currently 58.7F, Max: 66.6F, Min: 44.2F"
     title="Currently 58.7F, Max: 66.6F, Min: 44.2F" height="170" width="54" /></span> </td>
which looks like this as generated source (after AJAX JavaScript executes):
Code: [Select]
<td align="center" valign="top"><span style=""
lastobs="&lt;img src=&quot;./thermometer.php?t=58.6&quot; width=&quot;54&quot; height=&quot;170&quot; alt=&quot;Currently 58.6, Max: 66.6, Min: 44.2&quot; title=&quot;Currently 58.6, Max: 66.6, Min: 44.2&quot; /&gt;"
class="ajax" id="ajaxthermometer"><img src="./thermometer.php?t=58.6" alt="Currently 58.6, Max: 66.6, Min: 44.2" title="Currently 58.6, Max: 66.6, Min: 44.2" height="170" width="54"></span> </td>

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Boltek-PCI/NexStorm, microSferics ToA, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge/hub
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline aadamson

  • Member
  • *
  • Posts: 14
    • N1IP's Live Weather
Re: Way advanced ajax question - I think?
« Reply #3 on: November 10, 2007, 10:51:08 PM »
Thanks Ken, now I'm gonna have to let this settle in a bit... It's worse than indirection (a favorite stumbling block for C programmers), it's like 3 levels deeper than that and my brain isn't quite dusted off that far yet :)...

Hmm, does open some interesting possibilities tho...

Alan

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 6229
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Way advanced ajax question - I think?
« Reply #4 on: November 10, 2007, 11:00:16 PM »
Alan,
Keep in mind that a PHP script has to reside in the filesystem of the webserver, and that all PHP is executed before the page is sent to the users browser.

All JavaScript is executed in the users browser.  The AJAX function can READ a file from the current (domain the webpage came from) website, but not write to it.

It is possible to write a JavaScript that AJAX calls a PHP script with arguments that could control the execution of the PHP, but you need to be careful.. that's the way malware can exploit the browser and/or the webserver, unless written to specifically distrust (and heavily check) URL parameters on the PHP script.

I know of at least two instances of using HTTP to upload weather data (WeatherUnderground rapid fire, VWS WeatherFlash), but the receiving scripts are narrowly focused to try and prevent malware creeping in through that vector.

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Boltek-PCI/NexStorm, microSferics ToA, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge/hub
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline aadamson

  • Member
  • *
  • Posts: 14
    • N1IP's Live Weather
Re: Way advanced ajax question - I think?
« Reply #5 on: November 10, 2007, 11:04:40 PM »
Thanks Ken, yes, I've already played with writing server based files thru image tags and php with javascript in the middle... Thanks for clearing up the "indirection" topic as well, I have to remind myself of that often as it's easy to forget what environment, what executes what... btw, I think that's the new "who's on first" :)...

Thanks again, your a great help..  Bummer, now if only the clientraw.txt file had the last hourly date for humid, dew pt, etc.  I could use ajax instead of php to do the little arrows for those fields :)...

Alan

 

anything