FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Using echo to run separate PHP script from HTML
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Using echo to run separate PHP script from HTML [message #179918] Wed, 19 December 2012 07:30 Go to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
I adapted a PHP script that presents a vertical bargraph showing a goal and
current value and percentage. It was from:
http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoa l-thermometer-like-bar-graph-with-php/

My adaption includes a text file for the goal and current value as well as
height and width. This "goalgraph.php" works OK:

<?php
$datafile = "goalgraphdata.txt";
if( !file_exists($datafile) ) {
$df = fopen( $datafile, "w" ) or die( "File Error" );
// cur, goal, height, width
fwrite( $df, "100\r\n50\r\n200\r\n25\r\n" );
fclose( $df );
}

if (isset($_GET['cur']) && isset($_GET['goal']) && isset($_GET['height']) &&
isset($_GET['width']) ) {

header("Content-type: image/png");

//setting the values we'll use in our script pulled in from GET
$height = trim($_GET['height']);
$width = trim($_GET['width']);
$goal = trim($_GET['goal']);
$current = trim($_GET['cur']);

$df = fopen( $datafile, "w" ) or die( "File Error" );
// cur, goal, height, width
fwrite( $df, "$current\r\n$goal\r\n$height\r\n$width\r\n" );
fclose( $df );
}

$df = fopen( $datafile, "r" ) or die( "File Error" );
$current = trim(fgets( $df ));
$goal = trim(fgets( $df ));
$height = trim(fgets( $df ));
$width = trim(fgets( $df ));
fclose( $df );

if ($current > $goal) {
$current = 100;
$percent= "100%!";
} else if ($current == "" || $current == "0") {
$percent = "0%";
} else {
$percent = round(($current/$goal)*100) . "%";
}

$im = @imagecreate($width,$height)
or die("Cannot Initialize new GD image stream");

$bg = ImageColorAllocate($im,200,200,200); // grey
$fg = ImageColorAllocate($im,255,0,0); //red
$text_color = imagecolorallocate($im, 0, 0, 0); //black
$text_color1 = imagecolorallocate($im, 255, 255, 0255); //white


$fill = $height-(($current/$goal)*$height);

imagefilledrectangle($im, 0, $fill, $width, $height, $fg);

$font = 3;
imagestring($im, $font, 2, 0, trim($goal), $text_color);
imagestring($im, 5, 5, $height-$percent*$height/200-8, $percent,
$text_color1);
imagestring($im, $font, 2, $height-$percent*$height/100,
trim($current), $text_color);

imagepng($im);

imagedestroy($im);

?>

I am able to access this script using a simple HTML file if I do it with a
direct <img src="goalgraph.php" />. But when I try to do so using PHP within
the HTML, using echo, it does not work. I tried renaming the file php and
adding the "AddType application/x-httpd-php .htm" to a .htaccess file, but
no joy. Here is what I have:

<html>
<body>
Goalgraph
<hr />
<?php
//echo '<img src="' . "goalgraph.php" . '" />' . "\n";

$datafile = "goalgraphdata.txt";
if( file_exists($datafile) ) {
echo "File Found\n";
$df = fopen( $datafile, "r" ) or die( "File Error" );
$current = trim(fgets( $df ));
$goal = trim(fgets( $df ));
$height = trim(fgets( $df ));
$width = trim(fgets( $df ));
fclose( $df );

echo '<img src="' . htmlspecialchars("goalgraph.php?cur=" .
urlencode($current) . "&goal=" . urlencode($goal) .
"&width=" . urlencode($width) . "&height=" . urlencode($height) ) . '"
alt="Help us achieve our goal"' . " /> \n";
}
else
echo "File not found\n";
?>
<hr />
<img src="goalgraph.php" />
</body>

</html>

It's been a long time since I have done anything with PHP or HTML for that
matter, so I'm quite rusty, and it may be something simple. Any help will be
most kindly appreciated.

Thanks!

Paul
Re: Using echo to run separate PHP script from HTML [message #179919 is a reply to message #179918] Wed, 19 December 2012 10:02 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
On Wed, 19 Dec 2012 07:30:35 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
wrote:

> I am able to access this script using a simple HTML file if I do it with a
> direct <img src="goalgraph.php" />. But when I try to do so using PHP within
> the HTML, using echo, it does not work.

If your html script works, and you want to change it so that php is
generating the html, then run the php that you have and look at the
resulting page source to see how it campares with the html that works.

Richard Yates
Re: Using echo to run separate PHP script from HTML [message #179920 is a reply to message #179918] Wed, 19 December 2012 11:04 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 19/12/12 12:30, P E Schoen wrote:

> I am able to access this script using a simple HTML file if I do it with
> a direct <img src="goalgraph.php" />. But when I try to do so using PHP
> within the HTML, using echo, it does not work. I tried renaming the file
> php and adding the "AddType application/x-httpd-php .htm" to a .htaccess
> file, but no joy. Here is what I have:
>
> <html>
> <body>
> Goalgraph
> <hr />
> <?php
> //echo '<img src="' . "goalgraph.php" . '" />' . "\n";

well that's plain wrong ..you have too much of a buggers muddle of quotes.

printf makes things easier..

try
printf ('<img src="%s" />\n', 'goalgraph.php');

>
> $datafile = "goalgraphdata.txt";
> if( file_exists($datafile) ) {
> echo "File Found\n";
> $df = fopen( $datafile, "r" ) or die( "File Error" );
> $current = trim(fgets( $df ));
> $goal = trim(fgets( $df ));
> $height = trim(fgets( $df ));
> $width = trim(fgets( $df ));
> fclose( $df );
>
> echo '<img src="' . htmlspecialchars("goalgraph.php?cur=" .
> urlencode($current) . "&goal=" . urlencode($goal) .
> "&width=" . urlencode($width) . "&height=" . urlencode($height) ) .
> '" alt="Help us achieve our goal"' . " /> \n";
> }
> else
> echo "File not found\n";
> ?>
> <hr />
> <img src="goalgraph.php" />
> </body>
>
> </html>
>
> It's been a long time since I have done anything with PHP or HTML for
> that matter, so I'm quite rusty, and it may be something simple. Any
> help will be most kindly appreciated.
>
> Thanks!
>
> Paul


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: Using echo to run separate PHP script from HTML [message #179922 is a reply to message #179919] Wed, 19 December 2012 13:13 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
"Richard Yates" wrote in message
news:9ml3d8te6o6c4u2f17hov6o0cuvp2bg8c7(at)4ax(dot)com...

> On Wed, 19 Dec 2012 07:30:35 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
> wrote:

>> I am able to access this script using a simple HTML file if I do it with
>> a
>> direct <img src="goalgraph.php" />. But when I try to do so using PHP
>> within the HTML, using echo, it does not work.

> If your html script works, and you want to change it so that php is
> generating the html, then run the php that you have and look at the
> resulting page source to see how it campares with the html that works.

I was using Notepad to edit the file, so it was in Windows format and not
UNIX. I thought it was possible to save the file in UNIX format with
Notepad, but the only formats other than ANSI are Unicode and UTF-8, neither
of which work properly.

D-oh!

Thanks,

Paul
Re: Using echo to run separate PHP script from HTML [message #179923 is a reply to message #179920] Wed, 19 December 2012 13:23 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
"The Natural Philosopher" wrote in message
news:kasoik$onu$1(at)news(dot)albasani(dot)net...

> On 19/12/12 12:30, P E Schoen wrote:

<html>
<body>
Goalgraph
<hr />
<?php
//echo '<img src="' . "goalgraph.php" . '" />' . "\n";

> well that's plain wrong ..you have too much of a buggers muddle of quotes.

> printf makes things easier..

> try
> printf ('<img src="%s" />\n', 'goalgraph.php');

Actually that mishmash and the other "img src" using variables were
"correct", ugly enough.

The problem was not using UNIX file format.

Thanks,

Paul
Re: Using echo to run separate PHP script from HTML [message #179925 is a reply to message #179922] Wed, 19 December 2012 15:05 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/19/2012 1:13 PM, P E Schoen wrote:
> "Richard Yates" wrote in message
> news:9ml3d8te6o6c4u2f17hov6o0cuvp2bg8c7(at)4ax(dot)com...
>
>> On Wed, 19 Dec 2012 07:30:35 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
>> wrote:
>
>>> I am able to access this script using a simple HTML file if I do it
>>> with a
>>> direct <img src="goalgraph.php" />. But when I try to do so using PHP
>>> within the HTML, using echo, it does not work.
>
>> If your html script works, and you want to change it so that php is
>> generating the html, then run the php that you have and look at the
>> resulting page source to see how it campares with the html that works.
>
> I was using Notepad to edit the file, so it was in Windows format and
> not UNIX. I thought it was possible to save the file in UNIX format with
> Notepad, but the only formats other than ANSI are Unicode and UTF-8,
> neither of which work properly.
>
> D-oh!
>
> Thanks,
>
> Paul

That should not make a difference. ANSI is fine. The FTP transfer
should be text (not binary) mode, and will take care of the line end
differences.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using echo to run separate PHP script from HTML [message #179926 is a reply to message #179922] Wed, 19 December 2012 15:23 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 19-12-2012 19:13, P E Schoen wrote:
> I was using Notepad to edit the file, so it was in Windows format and
> not UNIX. I thought it was possible to save the file in UNIX format with
> Notepad, but the only formats other than ANSI are Unicode and UTF-8,
> neither of which work properly.
>

a free editor like Notepad++ can do this for you
http://notepad-plus-plus.org/
Re: Using echo to run separate PHP script from HTML [message #179928 is a reply to message #179922] Wed, 19 December 2012 15:32 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Wed, 19 Dec 2012 13:13:43 -0500, P E Schoen wrote:

> "Richard Yates" wrote in message
> news:9ml3d8te6o6c4u2f17hov6o0cuvp2bg8c7(at)4ax(dot)com...
>
>> On Wed, 19 Dec 2012 07:30:35 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
>> wrote:
>
>>> I am able to access this script using a simple HTML file if I do it
>>> with a direct <img src="goalgraph.php" />. But when I try to do so
>>> using PHP within the HTML, using echo, it does not work.
>
>> If your html script works, and you want to change it so that php
>> is generating the html, then run the php that you have and look at
>> the resulting page source to see how it campares with the html that
>> works.
>
> I was using Notepad to edit the file, so it was in Windows format and
> not UNIX. I thought it was possible to save the file in UNIX format
> with Notepad, but the only formats other than ANSI are Unicode and
> UTF-8, neither of which work properly.

Time for a new article. "Notepad Considered Harmful: or How I Learned to
Stop Worrying and Love Vi".

--
"... I've seen Sun monitors on fire off the side of the multimedia lab.
I've seen NTU lights glitter in the dark near the Mail Gate.
All these things will be lost in time, like the root partition last
week. Time to die...". -- Peter Gutmann in the scary.devil.monastery
Re: Using echo to run separate PHP script from HTML [message #179929 is a reply to message #179925] Wed, 19 December 2012 16:59 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
"Jerry Stuckle" wrote in message news:kat6lp$vdt$2(at)dont-email(dot)me...

> That should not make a difference. ANSI is fine. The FTP transfer should
> be text (not binary) mode, and will take care of the line end differences.

But I am editing the files on my local machine Apache server, so FTP is not
used. I'm using the evaluation version of perledit, but I think I should try
one of the other editors designed for code, such as notepad++ as suggested
by Luuk.

Now that I can get the PHP to work, I want to add some more functionality to
this. There are some widgets that are designed to handle contributions
through PayPal and other services, but I wanted to make something simple
that my friend can easily add to his forum:
http://dogsnharmony.com/community/

What I have now can be handled by simply adding the line:
<img src="goalgraph.php" alt="Help us get to our goal" />

The graph can be updated by manually uploading a new "goalgraphdata.txt"
file with the amounts on an occasional basis, although it would be nice to
see the graph update when the contribution is made. So I wanted to simulate
this by having text controls where a member can enter an amount, and then
click a "Contribute" button, and have the graph update accordingly. There
could also be another text box showing the total contributions.

But I'm not sure quite how to do this. As it is now set up, I would have to
add the new quantity, as entered in the HTML page, to the URL parameters in
the PHP code. Or I could just write the new values to the text file. But I'm
not sure how to transfer the values in the HTML controls to PHP variables to
be written to the file.

I can't find any PHP functions that would provide a listbox or textbox where
a user can enter data. Of course, I could echo an HTML input of type text,
but I'm not sure if the value is visible to PHP, as it is with JavaScript.
Maybe this requires JSON or an equivalent. Any ideas?

Thanks,

Paul
Re: Using echo to run separate PHP script from HTML [message #179931 is a reply to message #179929] Wed, 19 December 2012 17:50 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/19/2012 4:59 PM, P E Schoen wrote:
> "Jerry Stuckle" wrote in message news:kat6lp$vdt$2(at)dont-email(dot)me...
>
>> That should not make a difference. ANSI is fine. The FTP transfer
>> should be text (not binary) mode, and will take care of the line end
>> differences.
>
> But I am editing the files on my local machine Apache server, so FTP is
> not used. I'm using the evaluation version of perledit, but I think I
> should try one of the other editors designed for code, such as notepad++
> as suggested by Luuk.
>

Then you should be using the same OS for Apache as you are for your
editor, and EOL differences should not be a problem.

> Now that I can get the PHP to work, I want to add some more
> functionality to this. There are some widgets that are designed to
> handle contributions through PayPal and other services, but I wanted to
> make something simple that my friend can easily add to his forum:
> http://dogsnharmony.com/community/
>
> What I have now can be handled by simply adding the line:
> <img src="goalgraph.php" alt="Help us get to our goal" />
>
> The graph can be updated by manually uploading a new "goalgraphdata.txt"
> file with the amounts on an occasional basis, although it would be nice
> to see the graph update when the contribution is made. So I wanted to
> simulate this by having text controls where a member can enter an
> amount, and then click a "Contribute" button, and have the graph update
> accordingly. There could also be another text box showing the total
> contributions.
>
> But I'm not sure quite how to do this. As it is now set up, I would have
> to add the new quantity, as entered in the HTML page, to the URL
> parameters in the PHP code. Or I could just write the new values to the
> text file. But I'm not sure how to transfer the values in the HTML
> controls to PHP variables to be written to the file.
>
> I can't find any PHP functions that would provide a listbox or textbox
> where a user can enter data. Of course, I could echo an HTML input of
> type text, but I'm not sure if the value is visible to PHP, as it is
> with JavaScript. Maybe this requires JSON or an equivalent. Any ideas?
>
> Thanks,
>
> Paul

There are none. PHP runs on the server, not the client. It
communicates with the client via HTTP. You need to send the appropriate
HTML to the client and get the response back when the user submits the form.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using echo to run separate PHP script from HTML [message #179932 is a reply to message #179928] Wed, 19 December 2012 17:51 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/19/2012 3:32 PM, Peter H. Coffin wrote:
> On Wed, 19 Dec 2012 13:13:43 -0500, P E Schoen wrote:
>
>> "Richard Yates" wrote in message
>> news:9ml3d8te6o6c4u2f17hov6o0cuvp2bg8c7(at)4ax(dot)com...
>>
>>> On Wed, 19 Dec 2012 07:30:35 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
>>> wrote:
>>
>>>> I am able to access this script using a simple HTML file if I do it
>>>> with a direct <img src="goalgraph.php" />. But when I try to do so
>>>> using PHP within the HTML, using echo, it does not work.
>>
>>> If your html script works, and you want to change it so that php
>>> is generating the html, then run the php that you have and look at
>>> the resulting page source to see how it campares with the html that
>>> works.
>>
>> I was using Notepad to edit the file, so it was in Windows format and
>> not UNIX. I thought it was possible to save the file in UNIX format
>> with Notepad, but the only formats other than ANSI are Unicode and
>> UTF-8, neither of which work properly.
>
> Time for a new article. "Notepad Considered Harmful: or How I Learned to
> Stop Worrying and Love Vi".
>

LOL, I like notepad for quick and dirty stuff, i.e. testing code
snippits - it's small and fast. But for real work I prefer Crimson
editor on Windows, and vim on Linux.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using echo to run separate PHP script from HTML [message #179934 is a reply to message #179926] Wed, 19 December 2012 18:33 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
"Luuk" wrote in message news:rak9q9-obn(dot)ln1(at)luuk(dot)invalid(dot)lan...

> a free editor like Notepad++ can do this for you
> http://notepad-plus-plus.org/

I just downloaded and installed this. It seems to be very good.

Thanks,

Paul
Re: Using echo to run separate PHP script from HTML [message #179936 is a reply to message #179934] Wed, 19 December 2012 20:24 Go to previous message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
On Wed, 19 Dec 2012 18:33:26 -0500, "P E Schoen" <paul(at)peschoen(dot)com>
wrote:

> "Luuk" wrote in message news:rak9q9-obn(dot)ln1(at)luuk(dot)invalid(dot)lan...
>
>> a free editor like Notepad++ can do this for you
>> http://notepad-plus-plus.org/
>
> I just downloaded and installed this. It seems to be very good.

The only drawback I ran into was that it was a real pain to edit the
color schemes to something useful. Seems like most of the presets were
light text on dark backgrounds. I wondered why there were not preset
color schemes that duplicated the defaults of other, well-used
editors.
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: passing values into javascript
Next Topic: Digest Authentication
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Dec 16 15:51:30 EST 2017

Total time taken to generate the page: 0.00819 seconds