PHP / HTML表单发布到电子邮件而不发布数据

时间:2015-08-11 12:52:20

标签: php html forms sendmail

我无法获取此HTML表单以将数据发布到PHP。邮件来自ok,并且有标题(即First Name:Last Name:),但提交的实际数据是空白的。它可能有什么问题?

HTML表格数据:

 <div class="col-md-4 col-sm-12">
                <div class="contact-form bottom">
                <a name="contact" class="more scrolly"></a>
                    <h2>We'll call you back!</h2>
                    <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
                        <div class="form-group">
                            <input type="text" name="first" class="form-control" required placeholder="First Name">
                        </div>
                           <div class="form-group">
                            <input type="text" name="last" class="form-control" required placeholder="Last Name">
                        </div>
                           <div class="form-group">
                            <input type="tel" name="phone" class="form-control" required placeholder="Phone Number">
                        </div>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control" required placeholder="Email">
                        </div>
                        <div class="form-group">
                            <select input type="text" name="debt" class="form-control">
                            <option value="Debt_Level">Debt Level</option>
                            <option value="-">-</option>
                            <option value="3000-5000">£3000-£5000</option>
                            <option value="6000-1000">£6000-£10,000</option>
                            <option value="11000+">£11,000+</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <input type="occupancy" name="occupancy" class="form-control" required placeholder="Occupancy">
                        </div>                 
                        <div class="form-group">
                            <input type="submit" name="submit" class="btn btn-submit" value="Submit">
                        </div>
                    </form>
                </div>
            </div>

PHP FORM DATA:

    <?php
// if(!isset($_POST['submit']))
if(!empty($_POST['first']) && !empty($_POST['last']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['purpose']) && !empty($_POST['amount']) && !empty($_POST['mortgage']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}


$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$debt = $_POST['debt'] ;
$occupancy = $_POST['occupancy'] ;

$email_from = 'trustmoney.co.uk';//<== update the email address
$email_subject = "Landing Page App'";
$email_body = "You have received a new application request from: $email \n".
"First Name:” $first “\n".
"Last Name: “$last “\n".
"Telephone: “$phone “\n".
"Email: “$email “\n".
"Debt Amount:”$debt “\n".
"Occupancy: “$occupancy “\n".
"\n".
"\n".
"Sent from trustmoney.co.uk to: “\n". 
$to = "matt.mckracken@trustmoney.co.uk \n";//<== update the email address
$headers = "From: $email_from";
//$headers .= "Reply-To: $email ";
//Send the email!
mail($to,$email_subject,$email_body,$headers);

?>

2 个答案:

答案 0 :(得分:1)

PHP的逻辑并没有多大意义。正如@Epodax指出你正在检查输入值是否为空,然后输出一个错误,说明它们是,但电子邮件代码甚至不在IF函数内。

您输出数据的字符串也不能正确连接。

case NUMBER:
                    String n = value.toString();
                    if (this.formatString != null) {
                        thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
                        try {
                            int intV = Integer.valueOf(thisStr);
                            cv.set(CellTypes.INT, intV);
                        } catch (NumberFormatException ignored) {}
                        if (cv.getType() == null) {
                            try {
                                Double doubleV = Double.valueOf(thisStr);
                                cv.set(CellTypes.DOUBLE, doubleV);
                            } catch (NumberFormatException e) {
                                cv.set(CellTypes.STRING, thisStr);
                            }
                        }
                    }

管理表单验证的更好结构是将其分解为用户并将其返回到具有适当错误的表单。

这样的东西
$email_body = echo "You have received a new application request from:".$email."\n".
"First Name:".$first."\n".
"Last Name: "$last "\n". REST_OF_CODE_HERE

然后计算错误

$_SESSION['POST_VARS'] = $_POST;
$errors = 0;

if(empty($_POST['first'])) {
    $_SESSION['errorFirst'] = "Cannot Be Blank, Contain Numbers or Special Characters";
    $errors++;
}
if(empty($_POST['last'])) {
    $_SESSION['errorLast'] = "Cannot Be Blank, Contain Numbers or Special Characters";
    $errors++;
}

然后,您只需在表单中输出正确的会话变量,并显示相应的错误

if($errors == 0) {

    // Your Submit Email Code

} else {

    require( "YOUR_FORM_HERE.php" );
}

此外,您可以将用户原始值返回到表单,这样他们就不必从头开始。

if(isset($_SESSION['errorFirst'])) { echo "<div class=\"validateError\">" . $_SESSION['errorFirst'] . "</div>"; unset($_SESSION['errorFirst']); };

使用此方法,您可以将表单中的所有错误输出给您的用户。

答案 1 :(得分:0)

您的逻辑实现中存在错误。您应该使用||而不是&amp;&amp;如下

if(empty($_POST['first']) || empty($_POST['last']) || empty($_POST['phone']) || empty($_POST['email']) || empty($_POST['purpose']) || empty($_POST['amount']) || empty($_POST['mortgage']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}