If you want something you've never had, you must be willing to do something you've never done!

Powered by Blogger.

Php Programming Tutorial "User Registration" 0

Cerita Semalam Aaz | 10:52 PM | ,


Registering a user requires that he fills some information and that the information be valid. So we will setup two pages, one with a form to be filled and another which will receive the posted data and that will run validation code.
The form we have been using until now was submiting to the same page. Now we will submit to another page. From theregister_1.php page we will post to validate_1.php. To make this happen we declare the action attribute in the form tag:
<form method="POST" action="validate_1.php"></form>
We will use the POST method here since a new registration will change the user table. The form page is almost the same that we already saw:
<?
if (!isset($_GET['user_name']))
   $user_name = "";
else
   $user_name = $_GET['user_name'];
?>
<html>
<body>
<h3>Registration Form</h3>
<form method="POST" action="validate_1.php">

<p>User Name:
<input type="text" name="user_name"
value="<?php echo $user_name; ?>"></p>

<input type="submit" value="Submit the Registration Form">

</form>
</body>
</html>
The page that receives the posted data checks if the user_name field is empty. If it is then it shows a red error message otherwise it shows a congratulation message:
<html>
<body>
<?php
// trim takes the beginning and ending spaces
// out from user name
$user_name = trim($_POST['user_name']);
$error = '';
# Check if the user name is empty
if ($user_name == '')
   echo '<p style="color:red;">User name is required</p>';
else
   /*
   Here there would be the table insertion code
   inserting the new user in the database
   */
   echo '<h3>Congratulations ', $user_name,
   ', you have been successfully registered</h3>';
?>
</body>
</html>

Double submit

This two page approach has a very inconvenient problem. If the user reloads the validation page (F5, Ctrl-F5 or Ctrl-R depending on the browser) two things can happen. First thing is that some browsers will show him an undecipherable message (try it now) to which the user will have to answer yes or no. If he answers yes then the page will be rerun making another registration. Second thing is that other browsers will not even show a warning and the page will be rerun without further notice.

0 Responses So Far:

Related Post

 
Back To Top