javascript - 发送空白电子邮件的 PHP 脚本无法获取发布数据

标签 javascript php html twitter-bootstrap email

index.php 顶部的 PHP 代码。每当我点击提交时,它都会显示已发送的消息,但它会向我的电子邮件地址生成一封空白电子邮件,它不会从帖子中捕获数据。

<?php   
    $emailSubject = 'Customer Has a Question!';
    $webMaster = 'admin@princosoft.com';
    $name = $_POST['iname'];
    $email = $_POST['iemail'];
    $phone=$_POST['iphone'];
    $question = $_POST['imessage'];
    echo $name;
    $body = <<<EOD
    <br><hr><br>
    Name: $name <br>
    Email: $email <br>
    Questions: $question <br>
    EOD;
    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);
    $theResults = <<<EOD
    EOD;
    echo "$theResults";
?>
<div class="col-lg-12">
    <form name="sentMessage" action="/index.php" method="post" id="contactForm" novalidate >
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="text" name="iname" class="form-control" 
                        placeholder="Your Name  *" 
                        id="name"  
                        required data-validation-required-message="Please enter your  name.">
                    <?php  $name = $_POST['iname']; ?>
                    <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                    <input type="email" name="iemail" class="form-control" 
                        placeholder="Your Email *" id="email"  
                        required data-validation-required-message="Please enter your email address.">
                        <?php  $name = $_POST['iemail']; ?>
                    <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                    <input type="tel" name="iphone" 
                        class="form-control" 
                        placeholder="Your Phone *" id="phone"  
                        required data-validation-required-message="Please enter your phone number.">
                        <?php  $name = $_POST['iphone']; ?>
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <textarea class="form-control"name="imessage" placeholder="Your Message *" 
                        id="message"  
                        required data-validation-required-message="Please enter a
                        message.">/textarea>
                    <?php  $name = $_POST['imessage']; ?>
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="clearfix"></div>
                <div class="col-lg-12 text-center">
                    <div id="success"></div>
                    <button type="submit" name="isubmit" class="btn btn-xl">Send Message</button>
                </div>
            </div>
        </form>

这就是html+php的全部代码

最佳答案

好吧,看看吧,这里有很多错误。

  • 首先,我不是 EOD 的忠实粉丝。所以,我将其更改为只引用。
  • 您的文字需要缩进,严重的是,否则您无法阅读。

让我先给你 php,然后是 html。

<?php 
    print_r($_POST);  

    if (isset($_POST['isubmit'])){ // checking if the submit button variable is there from clicking on the button
        // mail variables

        $emailSubject = 'Customer Has a Question!';
        $webMaster = 'admin@princosoft.com';
        $name = $_POST['iname'];
        $email = $_POST['iemail'];
        $phone=$_POST['iphone'];  // please note this variable is not used anymore in the code. You either forgot it in your email, or you dont want anything with this
        $question = $_POST['imessage'];

        echo $name;

        //set up mail body
        $body = "<br><hr><br>";
        $body .= "Name:". $name . "<br>";
        $body .= "Email:". $email ."<br>";
        $body .= "Questions:". $question ."<br>";

        $headers = "From: $email\r\n";
        $headers .= "Content-type: text/html\r\n";
        $headers .= "MIME-Version: 1.0" . "\r\n";

        if (mail($webMaster, $emailSubject, $body, $headers)){
            echo "your mail has been send succesfully";
        }
    } else {
        // this is a test message. Just erase the whole else statement when this all works (for test purposes if the code is read)
        echo "variables aren't set yet"
    }
?>

如您所见,我对您的代码做了一些改动。我确保它检查是否设置了变量。这样,您就可以触发包含所有变量的邮件。现在它会在您每次刷新页面时运行邮件功能。你不会想那样做的。如果它已发送邮件,我也对邮件功能进行了检查,并向其添加了一个额外的 header 。

您应该尝试做的是净化您的输入。现在它不会在数据库中结束,所以这不是什么大问题,但是,如果您确实想将它发送到您自己的数据库以供引用怎么办?添加一个名为 mysqli_real_escape_string() 的函数很简单(如果您的站点上运行着数据库函数,假设您使用 mysqli。如果愿意,请检查一下。

<form name="sentMessage" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactForm" novalidate >
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" name="iname" class="form-control" placeholder="Your Name  *" id="name" required data-validation-required-message="Please enter your  name.">
                    <?php  $name = $_POST['iname']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?>
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="email" name="iemail" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
                    <?php  $name = $_POST['iemail']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?>
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="tel" name="iphone" class="form-control" placeholder="Your Phone *" id="phone"  required data-validation-required-message="Please enter your phone number.">
                    <?php  $name = $_POST['iphone'];// i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item  ?>
                <p class="help-block text-danger"></p>
            </div>
        </div>

        <div class="col-md-6">
            <div class="form-group">
                <textarea class="form-control"name="imessage" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea> <!-- issue here with wrong closing of text area in html -->
                    <?php  $name = $_POST['imessage']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?>
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div id="success"></div>
            <button type="submit" name="isubmit" class="btn btn-xl">Send Message</button>
        </div>
    </div>
</form>

我也看了你的html。这里的关键是缩进。有一些可疑的 php 位置,我没有得到。名字的 echo 。如果它们是跳棋,那很好,在最终产品中删除它们,如果它们在那里替换名称,您需要添加它们不同。我在评论中说明了它是如何解决的。 (不确定它是否需要,所以我将解决方案排除在答案之外,您需要的代码已经在这个答案中,您只需将一些部分放在一起)

编辑

我还需要补充一点(作为交互设计师),要求电话号码并不明智,尤其是在涉及转换时(客户留言的频率)。您希望人们与您联系。在网络上留下您的电话号码是“私有(private)”的事情,如果人们要求提供电话号码,它总是会给人带来怀疑。人们不知道他们的电话号码会怎样,所以他们放弃了。说明信息已被谨慎使用。

我的提示:不要将电话字段设为必填字段。只是电子邮件、姓名和消息。

关于javascript - 发送空白电子邮件的 PHP 脚本无法获取发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30994905/

相关文章:

javascript - 页面刷新时的 Vuex 状态

php - 选择所有元素直到 - XPath

c# - 简单的文本到 HTML 转换

jquery - 按 Enter 阻止用户提交表单

php - CSS无法在Wordpress网站中呈现

javascript - 网站未加载。 jQuery 未定义

javascript - 如果使用Vue.js显示="none",如何从数组中删除图像?

javascript - 如何获取 html 表格单元格内的图像路径

php - 无法在智能手机中获取文件的 tmp_name ($_FILES)

php - 如何拆分组合字符串