0

Let's start to say I'm still new to this php. I'm not going to give up, I will learn it. I have a problem whit my form code, that I have put together. I can't get it to send it, after it have validate all my inputs fields. Can not understand why. Maybe I can get some help? Maybe you can fix it so it works? So I have something to work with.

<?php
// This is your email address
$to = "[email protected]"; 

// Subject title
$subject = 'Testing work now pls';

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= "Reply-To: ".$email."\r\n";

// If press send
if (isset($_POST['send'])) {

    // If not empty
    $errors = array(
                    $errorsname = $errorsemail
                    );

    // Start validation

    // Check if name not empty
    if (empty($_POST["name"])) {
        $errorsname = "You must write their name.";
    } else {
        $name = ($_POST["name"]);
        // Check if only letters in name
        if (!preg_match("/^[a-zA-Z æøå-ÆØÅ]*$/",$name)) {
            $errorsname = "Their name only content letters."; 
        }
        // Check if name to short
        if(strlen($_POST['name']) < 2 )
            {
                $errorsname = 'Their name is too short.'; }
        // Check if name to long name
        if(strlen($_POST['name']) > 30 )
            {
                $errorsname = 'Their name is too long'; }
    }

    // Check email
    if (empty($_POST["email"])) {
        $errorsemail = "You must enter their email."; }
    else {
        $email = ($_POST["email"]);
        // Check if e-mail address syntax is valid
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
            $errorsemail = "Invalid email address."; }
    }

    // Check errors if non continue
    if (empty($errors))
        {

            // Make message ready
            $message = "
    Stupid just work pls! $navn, $email
    ";

            // Then send mail

            mail($to,$subject,$message,$headers);

            // Redirect to sucesse page
            header('Location: mysite.php');
        }
}
?>
<!DOCTYPE HTML> 
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style>
.errors {
    color: #FF0000;
}
</style>
</head>
<body>
<h2>Hmmm....Help?</h2>
<span class="errors"><?php echo $errormessage; ?></span>
<form name="kontakt" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input name="name" type="text" placeholder="Deres navn.." value="<?php echo $name;?>">
<span class="errors"><?php echo $errorsname;?></span>
<br><br>
E-mail: <input name="email" type="text" placeholder="Din e-mail.." value="<?php echo $email;?>">
<span class="errors"><?php echo $errorsemail;?></span>
<br><br>
<br><br> 
<input type="submit" name="send" value="Send"> 
</form>
</body>
</html>
6
  • What errors do you get? Commented Jun 11, 2014 at 19:38
  • 3
    basic debugging: did you check the return value of mail()? if it's FALSE, then you've got a mail configuration error. For anything else, did you check the mail server's logs to see what happened after PHP handed over the email? Commented Jun 11, 2014 at 19:41
  • Please post your server error logs, so that we can see what the problem is. Commented Jun 11, 2014 at 19:42
  • Yes as mentioned above, have you performed basic debugging to see where script execution varies from what you expect to happen? Commented Jun 11, 2014 at 19:43
  • You're explicitly setting a value for $errors, then checking later to see if it's empty() before sending mail. $errors will never be empty, so the mail won't be sent. Commented Jun 11, 2014 at 19:44

2 Answers 2

1

You're setting $errors to a non-empty array before you do the validation, so

if (empty($errors))

will always fail. You should initialize it as:

$errors = array();

Then your validation code should add to it in this way:

if (empty($_POST['name'])) {
    $errors['name'] = "You must write their name.";
}

and

if (empty($_POST['email'])) {
    $errors['email'] = "You must enter their email.";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have a multitude of issues here. Firstly in your HTML you need to check to see if your error variables are set, if so, display them, otherwise you are going to get php errors:

<span class="errors"><?php
            if (isset($errormessage)) {
                echo $errormessage;
            }
            ?></span>
        <form name="kontakt" method="post" action="<?php echo                 htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
            Name: <input name="name" type="text" placeholder="Deres navn.." value="<?php
            if (isset($name)) {
                echo $name;
            }
            ?>">
            <span class="errors"><?php
                if (isset($errorsname)) {
                    echo $errorsname;
                }
            ?></span>
            <br><br>
            E-mail: <input name="email" type="text" placeholder="Din e-mail.." value="<?php echo $email; ?>">
            <span class="errors"><?php if (isset($errorsemail)) {
                    echo $errorsemail;
                }
            ?></span>

Secondly, as states above, your $erros array should be instantiated similar or as Barmar has shown.

and lastly, your $email variable is not instantiated when loading the page, you're going to have to give it a default:

if(isset($_POST['email'])){
$email = $_POST['email'];}
else{
$email = '';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.