SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with a field validation code

    I would like to validate some fields in a form and Iím using this code:
    PHP Code:
    <?
    if($submitform == "yes") {
    $required_fields explode(","$required_fields);
    $required_fields[0] = "To (Friend's name)";
    $required_fields[1] = "To (Friend's email)";
    $required_fields[2] = "From (Your name)";
    $required_fields[3] = "From (Your email)";
    $required_fields[4] = "Subject";
    $required_fields[5] = "Message";
    }

    for(
    $i 0$i count($required_fields); $i++) {
            if($
    $required_fields[$i] == "") {    
            print 
    "<p class=\"maintext\">The required field <b><font color=\"#FF6600\">$required_fields[$i]</font></b> is empty!</p>\n";
            print 
    "<p><a href=\"javascript:history.back(1)\">Go back!</p>\n";
            exit;
        }
    }
    ?>
    The problem is that I always get this message: The required field To (Friendís name) is empty! This happens even if the field isnít empty. What could the problem be?

  2. #2
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused.
    Why do you use explode() on a string and then redefine the array that it created ?

    Actually, you should check the variables sent by the form. The form won't send "To (Friend's name) = Michael" etc. It's more depending on the name you give to the input field.
    For example if you have <input type="text" name="tofriendname" /> , you should check for the variable $tofriendname.

    For example:
    PHP Code:
    if (empty($tofriendname)) {
       
    // do stuff if the person didn't fill that field
    } else {
       
    // process the value of that field

    I hope this helps

  3. #3
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replaying Shin Ma!

    The variables sent by the form are these:
    to_name
    to_email
    from_name
    from_email
    subject
    message

    I have redefined the array because I don't want the message to look like this: The required field to_name is empty!
    Last edited by Marina; Nov 10, 2001 at 16:10.

  4. #4
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to use the for loop to validate the fields because the code is shorter than it would be if used if and also because I want to learn using variable variables (they seem somewhat confusing to me at this point...).

  5. #5
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  6. #6
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Marina,

    You seem to have an extra '$' in the condition:

    for($i = 0; $i < count($required_fields); $i++) {
    if($$required_fields[$i] == "") { //$$required_fields[$i]??

    Looks like once this condition is true (it would always be true) it prints and exits.. That's why only the first field (friend's name) prints out the error.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •