简体   繁体   中英

Contact Form don't work

When submitting my form i get a 404 page. Something is missing, or wrong. It's a wordpress website. I inserted the email in the options of the wordpress and nothing happen. I saw a lot of stack overflow questions but none helped me. I don't want to use plugins.

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

           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
                <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
                <input type="text" class="form-control" id="human" name="human" placeholder="">
           </div>
    <br/>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-5">
            <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <! Will be used to display an alert to the user>
        </div>
    </div>
</form>

PHP Code:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Contact Form'; 
        $to = 'example@domain.com'; 
        $subject = 'Contact ';

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

Create a .php file in theme (your current theme, if have child theme then in thild theme) then put below code in the file and save it.

    <?php
/*
* Template Name: Contact us
*/
get_header(); 
if (isset($_POST["submit"]))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'Contact Form'; 
    $to = 'example@domain.com'; 
    $subject = 'Contact ';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!$_POST['name']) 
    {
        $errName = 'Please enter your name';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    {
        $errEmail = 'Please enter a valid email address';
    }
    //Check if message has been entered
    if (!$_POST['message']) 
    {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) 
    {
        $errHuman = 'Your anti-spam is incorrect';
    }
    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) 
    {
        if (mail ($to, $subject, $body, $from)) 
        {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } 
        else 
        {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
}
?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <form class="form-horizontal" role="form" method="post" action="#">
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
                <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
                <input type="text" class="form-control" id="human" name="human" placeholder="">
            </div><br/>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-5">
                    <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <!-- Will be used to display an alert to the user -->
                </div>
            </div>
        </form>
    </main>
    <?php get_sidebar( 'content-bottom' ); ?>
</div>
<?php get_footer(); ?>

Now go to wp-admin and create new page (title may be contact page etc.), select page template "Contact us" from page template dropdown (right side of the page content), publish the page and view the page.

Hope it will help.

First of all thank you for your reply's.

The problem here is the php mail(). I hope i can help someone with this thread.

To reach this conclusion these were the steps:

  • Tried everything that i saw in stack overflow with the same problem has me;
  • Install Check Email Plugin ;
  • I did not received any email so i contact my hosting service and they said they will not enable the php mail() to avoid spam.

Solution - When this happen this is the best way to work around.

In action use file location url:

action="`<?php echo get_template_directory()'contact.php';?>`"

Try this.

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