PHP Tutorial: Form Verification And Simple Validation, A One Page script for PHP form verification.
Having used various means of verifying HTML forms I believe that this method of verifying a form to be the best mostly because it does everything on one page. It presents the form on one page and then when the submit button is pressed, if all the required fields are not filled out then it will present the form again with all the fields intact and in red lettering will point out the fields that are required to be filled out in red. It is not possible to click submit using this method even if the user has turned JavaScript off. While it is possible to use javascript to verify that all fields are filled out, if the user has turned off Javascript this method will not work any way. This is done using PHP and if you are hosted with Astahost then why not go ahead and use it. The only thing this form will not do is repopulate checkboxes since they are usually an indexed array (but don’t have to be , they could be associative) and I have another method for that but that is for later. You can take this script and modify it after seeing how it works and make it perform the way you would like for it to. This method will use both HTML and PHP in the same page so lets get started
CODE
<form action=”form.php” method=”post”>
<table border=”1″ cellpadding=”2″ bgcolor=”azure”><!–Put a nice border areound the table and add soft color–>
<tr>
<td width=”20%” align=”right”>First Name</td>
<td width=”80%”>
<input type=”text” name=”firstname” size=”20″ value=”<?php echo $_POST[firstname] ?>”></td><!–NOTICE the php in the values–>
</tr>
<tr>
<td width=”20%” align=”right”>Last Name</td>
<td width=”80%”>
<input type=”text” name=”lastname” size=”20″ value=”<?php echo $_POST[lastname] ?>”></td><!–will echo users input for repopulation–>
</tr>
<tr>
<td width=”20%” align=”right”>Username</td>
<td width=”80%”>
<input type=”text” name=”username” size=”20″ value=”<?php echo $_POST[username] ?>”> (must be between
6 an 12 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>Password</td>
<td width=”80%”>
<input type=”password” name=”password” size=”20″ value=”<?php echo $_POST[password] ?>”>
(Password must be at least 6 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>E-mail</td>
<td width=”80%”>
<input type=”text” name=”email” size=”40″ value=”<?php echo $_POST[email]; ?>”></td><!–Give more room for long emails–>
</tr>
<tr>
<td width=”20%” align=”right”> </td>
<td width=”80%”>
<input type=”submit” value=”” name=”submit”></td>
</tr>
</table>
<h3>The Username Password and the E-mail fields are required!</h3>
</form>
Using the code above as a model you can modify it to suit your needs for your own site. The regex used to validate I found at the Zend site and is meant to work with .be or .any two or three character extension in a URL I have just finished working on a script that repopulates checkbox data. After looking all over the net for a tutorial or even asking in forums to make it work, I built my own that works like I want, so if there are enough requests I will post it along with explainations and comments. It takes four pages of code to work, but two of them are almost identical it is just that one inserts data and the other updates the database.
CODE :
<?php /* this is guarunteed to work it is possible to use <? (short tags but this style works everywhere).*/
/*Only verify/validate form when it is submitted program name: form.php */
if(isset($_POST[submit])){
$error=”;//initialize $error to blank
if(trim($_POST[username])==” || strlen(trim($_POST[username])) < 6 ||strlen(trim($_POST[username])) >12){
$error.=”Please enter a username between 6 and 12 characters!<br />”; //concatenate the $error Message with a line break
}
if(trim($_POST[password])==” || strlen(trim($_POST[password]))< 6){
$error.=”Your password must be at least 6 characters in length!<br />”;//concatenate more to $error
}
if(trim($_POST[email])==”){
$error.=”An email address is required!<br />”;
}
else {
if(!eregi(”^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $_POST[email])) {
$error=”The e-mail you entered was not in the proper format!”;
}
}
if($error==”){//Hmmmm no text is in $error so do something else, the page has verified and the email was valid
// so uncomment the line below to send the user to your own success page or wherever (swap yourpage.php with your files location).
//echo “script type=\”text/javascript\”>window.location=\yourpage.php\”<script>”;
}
else{
echo “<span style=color:red>$error</span>”;
}
}
?>
That ends the PHP part of the script except for some PHP echos in the HTML section. The first line of code checks to see if the submit button has been pressed, it won’t do anything unless submit has been pressed so then the code goes right to the HTML part below thiese explainations. The next two if conditional statements check that if the user name and password meet the conditions following the if. In the case of the username if it is equal to ” (blank) OR if the length of the string after PHP has trimmed trailing whitespace is < (less than) 6 OR if the length of username is > (greater than) 12 then it will add to the $error variable and display the message in red because of the style embedded in the script. The || means OR in PHP and in the second if condition it works the same as the username only it requires at least 6 letters or letters and numbers or any printable character.
The verification and validation requires a little more explaination becuase it uses a regular expression to test for a valid email address. The first part of the email just checks to be sure that they even enter something and if they did then the else statement checks to see that the email is in a valid format namely a group or alphanumeric or printable charactersthen a “@” symbol then more alphanumeric characters and a “.”followed by alphabetic characters. the “,” seperating the regex then gives the second part with is theemail to check against. If this test fails then the user will see the form redisplayed with the message “The email you entered was not in the proper format!” will show in red.
If there are no errors the last if condition checks if the $error variable is empty or blank and if so then you would remove the comment the(//) in front of the echo “<…. and change the URL to the page you want the user to use. Finally all the concatenated
$errors are printed by the else statement. So now all that is left is to write the HTML form. and it is below and is tacked just below the code above these explainations. NOTE Just copy and paste the first section of code and then copy and paste the HTML below right after the the ?> closing tag.
CODE
<form action=”form.php” method=”post”>
<table border=”1″ cellpadding=”2″ bgcolor=”azure”><!–Put a nice border areound the table and add soft color–>
<tr>
<td width=”20%” align=”right”>First Name</td>
<td width=”80%”>
<input type=”text” name=”firstname” size=”20″ value=”<?php echo $_POST[firstname] ?>”></td><!–NOTICE the php in the values–>
</tr>
<tr>
<td width=”20%” align=”right”>Last Name</td>
<td width=”80%”>
<input type=”text” name=”lastname” size=”20″ value=”<?php echo $_POST[lastname] ?>”></td><!–will echo users input for repopulation–>
</tr>
<tr>
<td width=”20%” align=”right”>Username</td>
<td width=”80%”>
<input type=”text” name=”username” size=”20″ value=”<?php echo $_POST[username] ?>”> (must be between
6 an 12 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>Password</td>
<td width=”80%”>
<input type=”password” name=”password” size=”20″ value=”<?php echo $_POST[password] ?>”>
(Password must be at least 6 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>E-mail</td>
<td width=”80%”>
<input type=”text” name=”email” size=”40″ value=”<?php echo $_POST[email]; ?>”></td><!–Give more room for long emails–>
</tr>
<tr>
<td width=”20%” align=”right”> </td>
<td width=”80%”>
<input type=”submit” value=”” name=”submit”></td>
</tr>
</table>
<h3>The Username Password and the E-mail fields are required!</h3>
</form>
Using the code above as a model you can modify it to suit your needs for your own site. The regex used to validate I found at the Zend site and is meant to work with .be or .any two or three character extension in a URL I have just finished working on a script that repopulates checkbox data. After looking all over the net for a tutorial or even asking in forums to make it work, I built my own that works like I want, so if there are enough requests I will post it along with explainations and comments. It takes four pages of code to work, but two of them are almost identical it is just that one inserts data and the other updates the database.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb
How To Install Photoshop 7 On Linux (fedora) ?
A : What you may need:
CD-Writer, (maybe)
Windows, (needed!)
Adobe Photoshop 7 CD (well, if you didn’t how could you do this?) (needed!)
A blank CD, (maybe)
A Printer, (maybe)
NTFS Access from Linux on Dual Boot machines (maybe)
WINE (needed!)
So first of all we need access to a Windows machine, I’m dual booting, with Windows XP and Fedora Core 3, I’ve also got NTFS access for Linux, so copying the files over wasn’t hard at all and is much quicker than the other methods I will suggest.
First install Photoshop 7 on C:, could be any other drive but we’ve got to change it to match C: in the registry information, so if you’ve already got an installation, like I had residing on H:\Program Files\Adobe\Adobe Photoshop 7 then not to worry as it’s quite simple to make the change.
Now that you’ve installed it, first run it once, just so registry information gets updated then close it. Now we have to go into the registry and export it. So open up the run dialog box by either Start | Run or WinKey + R and type regedit.
We should now be in the Registry Editor program, we must now look for \HKEY_LOCAL_MACHINE\Software\Adobe\Adobe Photoshop 7.0\ it should be highlighted we will now export it, export it to your documents as adobe.reg, now if you aren’t dual booting with NTFS access then still follow these instructions but I’ll tell you when you have to change it.
Right click on abode.reg and edit it, Select All the text (Ctrl + A) then Copy it (Ctrl + C).
Open up a new notepad document and Paste (Ctrl + V) and save as adobe.txt (why did we do this?, well it saves editting the binary, since this is what a .reg file is when exported, and I had to edit it with a hex editor on linux, so I’m saving you time by doing it this way)
Now the changes for those without dual booting or NTFS access, (this is however if you’re using NTFS, if you’re using FAT32 then you should be fine, however my scope does not cover mounting drives, etc, (maybe later) but for now it’s getting PS7 working)
You have two options, print out the adobe.txt file, which means you’ll have to type things in manually or copy it onto CDR in which I’m about to explain.
Let me explain what I’m going to do, I’m going to copy the contents of the /Program Files/Adobe/Adobe Photoshop 7 directory onto CD, other methods could be NTFS/FAT32 access to your drive and then copy it to the right location or burning this onto CDR and transferring it this way and even networking between PCs, I’ll leave it up to you, oh and if you’re not printing the adobe.txt file out, then also add that into your CDR.
So now we should have our adobe.txt file and Directory of Photoshop 7 somewhere, either on CD or on hard drive.
We boot back over into Linux, open up console and browse through our User Directory’s wine location which should be “~/.wine/c_drive/Program Files” here we create a new directory mkdir -p Adobe, we now copy the Photoshop 7 directory into “~/.wine/c_drive/Program Files/Adobe/” we then go back to the root of our wine location ~/.wine and edit the system.reg file, but first we must ammend out text file.
We must remove all instances of “HKEY_LOCAL_MACHINE\”, we must make sure that our \ (slashes) are doubled so we replace \ with \\ on some editors you may need to replace \ with \\\\ because you need to escape it first, we must replace SOFTWARE with Software (not sure if we had to but for consistency sake I did). Now select all and copy it.
So now we edit system.reg, just scroll to the bottom and add our adobe.txt just by pasting it inside the file, don’t worry after wine run’s it’ll sort this file out.
Now all we got to do is run wine, from console type:
/usr/bin/wine /home/<USERNAME>/.wine/drive_c/Program\ Files/Adobe/Photoshop\ 7.0/Photoshop.exe
We can create a shortcut for this but I’ll let you choose on how to do that, Hint: make sure it’s ran in Terminal mode.
That’s It.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb Next Entries »















