首页 > 解决方案 > 如何在php中清理ajax发布数据?

问题描述

我有一个要清理的文本字段。来自文本字段的数据通过 ajax post 发送到 php 文件。在将其写入 mysql 数据库之前,我想在 php 文件中清理此字符串。

我的问题是(据我了解)我无法在该 php 文件中调用任何清理函数,因为它是由 ajax 调用的,因此这些函数不可用。这个对吗?如果是这样,您如何清理这些数据?

带有清理功能的 ajax 调用的 php 文件示例...

function sanitizeString($string)
{
    $string = strip_tags($string);  //I cannot access these functions because the file was called by ajax
    $string = htmlentities($string);
    $string = stripslashes($string);
}
$group_id = sanitizeString($_POST['userid']);
$textbox_value = sanitizeString($_POST['title_text']);
//Write these values to mysql database...

//DOES NOT WORK
$group_id = sanitizeString($_POST['userid']);
$textbox_value = sanitizeString($_POST['title_text']);

//replacing above with this does work (and commenting out function)
$group_id = $_POST['userid'];
$textbox_value = $_POST['title_text'];

标签: php

解决方案


推荐阅读