PHP将字符添加到jQuery.AJAX发布的字符串

时间:2013-01-24 10:00:50

标签: php jquery ajax forms codeigniter

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

我有一个表单,就像任何其他表单一样,被发布到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;
}

有人有任何想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

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

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

  应该修复

替换在#797行的regexp   system / core / Security.php由这一个:

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