0

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");
?>
5
Contributors
9
Replies
12
Views
4 Years
Discussion Span
Last Post by GliderPilot
1

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 :)

0

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?

0

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.

0

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.

0

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.

Edited by EvolutionFallen

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.