PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999





Introduction to PHP Programming

Simple Web application in PHP

"Hello World!" was great for programs that ran on the command line or in a graphical user interface. But the Hello World script needs to be updated for the web, where the basic unit of interaction is the form.

This is the simplest possible form handling script. It accepts a value from one input, the person's name and then responds by displaying it in the browser window.

hello-web.html

<html>
<body>

  <form action="hello-web.php" method="post">
  <label>Name:</label>
  <input type="text" name="frmName" size="24">
  <input type="submit" value="Submit">
  </form>

</body>
</html>
 

Because PHP accepts the values submitted from the form and automatically creates variables from the form input names, only one line is needed to generate a response. And because PHP assumes that it is working within the context of a web page, it automatically generates a HTTP header, telling the browser to display any text output.

hello-web.php

<html>
<body>

<p>Hello <?php echo($_POST["frmName"]); ?>!</p>

</body>
</html>
 

First, PHP outputs a content type header stating the following output is to be HTML. It then sends the P tag and "Hello" to the browser untouched. Once encountering the PHP start tag, it then executes the code until it reaches the PHP end tag. We value from the input parameter "for free" as PHP automatically creates it and initializes it with the value input by the form. In PHP, a variable will be created for any form input value or URL parameter and environment variable that has a value. At that point, it switches back to HTML mode and outputs the exclamation point and the script terminates.

A slightly more complete example than the hello-web script is presented here. It illustrates the concept all in one package without needing a separate html form. This is the basic structure of many browser based applications.

If the form variable does not exist in the CGI enviorment, we know to display the form asking the user for their name. It illustrates the concept all in one package without needing a separate html form. This is the basic structure of many browser based applications.

<html>
<body>
<?php
  
if(!$_POST["frmName"]){

    
// Switch to HTML mode to display form
?>
  <form action="hello-web.php" method="post">
  <label>Name:</label>
  <input type="text" name="frmName" size="24">
  <input type="submit" value="Submit">
  </form>
<?php
  
}
  else{
?>
<p>Hello <?php echo($_POST["frmName"]); ?>!</p>
<?php
  
}
?>
</body>
</html>
 

At the very end we switch back to PHP mode just to close the else clause. This may be overkill in this small application, but this kind of mode switching part of the php-way. You could use echo or print commands and forget the mode switching. Which to use depends on the situation and your mood. ;)

This script hints at how "finite-state machines" implemented as switch/case statements are at the core of many scripts that handle web based interactions. This one has only two states: invite the user to submit data and post submitted data back to the web. So an IF statement is sufficient.

Pages:  1   2   3   4Next:  Dynamic Content »




Copyright © 2017 PHPHelp.com