Page 1 of 1

Returning from a php file back to original HTML file

Posted: Sat Nov 22, 2008 10:32 pm
by ExpertAlmost
Good morning!

I am using a simple HTLM page to:
1) display a form with an image and a next/previous/current buttons
2) go to a php file that does processing based on the button the user clicked (updates the image)
3) return to the original HTML form to start the whole process over again (I can figure this step out!)

Problem is, how do I get back to the beginning of the original HTML file from php after I do my php processing? I can use GET/POST in HTML files to go to PHP. And I looked into using header() in php to get back to the beginning of my HTML script but I have already sent output to the browser. I can go to PHP but the return journey eludes me.... 8O hehehehehe

My HTLM file code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div>
<img src="Image1.png"/>
<form action="But2.php" method="post">
<input type="submit" name="previous" value="Previous">
<input type="submit" name="next" value="Next">
<input type="submit" name="current" value="Current">
</form>
<form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
</div>
</body>
</html>
My PHP file: (just a simple test...my actual php files are many and do a lot of processing)

Code: Select all

<?php
if ($_REQUEST["next"]) print "doing something for next</b><br/>\n\n";
if ($_REQUEST["previous"]) print "doing something for previous</b><br/>\n\n";
if ($_REQUEST["current"]) print "doing something for current</b><br/>\n\n";
// Want to go back to beginning of original HTLM script at this point
?>
 

Re: Returning from a php file back to original HTML file

Posted: Sat Nov 22, 2008 11:09 pm
by josh
Options
1) Change your script to not output any data ( or put ob_start() at the top of your script )
2) Instead of redirecting, include() the file that is generating the desired output. The equivalent would be _forward() in a MVC style framework

Re: Returning from a php file back to original HTML file

Posted: Sat Nov 22, 2008 11:27 pm
by ExpertAlmost
Thank you JSHpro2 for your reply! :)

I looked up the buffer flush for php and while that seems like a good function to know about, I am not clear how that gets me back to the original HTLM script to run from the beginning again. Or does the flush automatically take you back to line 1 of the html file?

As to include the PHP files, I did that previous but had a number of negative side effects, hence keeping my HTML processing and PHP process separate and using HTML forms to call the PHP...

Does this mean there IS NO function call in PHP to start an HTML script? Or to return to the script at the point of entry?

boggled but better informed... :lol:

Re: Returning from a php file back to original HTML file

Posted: Sun Nov 23, 2008 12:28 am
by josh
output buffering buffers output, a bandaid to prevent output from being sent to the browser so you can send your headers

Re: Returning from a php file back to original HTML file

Posted: Sun Nov 23, 2008 1:27 am
by ExpertAlmost
Thank you again Jshpro2! :) I have spent a few hours reading everything I can on output control and it makes sense. However...perhaps my question is not stated clearly. Let me try again...

My understanding is that the only way to gather user input in php is to use GET/POST in HTML. This can then call a php code page to process the user input. (I do not want to have 5 php files worth of processing in my one HTML file and just using include/require does NOT get me from the bottom of my HTML script back to the top to run again.) However I output the data (ob_start or not) I need to get back to my original HTML page, (or another HTML file is fine) from within my php page to gather data again from the user...then back to the php to process...until the user stops clicking on buttons.

1) Open HTML page in browser with some FORM buttons. User clicks a button.
2) Form button opens a php program which registers which button was clicked.
3) PHP program does some things.
4) A php generated number must be saved for the next iteration of the php program (after going back to HTLM).
4) Go back to step one and repeat as often as user clicks buttons...

Steps 1-3 I can do. Steps 4 and 5 I do not know how to do. What is the best way? In general, how to go back to an HTML script/file from a PHP file? How to save a PHP variable while going between PHP-HTML-PHP iterations?

I am new at this and struggling with it. Thank you so much for your help! :bow:

Re: Returning from a php file back to original HTML file

Posted: Sun Nov 23, 2008 3:15 am
by josh
You can output HTML directly from a PHP file. Either use include() to display the HTML from within PHP or use closeing and opening PHP tags and embed the HTML inline. I'd store the variable in a hidden form field, or in a session / cookie

Re: Returning from a php file back to original HTML file

Posted: Sun Nov 23, 2008 12:45 pm
by Eran
What jshpro is saying, is that your form can submit to the current page which can handle the form submission. That is the most common way to handle it, since you will probably need to have some error handling in case not all values were entered correctly, and it's best to avoid multiple redirects. By keeping the form handling logic on the same page, you can check the form values for errors and display them without redirecting.

Re: Returning from a php file back to original HTML file

Posted: Sun Nov 23, 2008 11:59 pm
by ExpertAlmost
A heartfelt thanks to everyone contributing to this thread! For super green new newbies like myself, the mentoring provided by those of you in the know really makes learning this stuff more efficient, less painful and certainly melts the Antarctic isolation of learning alone.

Good karma all around :D
ExpertAlmost (thanks to you!)