首页 > 解决方案 > PHP Onsubmit 发送电子邮件

问题描述

我有一个表格,接待员输入访客信息、名字、电子邮件等,我想使用表格中输入的电子邮件地址向访客发送电子邮件。

我有 php 邮件功能,但它目前只发送给我手动指定的特定发件人。如何使此表单上的提交按钮根据电子邮件字段的内容发送电子邮件?

我想我需要做这样的事情

<?php
$to      = $row['email'];
$subject = 'Welcome ' . $row['first_name'] . ';
$message = 'You have been booked in';
$headers = 'From: noreply@blah.com' . "\r\n" .
    'Reply-To: noreply@blah.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

我不认为 $row[] 是正确的,因为我想从表单中提取,而不是从表单输入的表中提取。

这是表单页面:

//serve POST method, After successful insert, redirect to customers.php page.
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    //Mass Insert Data. Keep "name" attribute in html form same as column name in mysql table.
    $data_to_store = filter_input_array(INPUT_POST);
    //Insert timestamp
    $db = getDbInstance();
    $last_id = $db->insert ('tb_bookings', $data_to_store);

    if($last_id)
    {
        $_SESSION['success'] = "Visitor signed in successfully!";
        header('location: bookings.php');
        exit();
    }  
}

//We are using same form for adding and editing. This is a create form so declare $edit = false.
$edit = false;

require_once('includes/admin-header.php'); 
?>
<div id="page-wrapper">
<div class="row">
     <div class="col-lg-12">
            <h2 class="page-header">Manual Sign-in</h2>
        </div>

</div>
    <form class="form" action="" method="post"  id="visitor_form" enctype="multipart/form-data">
       <?php  include_once('../forms/prebook_form.php'); ?>
    </form>
</div>

这是形式:

<fieldset>
    <div class="form-group">
        <label for="f_name">First Name *</label>
          <input type="text" name="first_name" value="<?php echo $edit ? $tb_bookings['first_name'] : ''; ?>" placeholder="First Name" class="form-control" required="required" id = "first_name" >
    </div> 

    <div class="form-group">
        <label for="l_name">Last name *</label>
        <input type="text" name="last_name" value="<?php echo $edit ? $tb_bookings['last_name'] : ''; ?>" placeholder="Last Name" class="form-control" required="required" id="last_name">
    </div> 

    <div class="form-group">
        <label for="email">Email</label>
            <input  type="email" name="email" value="<?php echo $edit ? $tb_bookings['email'] : ''; ?>" placeholder="E-Mail Address" class="form-control" id="email">
    </div>

    <div class="form-group">
        <label>Visiting Date</label>
        <input name="visiting_date" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>"  placeholder="Visiting Date" class="form-control"  type="date">
    </div>

    <div class="form-group">
        <label>Visiting</label>
        <input name="visiting" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>"  placeholder="Who are they visiting?" class="form-control"  id="visiting">
    </div>

    <div class="form-group text-center">
        <label></label>
        <button type="submit" class="btn btn-warning" >Save <span class="glyphicon glyphicon-send"></span></button>
    </div>            
</fieldset>

标签: phpjqueryhtmlforms

解决方案


也许是这样的......

if (isset($_POST['submit'])) {

    $to = $_POST['email'];
    $name = $_POST['first_name'];
    $subject = 'Welcome ' . $name;
    $message = 'You have been booked in';
$headers = 'From: noreply@blah.com' . "\r\n" .
    'Reply-To: noreply@blah.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

}

推荐阅读