如何将表单数据从jQuery提交到PHP?
我正在尝试用JQuery验证联系人表单,如果一切正常,将数据提交到php文件以发送到邮件,但我一直得到“访问控制允许来源”。如果页面位于“剥削RIP.com”,这是我在浏览器控制台中收到的错误 无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“” 如果页面位于“www.expoitrip.com”,则错误为 邮政404(未找到) 我试着加上如何将表单数据从jQuery提交到PHP?,php,jquery,html,forms,Php,Jquery,Html,Forms,我正在尝试用JQuery验证联系人表单,如果一切正常,将数据提交到php文件以发送到邮件,但我一直得到“访问控制允许来源”。如果页面位于“剥削RIP.com”,这是我在浏览器控制台中收到的错误 无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“” 如果页面位于“www.expoitrip.com”,则错误为 邮政404(未找到) 我试着加上 <?php header('Access-Control-A
<?php header('Access-Control-Allow-Origin: *'); ?>
编辑:多亏了@jeroen,问题已经解决,我能够发送邮件,但整个过程仍然无法按预期进行。我没有被转到感谢页面。这里是php
<?php
$myemail = "email@test.com"; //changed
date_default_timezone_set("Asia/Kolkata");
$name = $_POST['inputName'];
$phone = $_POST['inputPhone'];
$package = $_POST['inputPackage'];
$message1 = $_POST['inputMessage'];
$time = date("g:i:s a");
$ip = $_SERVER['REMOTE_ADDR'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Query at $time from Europe Landing Page";
$message = '<html><body>';
$message .= '<h1>Query Submitted on Singapore Landing Page:</h1>';
$message .= '<table rules="all" style="border-color: #666;width:400px;" cellpadding="10"><tr style="background:#0884d3;color:#ffffff;"><td colspan="2" align="center">Customer Details</td></tr>';
$message .= '<tr><td style="width:200px;">IP</td><td style="width:200px;">' . $ip . '</td></tr>';
$message .= '<tr><td>Name</td><td>' . $name . '</td></tr>';
$message .= '<tr><td>Phone/Email</td><td>' . $phone . '</td></tr>';
$message .= '<tr><td>Package</td><td>' . $package . '</td></tr>';
$message .= '<tr><td>Message</td><td>' . $message1 . '</td></tr>';
$message .= '</table></body></html>';
mail($myemail, $subject, $message, $headers);
header('Location: thank-you.html');
exit();
?>
您必须在JavaScript中执行重定向,而不是在PHP中执行重定向
...
else {
console.log('query in last else');
// Returns successful data submission message when the entered information is stored in database.
$.post("../mailer.php", {
inputName: name,
inputPhone: phone,
inputPackage: package,
inputMessage: message
}, function() {
window.location('thankyou.html');
});
}
可能与我在上看到的重复,但我不明白如何使用$.ajax而不是$.post?这件事我需要一些帮助。这个错误是如何产生的?你把标题放在哪里了?它应该与生成html表单的页面一起发送(不在mailer.php脚本上)@Pierre OlivierVares哦,我不知道。该页面是html,我不希望将其转换为php
http://www.exploitrip.com/testing/mailer.php
不存在,只需单击链接,就会出现404错误。看起来你需要正确的url或路径。谢谢,伙计,你的方向是对的,我使用了window.location.replace(“thank-you.html”)代码>
<?php
$myemail = "email@test.com"; //changed
date_default_timezone_set("Asia/Kolkata");
$name = $_POST['inputName'];
$phone = $_POST['inputPhone'];
$package = $_POST['inputPackage'];
$message1 = $_POST['inputMessage'];
$time = date("g:i:s a");
$ip = $_SERVER['REMOTE_ADDR'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Query at $time from Europe Landing Page";
$message = '<html><body>';
$message .= '<h1>Query Submitted on Singapore Landing Page:</h1>';
$message .= '<table rules="all" style="border-color: #666;width:400px;" cellpadding="10"><tr style="background:#0884d3;color:#ffffff;"><td colspan="2" align="center">Customer Details</td></tr>';
$message .= '<tr><td style="width:200px;">IP</td><td style="width:200px;">' . $ip . '</td></tr>';
$message .= '<tr><td>Name</td><td>' . $name . '</td></tr>';
$message .= '<tr><td>Phone/Email</td><td>' . $phone . '</td></tr>';
$message .= '<tr><td>Package</td><td>' . $package . '</td></tr>';
$message .= '<tr><td>Message</td><td>' . $message1 . '</td></tr>';
$message .= '</table></body></html>';
mail($myemail, $subject, $message, $headers);
header('Location: thank-you.html');
exit();
?>
...
else {
console.log('query in last else');
// Returns successful data submission message when the entered information is stored in database.
$.post("../mailer.php", {
inputName: name,
inputPhone: phone,
inputPackage: package,
inputMessage: message
}, function() {
window.location('thankyou.html');
});
}