Page 1 of 1

Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 2:09 pm
by laulau
Hi,
I need help in html or php code :oops:
I would like to choose which graphic (image) to display from a scrolling list:
Link to the "non working" :bash: page http://meyenheim.localweatherview.net/php/graphgw.php "EDIT it's ok now"

Here the code i've tried unsuccessfully :|
<form name="graph_s">
Ces graphiques représentent les données des dernières <select name="cy" onchange="graphgw.php" >
<option value="../images/graph1.jpg">24 heures</option>
<option value="../images/graph2.jpg">48 heures</option>

<?php $grname = selected.value ?>
</select>

</form>


<table cellpadding="0" cellspacing="0" id="Graph_menu">
<tr>
<td <img src=<?php echo $grname?> name="graphs" id="graphs" title="GraphWeather" alt="Graphique météo"/></td>
</tr>

<tr>
<td class="td_navigation_bar">:<a href="index.htm">actuelle</a>::<a href="gauges.htm">instruments</a>::<a href="today.htm">jour</a>::<a href="yesterday.htm">hier</a>::<a href="record.htm">records</a>::<a href="trends.htm">graph</a>:</td>
</tr>
</table>
your advices are welcome :)

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 3:20 pm
by nitrx
is the path good to your graphs http://meyenheim.localweatherview.net/images/graph1.jpg and http://meyenheim.localweatherview.net/images/graph2.jpg ? You start the code in your php folder

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 3:27 pm
by laulau
Yes the path is ok.
I've tried something else, the graph is showing but the changeimage is'nt working :(
http://meyenheim.localweatherview.net/php/graphgw1.php

EDIT :bash: i think this code is working, perhaps an update problem

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 3:53 pm
by daj
Laurent,

You have a small, but crucial error in your HTML....

<td <img src=<?php echo $grname?> name="graphs" id="graphs" title="GraphWeather" alt="Graphique météo"/></td>

should be

<td> <img src=<?php echo $grname?> name="graphs" id="graphs" title="GraphWeather" alt="Graphique météo"/></td>

So as your HTML was malformed, javascript had a problem locating the item to change

Also, no need for PHP. I assume you want a default value (24 hours) when the page loads. Remember PHP runs on the server before it gets to the browser so there will be no value. Far better to force a value, and image, then let the user select. At the moment the $grname is returning a blank, so no graph, when the page loads initially

Try..

<form name="graph_s">
Ces graphiques représentent les données des dernières <select name="cy" onchange="graphgw.php" >
<option value="../images/graph1.jpg" selected="selected">24 heures</option>
<option value="../images/graph2.jpg">48 heures</option>
<?php $grname = selected.value ?>
</select>

</form>


<table cellpadding="0" cellspacing="0" id="Graph_menu">
<tr>
<td <img src=<?php echo $grname?"../images/graph1.jpg"> name="graphs" id="graphs" title="GraphWeather" alt="Graphique météo"/></td>
</tr>

<tr>
<td class="td_navigation_bar">:<a href="index.htm">actuelle</a>::<a href="gauges.htm">instruments</a>::<a href="today.htm">jour</a>::<a href="yesterday.htm">hier</a>::<a href="record.htm">records</a>::<a href="trends.htm">graph</a>:</td>
</tr>
</table>

Changes in red, deletions in strikeout

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 4:38 pm
by laulau
Thanks David

Is there a way to use a PHP variable for the image name like i wanted to do in the code i've put in first post ?

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 4:42 pm
by daj
Can you explain why, or what you want to achieve?

The variable name does not exist when it comes down to the browser; only on the server when the server is creating the HTML for the page.

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 5:25 pm
by laulau
I have nothing in particular in "the oven"
I confused the php variables and those used in html pages :oops:
I've a lot to learn :lol:

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 5:54 pm
by daj
laulau wrote:I have nothing in particular in "the oven"
I confused the php variables and those used in html pages :oops:
I've a lot to learn :lol:
That's ok. Let me try to explain a little and see if it helps.

There are many elements to a web page but the basic language of a web page is HTML and this is what is used to describe the page content and layout to your browser on your PC. When you open a web page your browser will expect HTML commands only. For the most part the HTML is sent to you from a web server so when you visit a web page the webserver sends HTML back.

Now, it may be the case that the web server does 'some magic' to produce the HTML, and your PC browser does not need to know any of this. One of the Magic things is PHP.

PHP only runs on a web server and never runs in the browser on the PC. So if a web page has PHP code in it, the webserver must deal with it and remove all the PHP code before sending to your browser (which only wants HTML).

So the job of PHP is to run some code (on the webserver) and produce HTML to be included in the final HTML sent to the web browser.

Simple Example

Code: Select all

<html>
<body>
<p>This is paragraph 1</p>
<?php 
echo "<p>This is paragraph 2</p>";
?>
</body>
</html>
The webserver processes this page and finds PHP between the <?php...?> tags. A special PHP processer then steps in and executes the code, and in this case (echo) prints some text, which is also HTML.

The result sent to the browser for display is

Code: Select all

<html>
<body>
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
</body>
</html>
All the PHP is removed as the web server "ran" it and produced output -- and now only HTML exists for your browser.

So, in you case if you have a PHP variable '$graph', it never gets to the browser as it is removed. It is only useful while the server is building the HTML to send to you.

Javascript was added to the HTML standard to enhance it. Javascript, by contrast, runs only on the Browser on your PC and updates the page you are currently being shown. So in your case, you use Javascript to change the graph image on the page. The whole page is NEVER sent back to the server to be re-loaded, only the small change you made to the <IMG> tag in your browser.

So,
PHP manipulates the page on the server BEFORE the browser gets it.
Javascript works on the page AFTER the browser has it

Does that help? Let me know if something doesn't make sense

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 6:36 pm
by laulau
Thanks
It's clear now ;)

Re: Select a graph(image) with "select .. option" html/php ?

Posted: Sun 16 Jan 2011 6:53 pm
by PaulMy
David,
I wish I were 50 years younger and you were my teacher :clap: