Allowing ampersand (&) in a text box

I have searched for this but no luck.

I have a textarea on a form that I am trying to submit via ajax using jQuery. My problem is that the textarea contains an ampersand (&) which is interrupted. That is, any text after &

it will not be selected, including the ampersand (&). I am sending data like this:

message = "What are the concerns you are looking to address? "+othr+".\n";
var dataString = 'name=' + name + '&email=' + email + '&company=' + company + '&message=' + message + '&othr=' + othr + '&vpb_captcha_code=' + vpb_captcha_code + '&submitted=1';
$.ajax({  

type: "POST",  

url: "contactus-contact-form.php",  

data: dataString ,

      

I read encodeURIComponent()

and tried to implement the same but it doesn't work. Any pointers?

EDIT: Can't get text values:

message = "What are the concerns or security issues you are looking to address? "+othr+".\n";
var data0 = {name: + name + email: + email + company: + company + message: + message + 
othr: + othr + vpb_captcha_code: + vpb_captcha_code + submitted: "1"};

$.ajax({  

type: "POST",  
url: "contactus-contact-form.php",  
data: data0,
contentType: "application/json; charset=utf-8",
dataType: "json",

      

+3


source to share


2 answers


&

is a character used to separate key / value pairs in encoded forms of data. You need to avoid it if you want to use it as part of the data.

The easiest way to do this is to let jQuery take care of generating the lowercase encoded string for you. Pass the object to the argument data

:

$.ajax({  
    type: "POST",  
    data: {
        name: name, 
        email: email, 
        company: company, 
        message: message, 
        othr: othr,
        vpb_captcha_code: vpb_captcha_code,
        submitted: 1
    },
    // etc

      



If you really want to do it manually, then the function encodeURIComponent

will work:

var dataString = "name=" + encodeURIComponent(name) + "&email=" + encodeURIComponent(email) // etc etc etc

      

+5


source


ou can solve your problem with the encodeURIComponent () function. You have just changed the code as shown below. I think you have set the textarea value to the othr variable. You can just encode this variable with just encodeURIComponent (othr). This means you can parse the textarea value using the encodeURIComponent method.

 message = "What are the concerns you are looking to address? "+othr+".\n";
var dataString = 'name=' + name + '&email=' + email + '&company=' + company + '&message=' + message + '&othr=' + encodeURIComponent(othr) + '&vpb_captcha_code=' + vpb_captcha_code + '&submitted=1';
$.ajax({  

type: "POST",  

url: "contactus-contact-form.php",  

data: dataString ,
});

      

or




message = "What are the concerns or security issues you are looking to address? "+othr+".\n";
var data0 = {name: + name + email: + email + company: + company + message: + message + 
othr: +encodeURIComponent(othr) + vpb_captcha_code: + vpb_captcha_code + submitted: "1"};

$.ajax({  

type: "POST",  
url: "contactus-contact-form.php",  
data: data0,   
dataType: "json",

      

-2


source







All Articles