开发者

Reporting sanitized user input to the user via AJAX

I am writing some code to give live feedback to the user on the validation of a form using AJAX. I have got it checking length and if the field is empty. Now I want it to sanitize the users input and if the sanatized input differs from the users original 开发者_开发技巧input then tell them which characters are not allowed.

The code I have written so far works except some characters most notably a '£' symbol result in no response. I think it relates to json_encode and its encoding.

Here is the code:

  $user_input = 'asdfsfs£';
  $strip_array = str_split(strip($user_input));
  $orig_array = str_split($user_input);
  $diff_array = array_diff($orig_array,$strip_array);
  $diff_str = implode(', ',$diff_array);
  $final = json_encode($diff_str);

  function strip($input){return htmlentities(strip_tags($input),ENT_QUOTES);}

Hope someone can figure out a solution.


if this is to be done via AJAX then you will need to write a javascript function to accomplish this.

Let the server return a JSON object of the format

{
    original: 'asdfsfs£',
    edited: 'asdfsfs',
    illegalChars: ['£']
}

in your html, you should have empty elements next to the form input fields where you can output the form validation message

<input type="text" id="myTextField" />
<span id="myTextFieldLabel" />

now let your javascript update the content of the myTextFieldLabel component according to the validation data

Sean

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜