这是我昨晚提出的一个问题的扩展版本,但更详细。我所有的 php 文件和 js 文件都在一个名为 js 的文件夹中,从我的 html 页面向下一层。我有一个联系表格,可以使用 php mail 将我的购物车内容和表单输入邮寄到我的电子邮件中。我正在验证提交时的表单:
<form name="theForm" id="theForm" onsubmit="return validateFormOnSubmit(this)">
使用此提交按钮:
<input type=submit name=Submit value=Send style=cursor:pointer/>
验证正常工作,这里是代码(validate.js):
function setSelRange(inputEl, selStart, selEnd) {
if (inputEl.setSelectionRange) {
inputEl.focus();
inputEl.setSelectionRange(selStart, selEnd);
} else if (inputEl.createtextRange) {
var range = inputEl.createtextRange();
range.collapse(true);
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);
range.select();
}
}
window.onload=function(){
setSelRange(document.theForm.textbox, 0, 0);
}
function validateFormOnSubmit(theForm) {
var reason = "";
reason += validateName(theForm.name);
reason += validatePhone(theForm.phone);
reason += validateEmail(theForm.emaile);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}
simpleCart.checkout()
}
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = '#3399ff';
error = "The required field has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}
function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
}
function validateName(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (fld.value == "") {
fld.style.background = '#3399ff';
error = "Please enter a name.\n";
} else if ((fld.value.length < 2) || (fld.value.length > 30)) {
fld.style.background = '#3399ff';
error = "The nme is too long!\n";
} else {
fld.style.background = 'White';
}
return error;
}
function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
var error="";
var tfld = trim(fld.value); // valu
trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
if (fld.value == "") {
fld.style.background = '#3399ff';
error = "Please enter an email address.\n";
} else if (!emailFilter.test(tfld)) { //test email for illegal characters
fld.style.background = '#3399ff';
error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
fld.style.background = '#3399ff';
error = "The email address contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
function validatePhone(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
if (fld.value == "") {
error = "Please enter a phone number.\n";
fld.style.background = '#3399ff';
} else if (isNaN(parseInt(stripped))) {
error = "The phone number can only contain numbers.\n";
fld.style.background = '#3399ff';
} else if (!(stripped.length == 10)) {
error = "The phone number is the wrong length. area code.\n";
fld.style.background = '#3399ff';
}
return error;
}
当表单成功验证时,我想然后运行函数 simpleCart.checkout() :(来自上面的代码)
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}
simpleCart.checkout()
}
这是我要调用的simpleCart.js中的函数(设置为 emailCheckout):
me.checkout = function() {
if( me.quantity === 0 ){
error("Cart is empty");
return;
}
switch( me.checkoutTo ){
case PayPal:
me.paypalCheckout();
break;
case GoogleCheckout:
me.googleCheckout();
break;
case Email:
me.emailCheckout();
break;
default:
me.customCheckout();
break;
}
};
me.emailCheckout = function() {
itemsString = "";
for( var current in me.items ){
var item = me.items[current];
itemsString += item.name + " " + item.quantity + " " + item.price + "\n";
}
var form = document.createElement("form");
form.style.display = "none";
form.method = "POST";
form.action = "js/sendjs.php";
form.acceptCharset = "utf-8";
form.appendChild(me.createHiddenElement("wsp_key", wsp_key));
form.appendChild(me.createHiddenElement("wsp_code", wsp_code));
form.appendChild(me.createHiddenElement("textbox", textbox));
form.appendChild(me.createHiddenElement("name",name));
form.appendChild(me.createHiddenElement("phone",phone));
form.appendChild(me.createHiddenElement("emaile",emaile));
form.appendChild(me.createHiddenElement("jcitems", itemsString));
form.appendChild(me.createHiddenElement("jctotal", me.total));
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
它从表单和操作 sendjs.php 中接收购物车信息、输入和验证码密钥和代码,然后将其邮寄给我:
(来自上面的代码)
form.action = "js/sendjs.php";
如果验证码正确,它将邮寄给我并重定向到“from-accepted.php”页面,如果不正确,它将重定向到“form-rejected.php”页面(sendjs.php):
<?php
include_once("wsp_captcha.php");
if(WSP_CheckImageCode() != "OK") {
header('location:/form-rejected.php');
die();
}
$to = 'diysoakwells@hotmail.com';
$subject = 'Order Inquiry';
$jcitems = " <p><b>ORDER:</b></p><p> " . $_POST['jcitems']."<p/>" . "<p><b>Total:</b> $" . $_POST['jctotal']."</p>";
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: inquiry@diysoakwells.com' . "\r\n" .
'Reply-To: noreply@diysoakwells.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
$phone = $_POST['phone'];
$emaile = $_POST['emaile'];
$textbox = $_POST['textbox'];
$text = "<html><body><p>This form was submitted on Your Web Site on \n $date at\n $time</p><p><b>Message:</b>\n$textbox</p><p><b>Customers Email Address:</b>$emaile</p><p><b>Customers Name:</b> $name </p><p><b>Customers Phone Number:</b> $phone </p></html></body>";
$body = $text . $jcitems;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
?>
问题是验证后不会操作 sendjs.php 并且只是重新加载联系页面。调试器没有显示错误,我已经通过其他方式测试了 simpleCart.checkout,它应该可以工作。很抱歉这篇文章的长度,但我很困惑并且正在失去业务(并且为了让它工作而睡了很多觉)没有这个表格起作用。
这是该页面的链接:
http://www.diysoakwells.com.au/cart.php
有没有人有什么建议?我现在必须跳出一个小时,但我会监控这篇文章,当我回来时,至少在接下来的四五个小时内解决这个问题,或者直到问题解决。提前致谢。
杰米