我有一個客戶希望使用預訂頁面。該過程只是將聯繫信息發送給我自己。我一直是一名.NET開發人員,從未使用過PHP。我做了大量的研究,並試圖創建一個booking.php頁面。由於代碼與現有的聯繫頁面非常相似(它正在工作),因此我從contact.php複製了代碼,將其保存到booking.php,並根據需要調整了代碼。我已經發布了下面的代碼。我收到的錯誤消息是輸入一個有效的電子郵件地址。由於電子郵件(html/php)的代碼與聯繫人頁面相同,並且實際上使用的是有效的電子郵件,所以我很困惑如何調試:/。如果有人有任何建議,我會非常感激。PHP在預訂過程中出現錯誤
正如你可以看到下面,我只是在嘗試新領域的創建變量,並在我的電子郵件正文中不包括他們沒有。我打算在解決這個問題之後繼續討論這個問題。
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]][email protected]((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$duration = $_POST['duration'];
$people = $_POST['people'];
if(trim($name) == '') {
echo '<div class="error_message box-red box">Attention! You must enter your name.</div>';
exit();
}
else if(trim($email) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid email address.</div>';
exit();
}
else if(trim($phone) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid phone number.</div>';
exit();
}
else if(trim($address) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid address.</div>';
exit();
}
else if(trim($city) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid city.</div>';
exit();
}
else if(trim($state) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid state.</div>';
exit();
}
else if(trim($zip) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid zip.</div>';
exit();
}
else if(trim($duration) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid trip duration.</div>';
exit();
}
else if(trim($people) == '') {
echo '<div class="error_message box-red box">Attention! Please enter a valid number of people.</div>';
exit();
}
else if(!is_numeric($phone)) {
echo '<div class="error_message box-red box">Attention! Phone number can only contain digits.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message box-red box">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message box-red box">Attention! Please enter your message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "[email protected]";
$address = "[email protected]";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Yacht Charter Booked by ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";
$msg = wordwrap($e_body . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page' class='box-green box'>";
echo "<p><b>Email Sent Successfully.</b></p>";
echo "<p>Thank you $name, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
}
else {
echo 'ERROR!';
}
HTML
<form role="form" method="post" action="booking.php" id="bookingform">
<div id="message"></div>
<div class="one-half">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
<div class="one-half">
<label for="people">Number of people</label>
<select id="people">
<option value="" selected>Please Select</option>
<option value="6">Up to 6</option>
<option value="12">Up to 12</option>
</select>
</div>
<div class="one-half">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number" value="">
</div>
<div class="one-half">
<label for="email">E-mail address</label>
<input type="email" id="email"/>
</div>
<div class="one-half">
<label for="address">Address</label>
<input type="text" id="address" name="address" />
</div>
<div class="one-half">
<label for="zip">ZIP code</label>
<input type="text" id="zip" name="zip" />
</div>
<div class="one-half">
<label for="city">City</label>
<input type="text" id="city" name="city" />
</div>
<div class="one-half">
<label for="state">State</label>
<input type="text" id="state" name="state" />
</div>
<div class="one-half">
<label for="duration">Duration</label>
<select id="duration" name="duration">
<option value="">Please Select</option>
<option value="half" selected>1/2 Day</option>
<option value="3">3-Day (Catalina Special)</option>
</select>
</div>
<div class="one-half">
<label for="payment">Payment type</label>
<select id="payment" name="payment">
<option value="">Please Select</option>
<option value="cash" selected>Cash</option>
<option value="check">Check</option>
<option value="paypal">PayPal</option>
</select>
</div>
<input id="submit" name="submit" type="submit" value="Complete booking process" class="button medium full gold" />
</form>
** ADDED JAVASCRIPT //預訂表
$('#bookingform').submit(function(){
var action = $(this).attr('action');
$("#message").show(500,function() {
$('#message').hide();
$('#submit')
.after('<img src="images/contact-ajax-loader.gif" class="loader" />')
.attr('disabled','disabled');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
address: $('#address').val(),
city: $('#city').val(),
state: $('#state').val(),
zip: $('#zip').val(),
date: $('#date').val(),
duration: $('#duration').val(),
people: $('#people').val()
},
function(data){
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
$('#submit').removeAttr('disabled');
});
});
return false;
});
// CONTACT FORM
$('#contactform').submit(function(){
var action = $(this).attr('action');
$("#message").show(500,function() {
$('#message').hide();
$('#submit')
.after('<img src="images/contact-ajax-loader.gif" class="loader" />')
.attr('disabled','disabled');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
comments: $('#comments').val()
},
function(data){
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
$('#submit').removeAttr('disabled');
});
});
return false;
});
形式輸入需要一個名稱屬性 - ''。由於這個表單正在處理沒有名稱屬性的'contact.php',所以它可能會使用javascript來提交表單,並在其中添加name屬性。 – Sean
啊!謝謝肖恩!我沒有想到這一點。我現在要檢查腳本。 – EricBellDesigns
就是這樣..我現在正在處理腳本。如果你想補充一點,我會很高興接受它。我非常感謝你的時間和幫助。 – EricBellDesigns