验证后邮件未发送

时间:2012-05-10 09:16:30

标签: php javascript forms email

这是我昨晚问的一个问题的扩展版本,但更详细。我的所有php文件和js文件都在一个名为js的文件夹中,从我的html页面向下一级。 我有一个联系表格,邮寄我的购物车的内容,并使用php邮件形成我的电子邮件的输入。我正在验证提交表格:

<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

有没有人有任何建议?我现在必须弹出一个小时,但我会监视这个帖子,当我回来时,至少在接下来的四五个小时或者直到它修复之前解决这个问题。提前谢谢。

杰米

1 个答案:

答案 0 :(得分:0)

在Chrome中我遇到此错误:未捕获的TypeError:无法读取validate.js上未定义的属性“textbox”:16

和BTW我被重定向到http://www.diysoakwells.com.au/form-rejected.php,所以这个帖子似乎没问题。以前它没有用,因为我的购物车是空的