【问题标题】:Cannot get form to submit无法获取要提交的表单
【发布时间】:2014-03-04 03:37:24
【问题描述】:

我希望有人可以查看我的代码并让我知道发生了什么。我有一个表格。当它被提交时,弹出窗口会告诉我它失败了,我得到一个页面,上面写着“未定义”。任何人都对 A:为什么发送失败和 B:我需要如何修改我的 JavaScript 以使页面在提交后返回主页有任何想法。

HTML:

<div class="contact-form">
        <p class="mandatory">* indicates Manadatory Field</p>
        <div data-role="fieldcontain" class="text-field">
            <label for="firstname">First Name*:</label>
            <input type="text" name="firstname" value="" placeholder=""     class="required" id="firstname" />
        </div>
        <div data-role="fieldcontain" class="text-field">
            <label for="surname">Last Name:</label>
            <input type="text" name="surname" value="" placeholder="" id="surname" />
        </div>
        <div data-role="fieldcontain" class="text-field">
            <label for="email">Email Address*:</label>
            <input type="email" name="email" value="" placeholder=""     class="required" id="email"  />
        </div>
        <div data-role="fieldcontain" class="text-field">
            <label for="mobilephone">Mobile Number:</label>
            <input type="number" name="mobilephone" value="" placeholder="" id="mobilephone" />
        </div>    

<div data-role="fieldcontain">
<label for="message">Message*:</label>
<textarea name="message" id="message" placeholder="" class="required"></textarea>
</div>
<div class="send"><a href="javascript.js" data-role="button" data-theme="a" data-    iconpos="right" id="send-feedback">Send Message</a></div>

JAVASCRIPT

 $(function () {
     $("#symptomsemployersbutton").click(function () {
         $("#symptomsemployers").toggle("slow");
     });
 });

 $('#send-feedback').live("click", function () {
     var url = 'submit.php';
     var error = 0;
     var $contactpage = $(this).closest('.ui-page');
     var $contactform = $(this).closest('.contact-form');
     $('.required', $contactform).each(function (i) {
         if ($(this).val() === '') {
             error++;
         }
     }); // each
     if (error > 0) {
         alert('Please fill in all the mandatory fields. Mandatory fields are marked with an asterisk *.');
     } else {
         var firstname = $contactform.find('input[name="firstname"]').val();
         var surname = $contactform.find('input[name="surname"]').val();
         var mobilephone = $contactform.find('input[name="mobilephone"]').val();
         var email = $contactform.find('input[name="email"]').val();
         var message = $contactform.find('textarea[name="message"]').val();

         //submit the form
         $.ajax({
             type: "GET",
             url: url,
             data: {
                 firstname: firstname,
                 surname: surname,
                 mobilephone: mobilephone,
                 email: email,
                 message: message
             },
             success: function (data) {
                 if (data == 'success') {
                     // show thank you 
                     $contactpage.find('.contact-thankyou').show();
                     $contactpage.find('.contact-form').hide();
                 } else {
                     alert('Unable to send your message. Please try again.');
                 }
             }
         }); //$.ajax

     }
     return false;
 });

PHP

<?php 
header('content-type: application/json; charset=utf-8');

if (isset($_GET["firstname"])) {
$firstname = strip_tags($_GET['firstname']);
$surname = strip_tags($_GET['surname']);
$email = strip_tags($_GET['email']);
$mobilephone = strip_tags($_GET['mobilephone']);
$message = strip_tags($_GET['message']);
$header = "From: ". $firstname . " <" . $email . ">rn"; 

$ip = $_SERVER['REMOTE_ADDR']; 
$httpref = $_SERVER['HTTP_REFERER']; 
$httpagent = $_SERVER['HTTP_USER_AGENT']; 
$today = date("F j, Y, g:i a");    

$recipient = 'mark@launchintervention.com';
$subject = 'Contact Form';
$mailbody = "
First Name: $firstname   
Last Name: $surname 
Email: $email 
Mobile Phone: $mobilephone 
Message: $message

IP: $ip
Browser info: $httpagent
Referral: $httpref
Sent: $today
";
$result = 'success';

if (mail($recipient, $subject, $mailbody, $header)) {
    echo json_encode($result);
}
}
?>

【问题讨论】:

    标签: javascript php jquery forms submission


    【解决方案1】:

    你的条件语句永远不会在你的成功函数中触发,因为它总是错误的。 (data == 'success') 永远不会起作用,因为您对该字符串的 json 编码返回值 "success" 而不是 success。我不知道你为什么要对它进行 json 编码,但你应该做一些其他的事情,比如

    $result = array(
        'status' => 'success'
    );
    echo json_encode($result);
    

    那你就可以了

    (data.status == 'success')
    

    至于结果返回成功后重定向,在下面一行之后:

    $contactpage.find('.contact-form').hide();
    

    你应该这样做:

    setTimeout(function(){
        window.location = 'mydomain.tld/my-homepage.ext';
    }, 5000);
    

    你的contact-thankyou 类元素应该有一些类型的文本,比如“我们已经收到你的提交。你将在 5 秒内被重定向到主页。”。然后在 5 秒后,它们将根据之前定义的 setTimeout 函数重定向。

    您的标头声明末尾还有一个rn,我认为它应该是\r\n,但是您不会继续连接标头,因此它不是必需的。 Please review the RFC2822 on this.

    【讨论】:

    • 感谢您花时间浏览我的代码。这是我的第一个 PHP 集成。
    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多