2

I have an Ajax form that I want to allow the users to include apostrophes.

function print_and_update(id,type)
{    
$.ajax(
{
  url: "index.php",
  cache: false,
  type : "GET",
  data:
    {
      "q1" : $('#q1').val(),
      "q2" : $('#q2').val(),
      "q3" : $('#q3').val(),
      ......
      "q10" : $('#q10').val(),
    },
  success: ....

So I can replace the apostrophe for each individually. But is the best way? Should I be able to replace it for all the input text data?

"q1" : $('#q1').val().replace(/'/g, "'"),

I tried editing the PHP by adding htmlspecialchars and stripslashes with no success. How can I allow apostrophes for all text inputs?

N13Design
  • 85
  • 2
  • 2
  • 12
  • There is a syntax error in that line. The first `,` should be removed. – Ram Apr 23 '15 at 17:26
  • @Vohuman The code posted is a snippet. – N13Design Apr 23 '15 at 17:33
  • I meant `$('#q1').val(),.replace...` should be `$('#q1').val().replace...` – Ram Apr 23 '15 at 17:35
  • Thanks @Vohuman. I corrected the typo. This was just an error from transcribing here. I had it correct in my code. – N13Design Apr 23 '15 at 17:41
  • You also have an extra comma `"q10" : $('#q10').val(),`. Not sure if that's transcription related or not. – Robbert Apr 23 '15 at 17:42
  • What exactly do you mean by "It seems to not work after I try it with more than 4"? – Ram Apr 23 '15 at 17:42
  • @Vohuman. I found the problem with the more than 4. The previous developer had edited the form and removed some inputs but didn't update the jquery. What is the best way to allow apostrophes rather than added the replace to every single input? – N13Design Apr 23 '15 at 17:49
  • You don't need to do anything to allow apostrophes, what problem are you having when there are apostrophes? – Musa Apr 23 '15 at 19:20
  • When a user includes an apostrophe in an textarea or text input field the Form will say update complete successful but none of the changed data will be saved. It works if I use the replace function but I was hoping for a better/cleaner way. – N13Design Apr 23 '15 at 19:36

1 Answers1

0

Try this...

"q1" : $('#q1').val().replace(/\'/g, "'"),
lem2802
  • 1,152
  • 7
  • 18