我有一个简单的表单,其中包含四个输入字段:姓名、电子邮件、电话和消息。我正在使用 JavaScript 和 Ajax 将我的输入字段发布到 PHP 文件,以便通过 phpmailer 进行验证和邮寄。
我从验证函数中收集数组中的错误。I use and if 语句通过json_encode(array)
函数将错误发送回 html 页面并退出脚本。否则,我让脚本继续并成功地将电子邮件发送到我的收件箱。
我json_encode(array)
遇到的问题是退出发现错误的脚本。即使我没有用 die 或 exit 强制退出脚本。在这方面,由于脚本过早终止,因此不会发送电子邮件。
我在我的 php 文件下面列出了几行,以显示我的 if 语句是如何设置的。
我已经在网上搜索了无济于事的答案。我试图将该json_encode
函数放置在文件中的不同位置,但它不起作用。我尝试将错误数组与邮件发送函数中的成功或失败字符串结合起来,但没有奏效。
if ( $errors != null ) {
echo json_encode( $errors );
exit;
} else {
$mail = new PHPMailer( true );
/* Set the mail sender. */
$mail->setFrom( '[email protected]', 'John Smith' );
/* Add a recipient. */
//set who is receving mail
$mail->addAddress( '[email protected]' );
/* Set the subject. */
$mail->Subject = 'New message from contact form';
/* Set email to be sent as HTML */
$mail->isHTML( true );
/* Set the mail message body. */
$mail->Body =
我期待一旦 if 语句清除了错误,整个 php 文件脚本将继续,直到通过电子邮件发送数据。然而,这并没有发生。
我在另一个帖子上发布的另一个相关问题中找到了答案。错误数组包含所有空变量,我必须确定是否应在布尔值为 false(未发现错误)时发送邮件的条件语句并未将数组视为空变量。我不得不更改条件 if 语句以使用 array_filters。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句