I am new to PHP and I'd like the following form to require all fields. I am unsure whether or not this is possible since I am so new to this language. Here is the code. Any help would be useful!

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "$name has requested a tutor, here is a transcript of their message: $message";
$headers = "From: $email";
mail("name@email.com, $email", "New Request for Tutor", $body, $headers);
header("Location: confirmation.html");
?>

Recommended Answers

All 9 Replies

Hey, yes!

I like to do it this way though, it's personal preference but:

<?php

    if(!isset($_POST['name']))
    {
        echo 'You have not submitted your name';
        return 0;
    }else{
        $name = $_POST['name'];
    }


    if(!isset($_POST['email']))
    {
        echo 'You have not submitted your email';
        return 0;
    }else{
        $email = $_POST['email'];
    }

    if(!isset($_POST['message']))
    {
        echo 'You have not submitted your message';
        return 0;
     }else{
        $message = $_POST['message'];
     }

     if($name && $email && $message)
     {
         $body = "$name has requested a tutor, here is a transcript of their message: $message";
         $headers = "From: $email";
         mail("name@email.com, $email", "New Request for Tutor", $body, $headers);
         header("Location: confirmation.html");

     }  


?>

Hope this helps :)

Welcome to the forum. There are quite a few examples showing how to validate form fields within the forum already.

Here's a comprehensive reply I posted previously, which may be of some use. You say you're new, so reply back if it's too complicated for the time being, and we can try to break it down further.

http://www.daniweb.com/web-development/php/threads/426470/form-captcha/2#post1838019

So I've tried implementing the code you've indicated phorce. When I test it, it brings up a blank page on my website. What could that be caused by?

Check your error logs, there is an error in your PHP which will cause the page to appear blank. It can be a lot of adifferent things so best to refer to your error log to see what it says.

I think this is your problem:

mail("name@email.com, $email", "New Request for Tutor", $body, $headers);

Should be:

mail("name@email.com", $email, "New Request for Tutor", $body, $headers);

I assume but like @GilderPilot said, you should check the error log and see where the error is.

Check your error logs

As an alternative, you can enable on-page error reporting. You should remove this/comment it out once you're done debugging your page.

ini_set('display_errors',1);
error_reporting(E_ALL);

EDIT: Forgot to add that this won't work if you're getting a fatal error on the page, but you can create a separate script with this code, then after it use include to bring in the problematic script.

mail("name@email.com, $email", "New Request for Tutor", $body, $headers);

This is correct, it's sending the email to more than one address, one is pre-defined and the other is the e-mail supplied by the user.

@GilderPilot - Sorry, I misread it. I'm too tired for this! ha

Sorry, I misread it. I'm too tired for this! ha

haha, we've all made our share of 'oops' posts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.