草庐IT

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

coder 2024-04-18 原文

我已经构建了一个通过 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/

有关PHP 电子邮件表单通过时单词之间没有空格,也没有特殊字符的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  6. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  7. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  8. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  9. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  10. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

随机推荐