Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法获取要提交的表单_Javascript_Php_Jquery_Forms_Submission - Fatal编程技术网

Javascript 无法获取要提交的表单

Javascript 无法获取要提交的表单,javascript,php,jquery,forms,submission,Javascript,Php,Jquery,Forms,Submission,我希望有人能看看我的代码,让我知道发生了什么。我有一张表格。当它被提交时,弹出窗口出现,告诉我它失败了,我得到一个页面,上面只写着“未定义”。有人对A:为什么发送失败和B:我需要如何修改JavaScript以使页面在提交后返回主页有什么想法吗 HTML: <div class="contact-form"> <p class="mandatory">* indicates Manadatory Field</p> <div

我希望有人能看看我的代码,让我知道发生了什么。我有一张表格。当它被提交时,弹出窗口出现,告诉我它失败了,我得到一个页面,上面只写着“未定义”。有人对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>
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);
}
}
?>

您的条件语句永远不会在成功函数中激发,因为它将始终为false
(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
,但是您不继续对标题进行浓缩,因此不需要它

感谢您抽出时间查看我的代码。这是我的第一次PHP集成。
setTimeout(function(){
    window.location = 'mydomain.tld/my-homepage.ext';
}, 5000);