HTML form followed by php code
Moderator: General Moderators
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
I'm new to php and struggling. I have a page that starts with html code and a html form, followed by php code to process the form data input by the user. When I load the page in my browser (IE7), it displays fine but falls thru the html code and form, and executes the php code first. Do I need to set up the form in php; will that make it wait for form input before further processing? or? Thanks, Mike
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
Have I read any tutorials? I guess the answer is yes as I have 2 books I'm working with. If php is always executed first then I should think I'll need to code the form in php and not html. Cheers, Mike
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Well, now I AM confused. My books tell me the forms are set up in html coding which is what I've done and the php code follows that. I did this in Dreamweaver and here's my code so far:Code: Select all
<html>
<head>
<title>index.php3</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td rowspan="2" valign="top" bgcolor="#666666">
<img src="" name="image" width="100" height="50"></td>
<td width="100%" bgcolor="#666666"><font color="#CCCCCC">
</font></td>
</tr>
<tr>
<td align="center">
<h1><strong><font size="6" face="Arial, Helvetica, sans-serif">HWN.ORG MAINTENANCE SITE </font></strong></h1></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#666666" valign="top" align="center"> <table width="100" border="0" cellspacing="2" cellpadding="4">
<tr bgcolor="#CCCCCC">
<td> </td>
</tr>
</table></td>
<td width="100%" valign="top"> </td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr align="right" bgcolor="#CCCCCC">
<td>
<div align="center"><strong>For Editing or Resetting to Defaults, Storm Attributes Shown on the Home, Atlantic, and East-Pacific Pages of HWN.ORG </strong></div></td>
</tr>
</table>
<div align="center">
<p> </p>
</div>
<form method="post">
Which page do you want to edit?
<INPUT NAME="pagename" TYPE="RADIO" VALUE="Home">
Home
<INPUT NAME="pagename" TYPE="RADIO" VALUE="Atlantic">
Atlantic
<INPUT NAME="pagename" TYPE="RADIO" VALUE="Eastern Pacific">
Eastern Pacific
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Process">
</form>
<!-- process code to retrieve appropriate web page code based on above radio group choice -->
<?php
/* Requested hwn.org home page so go get it */
$filename = "index.html";
$homepagetext = file_get_contents($filename);
/* I have the file contents now write them to a flat text file */
$filename = "index.txt";
$handle = fopen($filename,"w");
if (fwrite($handle, $homepagetext) == FALSE) {
echo "Unable to write new file...";}
fclose($handle);
/* Reload the text file and process it */
$filename = "index.txt";
$homepagetext = file_get_contents($filename);
/* Search the file for occurrences of the character "~" which delimit a storm's name */
$name_start_position = strpos($homepagetext,"~",16559);
echo $name_start_position;
?>
<?php
if ($_REQUEST["pagename"] == "Atlantic")
echo "Atlantic"
?>
<?php
if ($_REQUEST["pagename"] == "Eastern Pacific")
echo "Eastern Pacific"
?>
</body>
</html>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]please tryand note that the time is always printed, for each request. There is no pause "after" the form.
Your brower sends a http request to the webserver. The webserver lets php handle the script. The complete output is sent to the browser. The Browser interprets that output, in case of html and a html form it prints the form elements - but that has nothing to do with php anymore, itÄs completely client side.
Code: Select all
<html>
<head><title>php test</title></head>
<body>
<pre><?php print_r($_POST); ?></pre>
<?php
if ( isset($_POST['iText']) && strlen(trim($_POST['iText'])) ) {
echo '<p>Text:', htmlentities($_POST['iText']), "</p>\n";
}
?>
<form method="post" action="?">
<div>
<input type="text" name="iText" />
<select name="iSelect"><option>a</option><option>b</option></select>
<input type="checkbox" name="iCheck" />
<input type="submit" />
</div>
</form>
<p style="color:red;"><?php echo date('H:i:s'); ?></p>
</body>
</html>Your brower sends a http request to the webserver. The webserver lets php handle the script. The complete output is sent to the browser. The Browser interprets that output, in case of html and a html form it prints the form elements - but that has nothing to do with php anymore, itÄs completely client side.
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
Sorry, I don't mean to be rude, I guess I need a forum that will help beginners. I'm obviously not up to your level as I have almost no idea of what you're suggesting. I'll look elsewhere and not waste any more of your and my time. Thanks, Mike
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
Well, thank you anyway.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Hey washingtondude,
PHP does run first but you must remember PHP is used to generate HTML. This:Could be written asand the result is exactly the same:So to the browser there is no difference. The browser doesn't see the PHP it doesn't care if it was even used, the browser only ever sees the HTML or CSS or whatever.
I always get a little concerned when people say that something done in HTML should be done in PHP because the fact is the only way you can do anything is with HTML the issue is what PHP you are using to control what HTML the browser sees. Do you follow?
If this makes PHP unsuitable for whatever task you wish to perform (I'm not clear on exactly what that task is) then you may need to consider JavaScript which is the only language that can be used to make changes to a page after it has been displayed.
PHP does run first but you must remember PHP is used to generate HTML. This:
Code: Select all
<head><title>Foo Bar</title></head>Code: Select all
<?php
$title = 'Foo Bar';
?><head><title><?php echo $title; ?></title></head>Code: Select all
<head><title>Foo Bar</title></head>I always get a little concerned when people say that something done in HTML should be done in PHP because the fact is the only way you can do anything is with HTML the issue is what PHP you are using to control what HTML the browser sees. Do you follow?
If this makes PHP unsuitable for whatever task you wish to perform (I'm not clear on exactly what that task is) then you may need to consider JavaScript which is the only language that can be used to make changes to a page after it has been displayed.
Re: HTML form followed by php code
"Please try" was meant as "please copy this example script to a file accessible through your webserver. Then use your browser to request the script and take a look at the output. Take your time to think about what you're seeing."washingtondude wrote:I'm obviously not up to your level as I have almost no idea of what you're suggesting.
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
Volka, et al:
Thanks for the clarification and help. I read your request as rewriting my code to match yours and honestly I couldn't see why I should do that, much less how. What your snippet is showing me is exactly what's happening with mine, i.e., even though the form appears early on in the page (before any php code), there's no way to have the program "hold" until the form is executed and then process the php code.
The isset command I would think, though I haven't looked that up in my books yet, checks to see if the form has been submitted and, if it has, then echoes the contents of the text box, yes? Additionally it shows how a dropdown box works as well as a checkbutton and if either of these are used then reflects the contents of same. The time of day will always be shown because it's hard-coded in php and not form-dependent which also confirms that since it's almost the very last statement in the program (page) it still executes right away.
I think, then, my little scraper/edit program will have to be split up into separate pages as there are 3 different files I need to display the contents from and allow the user to change them and then rewrite the file with the new contents, if any.
By the way, what's the proper way to refer to code? As a page, program, file, or ?
Thanks, Mike
Thanks for the clarification and help. I read your request as rewriting my code to match yours and honestly I couldn't see why I should do that, much less how. What your snippet is showing me is exactly what's happening with mine, i.e., even though the form appears early on in the page (before any php code), there's no way to have the program "hold" until the form is executed and then process the php code.
The isset command I would think, though I haven't looked that up in my books yet, checks to see if the form has been submitted and, if it has, then echoes the contents of the text box, yes? Additionally it shows how a dropdown box works as well as a checkbutton and if either of these are used then reflects the contents of same. The time of day will always be shown because it's hard-coded in php and not form-dependent which also confirms that since it's almost the very last statement in the program (page) it still executes right away.
I think, then, my little scraper/edit program will have to be split up into separate pages as there are 3 different files I need to display the contents from and allow the user to change them and then rewrite the file with the new contents, if any.
By the way, what's the proper way to refer to code? As a page, program, file, or ?
Thanks, Mike
-
washingtondude
- Forum Newbie
- Posts: 7
- Joined: Tue Jun 05, 2007 6:38 pm
HTML form followed by php code
Correction. What I wrote isn't quite correct as I reread yours. In your code there is php code first and then the form. However, I think I'm correct in saying it makes no difference whether the form is early or late in the code, it will work the same way. Correct? Mike
Yes, very good.
For each new request the webserver will start a new instance of php that handles this request. It can read the input that the client sent with this request and everything it prints is sent back to client - only the output. When the php instance is done (script ends, exit() is called, fatal error ....) it is removed. But that doesn't effect the client/browser. It only parses and interprets what it gets from the webserver. It doesn't care wether it was php that created the response or something else. And after it received the data the connection to the webserver is not needed anymore. Until the client sends a new http request you can e.g. shut down the webserver with no effect to the browser. You can still enter text into the textbox, check the checkbox and so on, that's client-side.
For each new request the webserver will start a new instance of php that handles this request. It can read the input that the client sent with this request and everything it prints is sent back to client - only the output. When the php instance is done (script ends, exit() is called, fatal error ....) it is removed. But that doesn't effect the client/browser. It only parses and interprets what it gets from the webserver. It doesn't care wether it was php that created the response or something else. And after it received the data the connection to the webserver is not needed anymore. Until the client sends a new http request you can e.g. shut down the webserver with no effect to the browser. You can still enter text into the textbox, check the checkbox and so on, that's client-side.