Results 1 to 2 of 2

Thread: Form validation problem

  1. #1
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Form validation problem

    I'm using Godaddy's gdform.php form mailer for my forms. The forms work perfectly except the validation. I need the visitors to fill in the fields and then submit the information to my email, but the problem is that everyone can submit even if he doesn't fill in the form. I want all of the fields to be required.

    Here is my form script :


    <form action="/gdform.php" method="post">
    <table align="center">

    <input type="hidden" name="subject" value="Form Submission">
    <input type="hidden" name="redirect" value="/Add/thankmes.html">
    <input type="hidden" name="required" value="YourName, Email, description">

    <tr>
    <td><p><font color=WHITE font size=4>Your Name:</p> </font></td> <td align=center height=50><p><input type="text" name="YourName" size="52"/></p></td>
    </tr>

    <tr>
    <td><p><font color=WHITE font size=4>Your E-mail:</p> </font></td> <td height=50><p><input type="text" name="Email" size="52"/></p></td>
    </tr>

    <tr>
    <td><p><font color=WHITE font size=4>Your Message:</p> </font></td> <td><br><p><textarea name="description" cols="40" rows="5">
    </textarea></p></td>
    </tr>

    <tr>
    <td><input type="submit" name="submit" value="Send"/></td>
    </tr>

    </table>
    </form>


    And here is the gdform.php script :

    <?php
    $request_method = $_SERVER["REQUEST_METHOD"] ;
    if($request_method == "GET"){
    $query_vars = $_GET;
    } elseif ($request_method == "POST"){
    $query_vars = $_POST;
    }
    reset($query_vars);
    $t = date("U");

    $file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
    $fp = fopen($file,"w");
    while (list ($key, $val) = each ($query_vars)) {
    fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
    fputs($fp,"$val\n");
    fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
    if ($key == "redirect") { $landing_page = $val;}
    }
    fclose($fp);
    if ($landing_page != ""){
    header("Location: http://".$_SERVER["HTTP_HOST"] ."/$landing_page");
    } else {
    header("Location: http://".$_SERVER["HTTP_HOST"] ."/");
    }


    ?>


    The user can fill in any, all, or none of the fields and submit. Does anyone know how to edit the gdform.php code so it doesn't pass empty fields ?

  2. #2
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    well....im gona go with an obvious one here
    Code:
    if(!isset($_GET['YourName'])) {
        //Yourname field hasnt been set so is there for empty
        header("Location: http://www.yoursite.com/formpage.php?error=1");
    }
    if(!isset($_GET['Email'])) {
        //Email wasnt set so it is empty
        header("Location: http://www.yoursite.com/formpage.php?error=1");
    the same can be applied to all of the different fields that come in

    then in your submit page

    Code:
    if(isset($_GET['error'])) {
       $error = $_GET['error'];
       switch ($error) {
           case 1:
                 echo "you did not enter your name, please re-enter your name to proceed";
                 break;
           case 2:
                  echo "you did not enter your email address, please re-enter your name to proceed";
                  break;
        }
    }
    you could also do the check to see if the email is in the right format, ie it as the '@' and .com/.co.uk etc
    but thats a different story :P
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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
  •