2014-08-30 188 views
-3

我想问你一个帮助。我使用简单的时间段预订日历,一切工作正常(详细信息正确地传输到msql数据库中),但我无法将所有表单详细信息发送到我的电子邮件以及客户电子邮件中以进行确认。使用php发送表单信息到电子邮件

我只能发送前。地址信息或名称信息,但不能同时发送,也不能发送所有详细信息。你能帮我么。非常感谢你们的帮助!

book_slots.php:

<?php 

include('php/connect.php'); 
include('new.php'); 

if(isset($_POST['slots_booked'])) $slots_booked = mysqli_real_escape_string($link, $_POST['slots_booked']); 
if(isset($_POST['name'])) $name = mysqli_real_escape_string($link, $_POST['name']); 
if(isset($_POST['address'])) $address = mysqli_real_escape_string($link, $_POST['address']); 
if(isset($_POST['email'])) $email = mysqli_real_escape_string($link, $_POST['email']); 
if(isset($_POST['phone'])) $phone = mysqli_real_escape_string($link, $_POST['phone']); 
if(isset($_POST['booking_date'])) $booking_date = mysqli_real_escape_string($link, $_POST['booking_date']); 
if(isset($_POST['cost_per_slot'])) $cost_per_slot = mysqli_real_escape_string($link, $_POST['cost_per_slot']); 


$booking_array = array(
    "slots_booked" => $slots_booked,  
    "booking_date" => $booking_date, 
    "cost_per_slot" => number_format($cost_per_slot, 2), 
    "name" => $name, 
    "address" => $address, 
    "email" => $email, 
    "phone" => $phone 
); 

$explode = explode('|', $slots_booked); 

foreach($explode as $slot) { 

    if(strlen($slot) > 0) { 

     $query = "INSERT INTO bookings (date, start, name, address, email, phone) VALUES ('$booking_date', '$slot', '$name', '$address', '$email', '$phone')"; 
     $result = mysqli_query($link, $query) or die(mysqli_error($link)); 

    } // Close if 

} // Close foreach 

header('Location: thankyou.php'); 


?> 

connect.php:

<?php 

// Make a MySQL Connection 
$host="my host"; 
$user="db_xxx"; 
$password="mypasswoe"; 
$db = "db_xxx"; 

$link = mysqli_connect($host, $user, $password); 
mysqli_select_db($link, $db) or die(mysql_error()); 

?> 

new.php:

<?php 
//if "email" variable is filled out, send email 
    if (isset($_REQUEST['email'])) { 

    //Email information 
    $admin_email = "[email protected]"; 
    $email = $_REQUEST['email']; 
    $subject = "Booking Confirmation"; 
    $comment = "Hi, \n\n Thank you for joining \n\n 
Regards Site Admin \n\n"; 

    //send email 
    mail($email, "$subject", "$comment", "From:" . $admin_email); 

    //Email response 
    echo "Thank you for contacting us!"; 
    } 

    //if "email" variable is not filled out, display the form 
    else { 
?> 

<form method="post"> 
    Email: <input name="email" type="text" /><br /> 
    Subject: <input name="subject" type="text" /><br /> 
    Message:Your Reservation has been made. Please check reservation detail below:<br /> 
    <textarea name="comment" rows="15" cols="40"></textarea><br /> 
    <textarea name="name" rows="15" cols="40"></textarea><br /> 
    <input type="submit" value="Submit" /> 
    </form> 

<?php 
    } 
?> 
+0

'或死亡(mysql_error()',需要是'或死(mysqli_error($ link)' - 你不能混用MySQL APIs,另外,删除''$ subject“,”$ co中的引号mue“ – 2014-08-30 23:35:16

+0

在打开'<?php'标记后立即在文件顶部添加错误报告 'error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否产生任何东西。 – 2014-08-31 00:00:15

+0

感谢您的回复我已经做出更改,因为您告诉我,我也添加了错误报告,但它不显示任何内容,只是标准谢谢您的留言 – 2014-08-31 00:18:35

回答

0
if(isset($_POST)){ 

//form validation vars 
$formok = true; 
$errors = array(); 

//sumbission data 
$ipaddress = $_SERVER['REMOTE_ADDR']; 
$date = date('d/m/Y'); 
$time = date('H:i:s'); 

//form data 
$name = $_POST['name']; 
$email = $_POST['email']; 
$telephone = $_POST['telephone']; 
$enquiry = $_POST['enquiry']; 
$message = $_POST['message']; 

//validate form data 

//validate name is not empty 
if(empty($name)){ 
    $formok = false; 
    $errors[] = "You have not entered a name"; 
} 

//validate email address is not empty 
if(empty($email)){ 
    $formok = false; 
    $errors[] = "You have not entered an email address"; 
//validate email address is valid 
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ 
    $formok = false; 
    $errors[] = "You have not entered a valid email address"; 
} 

//validate message is not empty 
if(empty($message)){ 
    $formok = false; 
    $errors[] = "You have not entered a message"; 
} 
//validate message is greater than 20 charcters 
elseif(strlen($message) < 20){ 
    $formok = false; 
    $errors[] = "Your message must be greater than 20 characters"; 
} 
//send email if all is ok 
if($formok){ 
    $headers = "MIME-Version: 1.0"."\r\n"; 
    $headers .= "Content-type:text/html;charset=UTF-8"."\r\n"; 
    $headers .= "From: ".$email."(".$enquiry.")\r\n"; 
    $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>"; 
    $emailbody .="<p><strong>Name: </strong>".$name."</p>"; 
    $emailbody .= "<p><strong>Email Address:</strong>".$email."</p>"; 
    $emailbody .= "<p><strong>Telephone:</strong>".$telephone."</p>"; 
    $emailbody .= "<p><strong>Enquiry:</strong>".$enquiry."</p>"; 
    $emailbody .="<p><strong>Message:</strong>".$message."</p>"; 
    $emailbody .= "This message was sent from the IP Address:".$ipaddress." on ".$date." at ".$time."</p>"; 
    mail("[email protected]","New Enquiry",$emailbody,$headers); 
} 
//what we need to return back to our form 
$returndata = array(
    'posted_form_data' => array(
     'name' => $name, 
     'email' => $email, 
     'telephone' => $telephone, 
     'enquiry' => $enquiry, 
     'message' => $message 
    ), 
    'form_ok' => $formok, 
    'errors' => $errors 
); 
//if this is not an ajax request 
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){ 
    //set session variables 
    session_start(); 
    $_SESSION['cf_returndata'] = $returndata; 

    //redirect back to form 
    header('location: ' . $_SERVER['HTTP_REFERER']); 
} 
} 
+0

它还没有工作,但感谢你的回复和帮助。现在我无法收到电子邮件 – 2014-08-31 10:44:40