【问题标题】:PHP adding characters to jQuery.AJAX posted stringPHP将字符添加到jQuery.AJAX发布的字符串
【发布时间】:2013-01-08 00:58:12
【问题描述】:

在网上或通过搜索功能找不到任何答案,所以我决定发布它:

我有一个表单,就像其他表单一样,被发布到 CodeIgniter 中的 AJAX 控制器。以下代码是正在发布的较大表单的一部分。

<tr>
    <td><label class="checkbox"><input type="checkbox" name="ef1_active" value="1"></label></td>
    <td><input type="text" name="ef1_label" value=""></td>
    <td><select name="ef1_clang" class="input-small">'.$optionlist.'</select></td>
    <td><label class="checkbox"><input type="checkbox" name="ef1_required" value="1"></label></td>
</tr>
<tr>
    <td><label class="checkbox"><input type="checkbox" name="ef2_active" value="1"></label></td>
    <td><input type="text" name="ef2_label" value=""></td>
    <td><select name="ef2_clang" class="input-small">'.$optionlist.'</select></td>
    <td><label class="checkbox"><input type="checkbox" name="ef2_required" value="1"></label></td>
</tr>
<tr>
    <td><label class="checkbox"><input type="checkbox" name="ef3_active" value="1"></label></td>
    <td><input type="text" name="ef3_label" value=""></td>
    <td><select name="ef3_clang" class="input-small">'.$optionlist.'</select></td>
    <td><label class="checkbox"><input type="checkbox" name="ef3_required" value="1"></label></td>
</tr>
<tr>
    <td><label class="checkbox"><input type="checkbox" name="ef4_active" value="1"></label></td>
    <td><input type="text" name="ef4_label" value=""></td>
    <td><select name="ef4_clang" class="input-small">'.$optionlist.'</select></td>
    <td><label class="checkbox"><input type="checkbox" name="ef4_required" value="1"></label></td>
</tr>

表单正在被序列化,并被推送到 ajax 以使用此代码保存:

var data = {
    content : content,
    property : property,
    data_type : dataType,
    form_id : $('#form_id').val()
};


 $.ajax({
    type: 'POST',
    url: url,
    data: data,
    dataType: 'json',
    success: function (result)
    {

        if(result.html == 'deleted')
        {
           window.location.reload(); 
        }
        else
        {
            $('#newForm').html('');
        }

    }
}); 

console.logged时'content'变量的序列化结果:

firstname=1&firstname_required=1&prefix=1&lastname=1&lastname_required=1&homePhone=1&homePhone_required=1&emailAddress=1&emailAddress_required=1&ef1_label=&ef1_clang=Code&ef2_label=&ef2_clang=Code&ef3_label=&ef3_clang=Code&ef4_label=&ef4_clang=Code

但是当我在 PHP 中回显它时,它看起来像这样:

firstname=1&firstname_required=1&prefix=1&lastname=1&lastname_required=1&homePhone=1&homePhone_required=1&emailAddress=1&emailAddress_required=1&ef1;_label=&ef1_clang=Code&ef2;_label=&ef2_clang=Code&ef3;_label=&ef3_clang=Code&ef4;_label=&ef4_clang=Code

注意所有_label 变量上添加的;... 尝试重命名变量,将其放在表单顶部等......没有任何效果; PHP 不断在这些文本字段上添加;。

甚至将控制器简化为:

public function save_form_content()
{       
    print_r($_POST['content']); exit;
}

有人知道吗?

提前致谢!

【问题讨论】:

  • 你想做什么?
  • 它就像一个小型表单构建器,表单的顶部是带有默认表单字段(如名字等)的复选框。第二部分是可以分配给我们 CRM 中不同选项字段的额外字段。然后标记将被保存到数据库中。

标签: php jquery ajax forms codeigniter


【解决方案1】:

这是 CodeIgniter 中的一个已知错误,因为您使用的是 xss_clean() 或 $config['global_xss_filtering'] = TRUE;,请参阅 bug report。更新 CodeIgniter 以修复问题,或者如果无法更新整个 CI,请查看 Git 提交以应用提交中的补丁。

根据其中一位开发者的说法:

应该修复替换第 797 行中的正则表达式 system/core/Security.php 通过这个:

$str = preg_replace('#(^&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', '\\1;\\2', $str);

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2023-03-14
    相关资源
    最近更新 更多