简体   繁体   English

提交按钮将不会发送电子邮件或在服务器上启动操作

[英]Submit button won't send email or start an action on the server

I have been working on this contact form for ages now and still haven't been able to send a working e-mail. 我已经使用此联系表已有很长时间了,但仍然无法发送有效的电子邮件。 So instead of wasting my time just guess and checking different solutions I figured I might ask for some help. 因此,不要浪费时间,而只是猜测并检查不同的解决方案,我想我可能会寻求帮助。

The form looks like this: 该表格如下所示:

 <form action="assets/php/contact.php" name="contact" id="contact" role="form" method="post"> <div class="form-group"> <label class="sr-only" for="name">Name</label> <input type="text" id="name" class="form-control" name="name" placeholder="Name*" required="" data-validation-required-message="Please enter your name."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <label class="sr-only" for="email">Email</label> <input type="email" id="email" name="email" class="form-control" placeholder="Your E-mail*" required="" data-validation-required-message="Please enter your email address."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <label class="sr-only" for="category">Video Category</label> <input type="text" id="category" class="form-control" name="category" placeholder="Category*" required="" data-validation-required-message="Please enter the catergory."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <textarea class="form-control" id="message" name="message" rows="7" placeholder="Message*" required data-validation-required-message="Please enter your message."></textarea> <p class="help-block text-danger"></p> </div> <div class="text-center"> <button type="submit" name="submit" class="btn btn-block btn-round btn-dark">Submit</button> </div> </form> 

Any my contact.php file looks like so: 我的任何contact.php文件如下所示:

<?php
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $category = $_POST['category'];
        $from = 'Contact Form'; 
        $to = 'testemail@gmail.com'; 
        $body = "From: $name\n Category: $category\n E-Mail: $email\n Message:\n $message";
    if ($_POST['submit']) {
        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>';
    }
}
?>

Currently whenever I press the submit button on my site, the page does nothing. 目前,每当我按下网站上的“提交”按钮时,该页面就什么也不做。 Before I was getting a 404 error for my PHP file but I seem to have fixed that. 在我的PHP文件出现404错误之前,我似乎已经解决了该问题。

This: 这个:

<button type="submit" name="submit" class="btn btn-block btn-round btn-dark">Submit</button>

Probably should be this: 可能应该是这样的:

<input type="submit" name="submit" value="submit" class="btn btn-block btn-round btn-dark">

<form> tag doesn't need a name attribute plus you should replace <form>标记不需要名称属性,您应该替换

<button type="submit" name="submit" class="btn btn-block btn-round btn-dark">Submit</button>

with this 有了这个

<input type="submit" name="submit" class="btn btn-block btn-round btn-dark">

your php code will throw undefined index error because there is no such $_POST['name'] or $_POST['email'] yet but it won't stop the execution you should modify it into this 您的php代码将引发未定义的索引错误,因为还没有这样的$ _POST ['name']或$ _POST ['email'],但是它不会停止执行,您应该将其修改为

 <?php
        if (isset($_POST['submit'])) {

            $name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];
            $category = $_POST['category'];
            $from = 'Contact Form'; 
            $to = 'testemail@gmail.com'; 
            $body = "From: $name\n Category: $category\n E-Mail: $email\n Message:\n $message";
            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>';
        }
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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