Results 1 to 3 of 3

Thread: Required Form Field

  1. #1
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Post Required Form Field

    Hi, i amwanting to know ow i can make these three form fileds required. i want 'YourName', 'YourEmail' and 'YourMessage' to be required.

    Below is contact_script.php file
    PHP Code:
    <?php

    // get posted data into local variables
    $EmailFrom "ryan.fitton@hotmail.co.uk";
    $EmailTo "ryan.fitton@hotmail.co.uk";
    $Subject "Email From Personal Website";
    $YourName Trim(stripslashes($_POST['YourName'])); 
    $YourEmail Trim(stripslashes($_POST['YourEmail'])); 
    $YourMessage Trim(stripslashes($_POST['YourMessage'])); 

    // validation
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
      exit;
    }

    // prepare email body text
    $Body "";
    $Body .= "YourName: ";
    $Body .= $YourName;
    $Body .= "\n";
    $Body .= "YourEmail: ";
    $Body .= $YourEmail;
    $Body .= "\n";
    $Body .= "YourMessage: ";
    $Body .= $YourMessage;
    $Body .= "\n";

    // send email 
    $success mail($EmailTo$Subject$Body"From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";

    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
    }

    ?>
    Below is the form part of contact.php (dont know if you will need it )
    HTML Code:
    <form method="post" action="contact_script.php">
    
    <p>Your Name:<br />
    <input type="text" name="YourName" class="contact_text_Input" />
    </p>
    
    <p>Your Email:<br />
    
    <input type="text" name="YourEmail" class="contact_text_Input" />
    </p>
    
    <p>Your Message:<br />
    <textarea name="YourMessage" cols="" rows="" class="contact_textarea_Input"></textarea>
    </p>
    
    <p><input type='image' name='submit' src='images/send_button.gif' class='contact_submit_Input' />
    </p>
    </form>

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    This is nice and simple just add this near to the top of your script after you've trimmed the slashes:

    PHP Code:
    if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
    print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php?e=null\">";
    exit;

    Then in contact.php, put this somewhere after your form, or just somewhere where the message can be clearly seen:

    PHP Code:
    if(isset($_GET['e']) && $_GET['e'] == 'null') {
    echo 
    "You didn't fill in all of the required fields.";


    The trim just basically goes to the fields and strips and whitespace out of them, if someone tries to enter " " with a load of spaces in the middle, it will count as an empty string and will take them back to the contact form.

    The || between the conditions acts as an OR, saying - If this is blank, or this is blank, or if this is blank then do this : code here.

    Hope this solves your problem.

    Edit: I don't quite get the validation in your script, was it taken out or something, have you forgotten to post the code for it?

    PHP Code:
    $validationOK=true;
    if (!
    $validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
      exit;

    This will never get executed with the code you've provided since the only thing that happens to $validationOK is that it evaluates to true.
    There's no part in the code where a condition sets it false, is there more of the code somewhere?
    Last edited by Schmoopy; 04-21-2009 at 10:25 PM.

  3. #3
    Join Date
    Feb 2007
    Posts
    145
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks the script works great, i used a contact form generator here: http://www.tele-pro.co.uk/scripts/contact_form/, so i dont know whay the validation code as put in.

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
  •