How to escape “&” ampersand character in form input using jQuery

后端 未结 5 1088
余生分开走
余生分开走 2020-12-30 06:00

I have a problem with my jQuery script that send data via POST method. The problem is whenever it sends data via Ajax that has an "&" ampersand in the sentence

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 06:49

    I've been having a huge problem exactly with this situation.

    This is just to say that the last answer from Andrew Koester is the perfect answer I was looking for.

    In case you are passing multiple form entries from a jQuery form to PHP through the .ajax() call like this:

    data: "name=" + name + "&message=" + message + ...

    DON'T USE THIS METHOD, it will block the ampersand(&) character from being written by the user on any of the input fields of your form.

    Use this one instead as suggested by Andrew:

    data: {"name": name, "email": email, "subject": subject, "comments": comments},

    This way the user can write any kind of special character whithout worrying a about conflicting with the ajax declaration.

提交回复
热议问题