1

I have a Text box and am just checking the availability of the text using jqueryAjax. Am facing a Issue like when am checking for a text containing & the Post Variables in the jquery I checked for the solution which was given in the Stack overflow Thread but still in that when we take the html encoded value for & it gives & which will again have & and as a result the jquery ajax post variable gets broken. the code i tried is given below

    //from the text field
    var textfieldValue=$('#text').val();

    //Jquery Ajax
    $.ajax({
       type: "POST",
       async: false,
       url: "ajax/CheckAvailabilityphppage.php",
       data: "fieldvalue="+textfieldValue,
       dataType: "json",
       success: function(data){
         if (data['Status'] == 'YES'){
           alert('yes its Available');
          }else{
           alert('Sorry Not Available');
          }
       }
   });

In the Firebug the Post Variables are shown as (if the text is P&P)

P   
fieldvalue P

The &P is taking as an another Post Parameter and as a result the ajax is only validating the text P

Any Help or suggestions are much Appreciated.

Thanks a Lot

Community
  • 1
  • 1
Philemon philip Kunjumon
  • 1,417
  • 1
  • 15
  • 34

2 Answers2

1

You need to URL-encode the value properly.

Use encodeURIComponent for that.

CBroe
  • 91,630
  • 14
  • 92
  • 150
1

Pass an object to the data parameter and jquery will automagically encode it for you.

data: { fieldvalue: textfieldValue },
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • To be fair, CBroe's method should have worked if this one did. `data: "fieldvalue="+encodeURIComponent(textfieldValue),` – Kevin B Apr 07 '14 at 15:53