Current location: Hot Scripts Forums » Programming Languages » PHP » Php form validation script


Php form validation script

Reply
  #1 (permalink)  
Old 10-07-04, 08:04 AM
blessedrub blessedrub is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Php form validation script

I have a form going to a database but I need to validate the form first Can anyone please help me with a php script that will validate the form before passing it to the database.
Reply With Quote
  #2 (permalink)  
Old 10-07-04, 12:08 PM
Wuiqed Wuiqed is offline
Wannabe Coder
 
Join Date: Aug 2004
Location: Sverige
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by blessedrub
I have a form going to a database but I need to validate the form first Can anyone please help me with a php script that will validate the form before passing it to the database.
Here's the basic idea of how I do it:

Put this above the form in your form.php (or whatever the form page is called)
PHP Code:



//Check to see if notfilled=1 was passed along the URL
$notfilled $_GET["error"];
if (
$notfilled 1) {
    Echo 
"You did not fill out all the fields of this forum";

And this in the register.php (or whatever it is called)
PHP Code:

//Check to see wich fields was not filled.

//In this case, we'll just check if first name
//or last name was not filled.
//If it wasn't filled we'll send them back to the form
//and tell them it wasn't completely filled out.

if(empty($_POST['f_name']) || empty($_POST['l_name'])) {
    
header("Location: form.php?error=1");

Reply With Quote
  #3 (permalink)  
Old 10-07-04, 05:03 PM
4n7hr4x 4n7hr4x is offline
Banned
 
Join Date: Jun 2004
Posts: 237
Thanks: 0
Thanked 0 Times in 0 Posts
i would just make it all in one php script, like so:

PHP Code:

<?php


if (isset($_POST['submit'])) {
$m NULL;

if (empty(
$_POST['name'])) {
$n FALSE;
$m .= '<p>Please enter a name.</p>';
} else {
$n $_POST['submit']
}

if (
$n) {
$query "QUERY";
$result = @mysql_query ($query);

if (
$result) {
echo 
'<p>Message</p>';
} else {
echo 
' could not do w/e due to system error.</p><p>' mysql_error() . '</p>';
}
if (isset(
$m)) {
echo 
'<font color="red">'$m'</font>';
}
}
}
?>
almost the entire code, just added some so you know how it all works, post anyquestions, and of course you would make the form after, action being:
PHP Code:

<?php echo $_SERVER['PHP_SELF']; ?>

or just the file name itsself.
Reply With Quote
  #4 (permalink)  
Old 10-08-04, 03:43 AM
blessedrub blessedrub is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
I've tried several things but I can't figure out why I'm recieving the following error: document.webform. FirstName.value is null or not an object

This is the following code.
<SCRIPT LANGUAGE="JavaScript">
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function checkFields() {
missinginfo = "";
if (document.webform.FirstName.value == "") {
missinginfo += "\n - FirstName";
}
if ((document.webform.LastName.value == "") ||
(document.form.from.value.indexOf('@') == -1) ||
(document.form.from.value.indexOf('.') == -1)) {
missinginfo += "\n - Last Name";
}
if ((document.webform.Address.value == "") ||
(document.form.website.value.indexOf("http://") == -1) ||
(document.form.website.value.indexOf(".") == -1)) {
missinginfo += "\n - Address";
}
if(document.webform.Email.value == "") {
missinginfo += "\n - Email";
}

if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>
</HEAD>

And the form code is
<form name ="webform" action = "tryHELP.php" method = post onSubmit="return checkFields();">
<p>
<input type="checkbox" name="join" value="checkbox">
Yes I would like to become a Total Victory Partner.<br>
<br>
I would like to contribute the following on a monthly basis<br>
(Please enter the amount in 20, 50, 100 format without the decimal)</p>

<p align="center">
<input type= Text Name= "Array[Amount]" size="10">
</p>
<p align="center"> First Name
<Input Type = Text Name = "Array[FirstName]" Size = 20>
<br>
Last Name
<Input Type = Text Name = "Array[LastName]" Size = 40>
Reply With Quote
  #5 (permalink)  
Old 10-16-04, 07:15 PM
webx2000i webx2000i is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Try to add form id = "webform"
***********************
I think the best way to validate a form is to start on the client side using javascript then on the server side using php since javascript can be disabled by the user.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Competition entry form / random entry draw script with PHP? Tim Smith PHP 0 08-12-04 01:45 AM
Adding to an existing PHP script to drop form info into an Excel ss jerryh0707 PHP 1 07-21-04 03:03 PM
Run PHP Script Within PHP Script Reg PHP 1 06-29-04 09:09 AM
Affiliate script (PHP) whtiebear Job Offers & Assistance 2 12-21-03 12:12 AM
PHP script that uploads and emails the form. ./n00b3 PHP 5 11-24-03 03:16 PM


All times are GMT -5. The time now is 04:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.