PHP 电子邮件表单通过时单词之间没有空格,也没有特殊字符

标签 php ajax email

我已经构建了一个通过 AJAX 使用 php 邮件功能发送的表单。我的问题是,当我收到电子邮件时,所有空格都被替换为“+”,特殊字符被替换为“%40”之类的内容。

这是我的 AJAX 脚本:

<script>
    jQuery(document).ready(function($) {
        $('.builderForm').each(function() {
            $(this).on("submit", function(event) {
                event.preventDefault();
                var formData = $(this).serialize();
                var loading = '<div id="overlay"><img id="loading" src="<?php echo LAYERS_FORM_BUILDER__PLUGIN_URL;?>assets/images/loading.gif"></div>';
                $(loading).appendTo('body');
                $.ajax({
                        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                        url         : '<?php echo LAYERS_FORM_BUILDER__PLUGIN_URL;?>includes/class-send-form.php', // the url where we want to POST
                        data        : {'formData' :formData}, // our data object
                        dataType    : 'json', // what type of data do we expect back from the server
                        encode      : true
                    })
                    .done(function(data) {
                        // log data to the console so we can see
                        //console.log(data); 
                        setTimeout(function(){$('#overlay').hide();}, 4000);
                        $('#overlay').remove();
                        $('.successMsg').html("Message Was Sent Successfully!!");
                        setTimeout(function(){$('.successMsg').hide();}, 5000);
                        // Reset form after submission
                        $(".builderForm")[0].reset();
                    });

                event.preventDefault();

            });
        });
    });
</script>

还有我的 PHP 邮件处理程序代码:

$layers_email_address = '';
$layers_email_subject = '';

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {
    parse_str($_REQUEST['formData'], $formFields)
//$formFields = explode('&', $_REQUEST['formData']);
$html='<html><body>';
foreach($formFields as $key => $value) {
    $fields = explode('=', $key);
    $field = $fields[0];
    $value = $fields[1];
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .='</body></html>';
$to = "sam.skirrow@gmail.com";
$subject = "Form Submission";
$txt = $html;
$headers = "From: <sam.skirrow@gmail.com>". "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
mail($to,$subject,$txt,$headers);

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

我怎样才能让我的电子邮件以痛苦的文本形式出现,而不是省略特殊字符?

发送多个复选框数据时出现问题,这里是动态创建复选框的代码(它位于 foreach 语句中,并获取小部件中“选择选项”文本区域的每一行以填充每个复选框

if ($item['input_type'] == 'checkbox') { ?>
    <div class="options_container">
        <?php foreach(explode("\n", $item['select_options']) as $select_option) { ?>
        <input type="checkbox" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/\s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><span class="checkbox_option"><?php echo $select_option; ?></span>
        <?php } ?>
    </div>
<?php }

编辑:php 邮件处理程序的新代码:

$layers_email_address = '';
$layers_email_subject = '';

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {
    parse_str($_REQUEST['formData'], $formFields)

$html = '<html><body>';
foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .= '</body></html>';
$to = 'sam.skirrow@gmail.com';
$subject = "Form Submission";
$txt = $html;
$headers = "From: <sam.skirrow@gmail.com>" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1" . "\r\n";
mail($to, $subject, $txt, $headers);

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

最佳答案

函数 parse_str() 正是您要找的。


使用 parse_str($_REQUEST['formData'], $formFields) ] 而不是 $formFields = explode('&', $_REQUEST['formData']);

这将解析您的字符串 $_REQUEST['formData']key => value 创建一个数组在变量 $formFields 中.

函数文档 here .

编辑:

对于 foreach 工作,您需要这样做:

foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}

关于PHP 电子邮件表单通过时单词之间没有空格,也没有特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766686/

相关文章:

.net - 如何将我的 Web 应用程序与另一个 IM 系统的 Skype、Gtalk 集成?

javascript - 页面重新加载返回到index.php

php - Symfony 5 & EasyAdmin 3.0 - 找不到/admin 路由

java - 如何? - 在同一文件执行 GET 之前将字符串插入 php 文件

ajax - 如何将 Ajax 错误结果与原始请求关联起来?

delphi - 在 Delphi 中生成嵌入图像的 HTML 电子邮件

php - Mysql,如何在选择数据时自定义时间戳

javascript - 来自数据库数组的简单 Javascript 幻灯片

jquery - ajax 请求后从多个 div 创建 fancybox 画廊

php - 使用 gmail smtp 服务器时如何更改发件人地址