HTML表单不再发布输入数据

时间:2014-11-02 19:59:17

标签: php ajax

我有一个'订阅'在使用Ajax验证输入字段的网站上,然后通过php处理表单。实际的html代码驻留在Wordpress页面上。由于某种原因,表格不再发布。没有数据发布到MySQL,也没有发送或接收电子邮件。

页面上会显示一条状态消息,只是挂在"请等待我们处理您的信息..."这条消息是我的Ajax代码的一部分,所以我在想我的PHP脚本有问题吗?我有一个联系人'使用几乎完全匹配'订阅'的页面页面用于验证输入字段,它可以工作。

我能够从早期工作时恢复ajax和php脚本,我上传回我的服务器,什么都没有,表单仍然没有处理。我还确保我的主机上的数据库凭据是正确的,甚至测试了我与mysql的连接,我能够连接。接下来,我确保能够使用以下脚本向我的主机发送邮件:

<?php
mail('subscriptions@3elementsreview.com','Test mail','The mail function is working!');
echo 'Mail sent!';

我不知道导致表格挂起的原因,我很乐意解决这个问题。任何帮助将不胜感激!

php代码:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    define('DB_NAME', '######');
    define('DB_USER', '######');
    define('DB_PASSWORD', '######');
    define('DB_HOST', '######');        
    $first = Trim(stripslashes($_POST['First']));
    $last = Trim(stripslashes($_POST['Last']));
    $city = Trim(stripslashes($_POST['City']));
    $state = Trim(stripslashes($_POST['State']));
    $country = Trim(stripslashes($_POST['Country']));
    $email = Trim(stripslashes($_POST['Email']));
    $tempt = $_POST['tempt'];
    $tempt2 = $_POST['tempt2'];            
    if ($tempt == 'http://' && empty($tempt2)) {
        $error_message = '';
        $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9­-]+\.[a-zA-Z.]{2,5}$/";
        if(!preg_match($reg_exp, $email))
            $error_message .= "<p>A valid email address is required.</p>";
        if (empty($first))
            $error_message .= "<p>Please provide your first name.</p>";
        if (empty($last))
            $error_message .= "<p>Please provide your last name.</p>";
        if (!empty($error_message)) {    
            $return['error'] = true;
            $return['msg'] = "<p>The request was successful, but the form was not filled out correctly.</p>".$error_message;
            echo json_encode($return);
            exit();
            $dbc = mysql_connect('######', '######', '######');
            $check_email_for_duplicates = mysql_query($dbc, "select * from 'members' where 'Email' = '".mysql_real_escape_string($email)."'");
            if(mysql_num_rows($check_email_for_duplicates) > 0) //Email address is unique within this system and must not be more than one
                echo 'Sorry, <b>'.$email.'</b> has already subscribed.';    
        } else {        
            $return['error'] = false;
            $return['msg'] = "<p style='top:12px; color:#ff6000; left:15px; text-align:left; font-size:1.50em;'>".$first .", <p style='top:0px; width:100%; left:15px; text-align:left; line-height:1.1em;'>your subscription request has been processed.</p>";
            echo json_encode($return);
        }
    } else {        
        $return['error'] = true;
        $return['msg'] = "<p>There was a problem while sending this form. Try it again.</p>";
        echo json_encode($return);
    }
    $to = "######, ######";
    $subject = "New Email Address for Mailing List";
    $headers = "From: $email\n";
    $headers .= "Content-type: text/html\r\n";
    $message = "<span style='color:#252525; font-size:1.2em;'>A visitor to 3Elements Review has entered the following information so they can be added to your mailing list.</span><br>\n
    <br>
    <span style='color:#252525; font-weight:bold; font-size:1.35em;'>$first $last</span><br>
    <span style='color:#252525; font-weight:bold; font-size:1.35em;'>$city, $state</span><br>
    <span style='color:#252525; font-weight:bold; font-size:1.35em;'>$country</span><br>
    <span style='color:#252525; font-weight:bold; font-size:1.35em;'>$email</span>";
    mail($to,$subject,$message,$headers);
    $user = "$email";
    $usersubject = "Thank you for subscribing to 3Elements Review";
    $userheaders = "From: ######\n";
    $userheaders .= "Content-type: text/html\r\n";
    $usermessage = "<body style='background-color:#dbf7ff; width:100%; height:105%;'>
    <span style='color:#353535; font-size:2em; font-weight:bold; font-family:pt sans;'>Welcome</span> <span style='color:#ff6000; font-size:2em; font-weight:bold; font-family:pt sans;'>$first</span><span style='color:#353535; font-size:2em; font-weight:bold; font-family:pt sans;'>,</span><br>
    <br>
    <span style='color:#353535; font-size:1.30em; font-family:noto serif;'>We're glad that you've decided to subscribe to our email list. We adore the people of $city!</span><br>
    <br>
    <span style='color:#353535; font-size:1.30em; font-family:noto serif;'>We have a lot of great things in the works that we're excited about at <em>3Elements Review,</em> and we can't wait to show <em>you</em> and all of the other writers, artists, and photographers immersed in the $city literary scene, all that <strong><em>3Elements Review</em></strong> has in store!</span><br>
    <br>
    <span style='color:#353535; font-size:1.30em; font-family:noto serif;'>Stay tuned for information regarding our issue release dates, contests, and other news!<br>
    <br>
    If you find us interesting enough, like us on Facebook and follow us on Twitter!</span><br>
    <br>
    <br>
    <span style='color:#353535; font-size:1.30em; font-family:noto serif;'>Sincerely,</span><br>
    <br>
    <span style='color:#ff6000; font-size:1.50em; font-family:pt sans;'>The 3Elements Team</span><br>
    <br>
    <br>
    <p>
    <span style='color:#353535; font-size:1.25em; font-family:pt sans; font-weight:bold;'>######</span><br>
    <span style='color:#353535; font-size:1.25em; font-family:pt sans; font-weight:bold;'>######</span><br>
    <span style='color:#ff6000; font-size:1.25em; font-family:pt sans; font-weight:bold;'>######</span><br>
    <img src='http://www.3elementsreview.com/images/logo-2.svg' width='122px' height='95px'>
    <br>
    <p>
    <span style='color:#353535; font-size:.9em; font-family:noto serif;'>You're receiving this email because you or someone you know subscribed you to our email list.</p>
    <span style='color:#353535; font-size:.9em; font-family:noto serif;'>If you would rather not receive email from 3Elements Review, you can unsubscribe by clicking <a href='mailto:######?Subject=Unsubscribe%20Request'>here</a>.</p>
    <br>
    <p>
    </body>";
    mail($user,$usersubject,$usermessage,$userheaders);
    $link = mysql_connect('######', '######', '######');
    if (!$link)
        die('Could not connect: ' . mysql_error());
    $db_selected = mysql_select_db('######', $link);
    if (!$db_selected)
        die('Can\'t use ' . '######' . ': ' . mysql_error());
    $value = mysql_real_escape_string($_POST['First']);
    $value2 = mysql_real_escape_string($_POST['Last']);
    $value3 = mysql_real_escape_string($_POST['City']);
    $value4 = mysql_real_escape_string($_POST['State']);
    $value5 = mysql_real_escape_string($_POST['Country']);
    $value6 = mysql_real_escape_string($_POST['Email']);
    $sql = "INSERT INTO members (First, Last, City, State, Country, Email, Date) VALUES('$value','$value2','$value3','$value4','$value5','$value6',NOW() + interval 2 hour)";
    if (!mysql_query($sql))
        die('Error: ' . mysql_error());
    mysql_close();
} else {}
?>

AJAX代码:

//*************************************************************REQUIRED FIELD SCRIPT FOR SUBSCRIBE PAGE*************************************************//
$(document).ready(function() {
  $('form #response2').hide();
  $('.button2').click(function(e) {
    e.preventDefault();
    var valid = '';
    var required = ' is required';
    var first = $('form #first').val();
    var last = $('form #last').val();
    var email = $('form #email').val();
    var tempt = $('form #tempt').val();
    var tempt2 = $('form #tempt2').val();
    if (first = '' || first.length <= 1)
        valid += '<p>Your first name' + required + '</p>';
    if (last = '' || last.length <= 1)
        valid += '<p>Your last name' + required + '</p>';
    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))
        valid += '<p>Your e-mail address' + required + '</p>';
    if (tempt != 'http://') 
        valid += '<p>We can\'t allow spam bots.</p>';
    if (tempt2 != '') 
        valid += '<p>A human user' + required + '</p>';
    if (valid != '')
        $('form #response2').removeClass().addClass('error2').html('' +valid).fadeIn('fast');
    else {
        $('form #response2').removeClass().addClass('processing2').html('<p style="top:0px; left:0px; text-align:center; line-height:1.5em;">Please wait while we process your information...</p>').fadeIn('fast');
        var formData = $('form').serialize();
        submitForm(formData);
    }
  });
});
function submitForm(formData) {
    $.ajax({
            type: 'POST',
            url: 'http://3elementsreview.com/blog/wp-content/themes/3elements/php-signup/sign-up-complete.php',
            data: formData,
            dataType: 'json',
            cache: false,
            timeout: 4000,
            success: function(data) {
                $('form #response2').removeClass().addClass((data.error === true) ? 'error2' : 'success2').html(data.msg).fadeIn('fast');
            if ($('form #response2').hasClass('success2'))
                setTimeout("$('form #response2').fadeOut('fast')", 6000);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
                $('form #response2').removeClass().addClass('error2')
                .html('<p>There was an <strong>' + errorThrown +
                '</strong> error due to an <strong>' + textStatus +
                '</strong> condition.</p>').fadeIn('fast');
        },
        complete: function(XMLHttpRequest, status) {
                $('form')[0].reset();
        }
    });
};

1 个答案:

答案 0 :(得分:0)

我几天前想到了这一点,但我在我网站上的两个不同脚本中使用了'functionSubmitForm'。此外,我重新排序了我的页脚中的脚本链接,我的表单再次运行。