简体   繁体   中英

Email form not working (Php noobie)

I just started learning php and my first project was to make a working contact form. I have typed up what I could, but for some reason there appears to be some sort of errors that php is not telling me. I have looked for solutions to the issue on another web forum like phpacademy, and to no avail. I finally came to try out stack overflow, since I have heard great things about this forum. Any help is appreciated, and thanks in advance.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Contact</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/styles.css"/>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't websites if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>


    <?php

    if ($_POST['submit']){


if(!$_POST['firstname']){

$error='<br /> Please Enter your first name';

}

if(!$_POST['lastname']){

$error='<br /> Please Enter your last name';

}

if(!$_POST['email']){

$error='<br /> Please Enter your email address';

}

if(!$_POST['phone']){

$error='<br /> Please Enter your phone number';

}

if(!$_POST['message']){

$error='<br /> Please Enter your message you would like to send';

}

if($_POST['email']!="" and !filter_var($_POST['email'],
FILTER_VALIDATE_EMAIL)){

$error.='<br /> Please enter a valid email address';

}


if($error){
$result='<div class="alert alert-danger"><strong>One or more errors has occurred</strong>'.$error.'</div>';
}

else {
if (mail("casey@caseywoelfle.com", "Potential Client!", "FirstName: " .
$_POST['firstname'] . "

LastName ".$_POST['lastname'] ."

Email: " .$_POST['email'] . "

Phone: ".$_POST['phone']  . "  

Comment: ".$_POST['comment'])) {


$result = '<div class="alert alert-success"><strong>Thank you!</strong>I\'ll be in touch within 2 business days.</div>';

} else {


$result = '<div class="alert alert-danger"> Sorry, there was an error sending your message. Please try again later.</div>';

}

}

}

?>

<nav class="navbar">
    <div class="container-full">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand honey-sb" href="#">CW</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a class="nLink" href="index.html">Home</a></li>
                <li><a class="nLink" href="about.html">About</a></li>
                <li><a class="nLink" href="portfolio.html">Portfolio</a></li>
                <li><a class="nLink" href="contact.php">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row contact-sec">

        <div class="col-md-12">
                <form class="form-horizontal" method="post">
                        <div class="well">
                        <h1 class="text-center header">Contact Me</h1>
                            <h3 class="text-center">
                                Do you have a project you would like me to take a look at or maybe you have an idea?<br/>Either
                                way don’t hesitate to send me an email using the contact form or give me a call.
                            </h3>
                        </div>

                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
                            <div class="col-md-6">
                                <input id="InputFirstName" name="firstname" type="text" placeholder="First Name" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
                            <div class="col-md-6">
                                <input id="InputLastName" name="lastname" type="text" placeholder="Last Name" class="form-control">
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-envelope"></i></span>
                            <div class="col-md-6">
                                <input id="InputEmail" name="email" type="text" placeholder="Email Address" class="form-control">
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-earphone"></i></span>
                            <div class="col-md-6">
                                <input id="InputPhone" name="phone" type="text" placeholder="Phone" class="form-control">
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-pencil"></i></span>
                            <div class="col-md-6">
                                <textarea class="form-control" id="InputMessage" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
                            </div>
                        </div>
                        <div class="well">

                            <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-4 text-center">
                                <button type="submit" id="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
                            </div>
                            </div>

                        </div>
                </form>
    </div>
</div>
</div>




<footer>
    <div class="container-fluid">

        <div class="row" id="f-nav">

            <div class="col-xs-6 col-sm-3 text-center" id="f-contact">
                <h2 class="footerbolder">Contact</h2>

                <img src="images/ph.png" class="img-responsive" alt=""/>

            </div>

            <div class="col-xs-6 col-sm-3 text-center" id="f-about">
                <h2 class="footerbolder">About Us</h2>

                <img src="images/ph.png" class="img-responsive" alt=""/>

            </div>

            <div class="col-xs-6 col-sm-3 text-center" id="f-services">
                <h2 class="footerbolder">Services</h2>

                <img src="images/ph.png" class="img-responsive" alt=""/>

            </div>

            <div class="col-xs-6  col-sm-3 text-center" id="f-follow">
                <h2 class="footerbolder">Follow Me</h2>

                <img src="images/ph.png" class="img-responsive" alt=""/>

            </div>

        </div>

        <div class="row">

            <div class="hidden-xs col-sm-12" id="pp-tos">
                <div class="center-block text-center">
                    <a class="" href="#">Terms of Service</a>
                    <a>|</a>
                    <a class="" href="#">Privacy Policy</a>
                </div>
            </div>
        </div>




    </div>
</footer>





<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/Fittext/jquery.fittext.js"></script>
</body>
</html>

@B-and-P is right, if all your error checking validates, then your error variable is undefined by the time you reach your

if($error)

statement. To fix this, simply declare your error variable at the top to be null like

$error = NULL;

that way when you check for it, it is defined and if there is no errors, your script will send your mail.

There is no action attribute in your form tag. Example:

<form class="form-horizontal" method="post" action="send_form_email.php">

cut the PHP script from your code and put it in a separate file. Save as send_form_email.php

http://askguyson.com/simple-form-to-email-php-contact-form/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM