SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,
    I am a beginner of a few weeks to PHP, and I am currently making a very simple guestbook with PHP and MySQL.

    I would like to do a very simple error check on the guestbook. What I would like it to do, is If the form is submitted, it should check if the Name, Homepage, Email and Message fields are complete, and if they are not, it should return a simple: "You have not filled in _FIELDNAME_ !"

    Could someone help me with this?

    THanks in advance

    -AJ O'Connell

  2. #2
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($name=="") {

    echo("You did not fill in your name. Please return and do so. ");

    }

    if ($email=="") {

    echo("You did not fill in your email address. Please return and do so. ");

    }

    if ($website=="") {

    echo("You did not fill in your website address. Please return and do so. ");

    }

    else {

    echo("Thanks for your submittal, $name!");

    }



    A small example for you.


  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A slightly different but just as useful and a little cleaner approach would be.
    <?
    if ($submit) {
    if (!$name) {
    $error[] = "Missing Name";
    }

    if (!$email) {
    $error[] = "Missing Email";
    }
    if ($email) {
    if ( eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", $email, $arr_vars)
    or !eregi ("^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$", $email, $arr_vars)) {
    $error[] = "Bad email address";
    }
    }


    if (!$website) {
    $error[] = "Missing Website";
    }

    if(!$error) {
    //process the form
    }
    else {
    for ($j=0;$j < count($error);$j++) {
    print "<li><b>". $error[$j] ."</b>\n";
    }
    ?>
    <input type="text" name="name" size="20" maxlength="100" value="<?php print $name; ?>"><br>
    <input type="text" name="email" size="20" maxlength="100" value="<?php print $email; ?>"><br>
    <input type="text" name="website" size="20" maxlength="100" value="<?php print $website; ?>"><br>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy, Thanks.

    However, one thing confuses me,
    I don't understand the following part:

    Code:
    if(!$error) { 
    //process the form 
    } 
    else { 
    for ($j=0;$j < count($error);$j++) { 
    print "<li><b>". $error[$j] ."</b>\n"; 
    }
    What does the //process the form bit mean? i.e. what would I insert into here?

    And what does the following function do?
    THanks again.
    -AJ


  5. #5
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    typical, no thanks for james.


    jus kiddin

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJ, I assume that you will be either writing the results to a text file or a dataabse, so where I have
    //process the form

    You would do that there, Basically I check for the missing fields and if theya re missing I store an error message corresponding to each one in an array called $error, so
    this checks to see if the array $error exists, if it doesn't you can process the data however eyou were planning.
    if(!$error) {
    //process the form
    }

    if it does it loops through the $error array and prints the little error messages for each field missed
    else {
    for ($j=0;$j < count($error);$j++) {
    print "<li><b>". $error[$j] ."</b>\n";
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •