validation close but no cigar

  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post July 17th, 2004, 2:08 am

woke up this morning and tried the code. the code doesn't work at all. it just goes ahead and sends the email off. is there another javascript i can use, or better still, a php validation code that just returns a message 'you have not filled out.....'

one thing: with php validation, do u have to use the back button to get back to the form? cannot an alert box appear or is that just for javascript validation? i have php code validation but it doesnt work right at all. any advance on this please?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 17th, 2004, 2:08 am

  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post July 17th, 2004, 7:36 am

Code: [ Select ]
$required_fields  = array('name','email','contactaddress','mobile','workhometel','contactpostcode','projectpostcode','designstyle','programtimecompletion','budgetcost','feeconstraint','projectdeadline','whybuild');

for($i=0;$i<count($required_fields);$i++) {
  if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
    $err[]='You must provide a '.$_POST[$required_fields[$i]];
  }
}
if(count($err)>0) {
  //there were errors, re-display the form with bad values highlighted or something like that
$err[]='The email field does not appear to be in the correct format';
  1. $required_fields  = array('name','email','contactaddress','mobile','workhometel','contactpostcode','projectpostcode','designstyle','programtimecompletion','budgetcost','feeconstraint','projectdeadline','whybuild');
  2. for($i=0;$i<count($required_fields);$i++) {
  3.   if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
  4.     $err[]='You must provide a '.$_POST[$required_fields[$i]];
  5.   }
  6. }
  7. if(count($err)>0) {
  8.   //there were errors, re-display the form with bad values highlighted or something like that
  9. $err[]='The email field does not appear to be in the correct format';
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post July 17th, 2004, 10:03 am

PHP Code: [ Select ]
 
    if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
 
        $err[]='You must provide a '.$_POST[$required_fields[$i]];
 
    }
 
 
  1.  
  2.     if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
  3.  
  4.         $err[]='You must provide a '.$_POST[$required_fields[$i]];
  5.  
  6.     }
  7.  
  8.  

Having established, using the if, that there is nothing
in the variable $_POST[$required_fields[$i]],
you then try to echo it? Try altering the code to this:

PHP Code: [ Select ]
$required_fields    = array('name','email','contactaddress','mobile','workhometel','contactpostcode','projectpostcode','designstyle','programtimecompletion','budgetcost','feeconstraint','projectdeadline','whybuild');
 
 
 
$err=array();    //always initiate vars
 
 
 
for($i=0;$i<count($required_fields);$i++) {
 
    if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
 
        $err[]='You must provide a '.$required_fields[$i];     //you want to echo the required fieldname NOT the empty post variable
 
    }
 
}
 
if(count($err)>0) {
 
    //there were errors, re-display the form with bad values highlighted or something like that
 
$err[]='The email field does not appear to be in the correct format';
 
 
  1. $required_fields    = array('name','email','contactaddress','mobile','workhometel','contactpostcode','projectpostcode','designstyle','programtimecompletion','budgetcost','feeconstraint','projectdeadline','whybuild');
  2.  
  3.  
  4.  
  5. $err=array();    //always initiate vars
  6.  
  7.  
  8.  
  9. for($i=0;$i<count($required_fields);$i++) {
  10.  
  11.     if(!isset($_POST[$required_fields[$i]]) || $_POST[$required_fields[$i]]=='') {
  12.  
  13.         $err[]='You must provide a '.$required_fields[$i];     //you want to echo the required fieldname NOT the empty post variable
  14.  
  15.     }
  16.  
  17. }
  18.  
  19. if(count($err)>0) {
  20.  
  21.     //there were errors, re-display the form with bad values highlighted or something like that
  22.  
  23. $err[]='The email field does not appear to be in the correct format';
  24.  
  25.  


I tested it with $_GET[] but it all seems to work
fine for me. Also turn up your error reporting to
full for testing:

PHP Code: [ Select ]
 
error_reporting(2047);    //put this at line 1 of the php doc
 
 
  1.  
  2. error_reporting(2047);    //put this at line 1 of the php doc
  3.  
  4.  
CSS website design tutorials
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post July 17th, 2004, 10:14 am

ok. here goes. i have these files that dont work properly. the validation brings up the errors but then blanks out the whole form so they user has to begin again which is not what i want at all. this is the closest to good validation i have. please can you look at it for me and either correct it or let me know what the problem is.
  • Cafu
  • Student
  • Student
  • No Avatar
  • Joined: Jul 15, 2004
  • Posts: 97
  • Status: Offline

Post July 17th, 2004, 1:10 pm

First of all, you probably shouldn't post code that has your database username/password in it.


It looks like you are displaying error messages and telling the user to hit their back button to go back and correct the errors. You are probably encountering the IE bug described here: http://www.experts-exchange.com/Web/Web ... 35573.html

adding
Code: [ Select ]
header("Cache-Control: private");

should solve the problem.

Personally, I don't think this is the best way to go about validation. It would be better if you returned the error messages with the form as they previously filled it out.

for example, on your form you could do the input elements like this:

Code: [ Select ]
<input type="text" name="name" id="name" value="<?= $frm["name"] ?>" class="textstyle"/>


the first time through, the value for this element will be blank because you haven't created an array called $frm yet.

then, when you receive the form put all the data received into this $frm array like this:


Code: [ Select ]
$frm = $HTTP_POST_VARS;


so, when you return the form to them with error messages, all the elements will be filled out. Its a little more involved with checkboxes, radios and selects, but it works.

Another benefit of using a technique like this is that you can use the same form include for modifying existing data, like a form to update an existing user's data.. You just do your query, put the data in the $frm array and send them the form with all the elements already filled out.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post July 17th, 2004, 7:52 pm

Since theese topics were all about problems validating the same form I have merged them in cronological order. Topics are as follows,

javascript validation not really validating
Started : Thu Jul 15, 2004 5:16 pm
By : buzzby365

Javascript Not working
Started : Fri Jul 16, 2004 9:57 am
By : buzzby365

form actions on self
Started : Fri Jul 16, 2004 1:14 pm
By : buzzby365

i have this validation but it doesn't work
Started : Sat Jul 17, 2004 10:36 am
By : buzzby365

validation close but no cigar
Started : Sat Jul 17, 2004 1:14 pm
By : buzzby365

Buzzby365 Please keep any more problems with validating this form in this thread, there is no need to keep starting new topics about it.
Strong with this one, the sudo is.
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post July 18th, 2004, 6:43 am

PHP Code: [ Select ]
if (empty($_POST['name']) || empty($_POST['email']))
 
{
 
    echo "fill in all the required fields";
 
}
  1. if (empty($_POST['name']) || empty($_POST['email']))
  2.  
  3. {
  4.  
  5.     echo "fill in all the required fields";
  6.  
  7. }


i have this simple php code as the validation but i seem to be gettin errors. the form submits even tho the form hasnt been filled. what comes up is the error message and the confirmation
  • Cafu
  • Student
  • Student
  • No Avatar
  • Joined: Jul 15, 2004
  • Posts: 97
  • Status: Offline

Post July 18th, 2004, 5:52 pm

How about this?

PHP Code: [ Select ]
if (empty($_POST['name']) || empty($_POST['email']))
 
{
 
    echo "fill in all the required fields";
 
    die;
 
}
  1. if (empty($_POST['name']) || empty($_POST['email']))
  2.  
  3. {
  4.  
  5.     echo "fill in all the required fields";
  6.  
  7.     die;
  8.  
  9. }
  • buzzby365
  • Proficient
  • Proficient
  • No Avatar
  • Joined: May 14, 2004
  • Posts: 288
  • Status: Offline

Post July 18th, 2004, 5:57 pm

thats exactly what i am using. i could hace used DIE or EXIT. produced the same thing. i have adpated the validation to suit the form. brill tho

Post Information

  • Total Posts in this topic: 24 posts
  • Users browsing this forum: No registered users and 151 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.