微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP电子邮件表单在单词之间没有空格而没有特殊字符

我已经构建了一个通过AJAX使用PHP邮件功能发送的表单.我的问题是当我的电子邮件通过时,所有空格都被”替换,而特殊字符被’@’等替换.

这是我的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 = "[email protected]";
$subject = "Form Submission";
$txt = $html;
$headers = "From: <[email protected]>". "\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语句中,并在窗口小部件中的“select-options”文本区域的每一行填充每个复选框

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 = '[email protected]';
$subject = "Form Submission";
$txt = $html;
$headers = "From: <[email protected]>" . "\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 =>的数组.变量$formFields中的值.

功能here的文档.

编辑:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐