当表单在本地服务器上正常工作时,为啥表单中的数据没有发布到主机服务器上的 PHP 文件?
Posted
技术标签:
【中文标题】当表单在本地服务器上正常工作时,为啥表单中的数据没有发布到主机服务器上的 PHP 文件?【英文标题】:Why is the data from the form not getting posted to the PHP file on the host server, when it works fine on the local server?当表单在本地服务器上正常工作时,为什么表单中的数据没有发布到主机服务器上的 PHP 文件? 【发布时间】:2021-03-10 13:12:18 【问题描述】:我正在尝试构建一个简单的联系表单。当我在本地服务器上托管网站时,ajax 将数据发布到 php 文件,并且 PHP 文件能够获取信息。但是当我在主机服务器上托管网站时,PHP 文件无法访问发布的数据。我不太清楚为什么。
JQuery/Ajax:
$(document).ready(function()
$("#contactFormToSubmit").submit(function(e)
e.preventDefault();
let fullname = $("#nameForm")
.val()
.trim();
let subject = $("#subjForm")
.val()
.trim();
let email = $("#emailForm")
.val()
.trim();
let message = $("#textForm")
.val()
.trim();
let new_form = new FormData();
new_form.append("fullname", fullname);
new_form.append("subject", subject);
new_form.append("emailFrom", email);
new_form.append("message", message);
$.ajax(
type: "POST",
url: "contactFormPHP/submitform.php",
data: new_form,
contentType: false,
processData: false,
cache: false,
success: function()
$("#FormSubmitDiv").append(
'<div class="alert alert-success text-center alert-dismissible fade show" role="alert"><strong>Submitted successfully! Thank you.</strong><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>'
);
$(".alert-success").fadeOut(10000);
$("#contactFormToSubmit")[0].reset();
);
);
);
和 PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'config.php';
require 'plugins/PHPMailer/src/Exception.php';
require 'plugins/PHPMailer/src/PHPMailer.php';
require 'plugins/PHPMailer/src/SMTP.php';
$to = '*********@gmail.com';
if(isset($_POST['fullname']))
echo "<script>console.log('It is working properly!');</script>";
if(!isset($_POST['fullname']))
echo "<script>console.log('It is not working properly!');</script>";
$fullname = $_POST['fullname'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = $_POST['emailFrom'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: " .$email_from. "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
function console_log($fullname, $with_script_tags = true)
$js_code = 'console.log(' . json_encode($fullname, JSON_HEX_TAG) .
');';
if ($with_script_tags)
$js_code = '<script>' . $js_code . '</script>';
echo $js_code;
$mail = new PHPMailer(true);
try
$mail->STMPOptions = array(
'ssl'=> array(
'verify_peer'=> false,
'verify_peer_name'=> false,
'allow_self_signed'=> true,
)
);
$mail->isSMTP();
$mail->SMTPDebug = 2; // Send using SMTP
$mail->Host = CONFIG['email']['host']; // Set the SMTP server to send through
$mail->SMTPAuth = true;
$mail->Port = CONFIG['email']['port']; // Enable SMTP authentication
$mail->SMTPSecure = CONFIG['email']['SMTPSecure']; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Username = CONFIG['email']['username']; // SMTP username
$mail->Password = CONFIG['email']['password']; // SMTP password
//Recipients
$mail->AddReplyTo($email_from, $fullname);
$mail->setFrom($email_from, $fullname);
$mail->addAddress($to, 'Blue Owl Learning');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
$mail->send();
echo 'Message has been sent';
catch (Exception $e)
echo "Message could not be sent. Mailer Error: $mail->ErrorInfo";
【问题讨论】:
【参考方案1】:我使用 .htaccess 来更改链接。所以虽然没有明显的语法错误,但我使用的链接有点错误,所以将 url 从
url: "contactFormPHP/submitform.php"
到
url: "contactFormPHP/submitform"
解决了问题
【讨论】:
以上是关于当表单在本地服务器上正常工作时,为啥表单中的数据没有发布到主机服务器上的 PHP 文件?的主要内容,如果未能解决你的问题,请参考以下文章
从表单尝试时记录集不可更新,但如果仅加载子表单,则可以正常工作