【问题标题】:HTML formsfails to post in UTF-8 - despite trying recommendationsHTML 表单无法以 UTF-8 发布 - 尽管尝试了建议
【发布时间】:2017-08-28 01:22:55
【问题描述】:

在阅读了一些类似的问题(UTF-8 all the way through, php form submit utf8?enter link description here, UTF-8 not working in HTML forms enter link description here)并尝试了他们的所有建议后,但我的应用程序中的用户表单仍然存在无法处理特殊字符(é、è 等)。一些补充说明:

  • 我可以在数据库(Postgres)上保存特殊字符,添加新用户或编辑现有条目,没有问题。如果我在 webapp 上检索这些条目,它会成功显示特殊字符(例如,adminPage/usersTable 或 adminPage/userInfoEdit#)。

  • 但是,如果我尝试保存该条目,它会将乱码写入数据库。

PHP 标头

  <?php header("Content-Type: text/html;charset=UTF-8");

PHP 帖子

if (empty($_POST) === false && empty($errors) === true) {
    $registerData = array(
        'email' => $_POST['email'],
        'first_name' => $_POST['firt_name'],
        'last_name' => $_POST['last_name'],
    );
registerUser($registerData);

PHP 函数

function registerUser($registerData) {
    array_walk($registerData, 'arraySanitize');
pg_query("INSERT INTO utilisateur ($fields, date_inscription, statut) VALUES ($data, '$date','$statut')");
function arraySanitize(&$item) {
    $item = htmlentities(strip_tags(pg_escape_string($item)));
}

HTML 表单

<form method="post" action="" accept-charset="UTF-8">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label>First name *</label>
                <input type="text" class="form-control" name="firt_name" value="">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label>Last name *</label>
                <input type="text" class="form-control" name="firt_name" value="">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label>email *</label>
                <input type="text" class="form-control" name="email" value="">
            </div>
        </div>

    </div>
</form>

我们将不胜感激任何 cmets、建议、提示、资源。如果您有任何问题,我很乐意澄清。 非常感谢!

【问题讨论】:

    标签: php html forms encoding utf-8


    【解决方案1】:

    您的 HTML &lt;head&gt; 是否有 &lt;meta charset="utf-8"&gt; 标记?

    【讨论】:

    • 是 @kmoser:
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2016-02-29
    相关资源
    最近更新 更多