Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
AJAX loses "+" from email address string

I have some ajax code that seems to work fine for most of the places I've used it so far, however on trying to use it for a user details update on my page, it seems to lose the "+" symbol if used in an email address (such as test+test@test.com comes through as test test@test.com in my PHP file.

function prefUpdate() {
    let ytTog = document.getElementById("togBtn").checked;
    let unameNew = document.getElementById("uname").value;
    let usnameNew = document.getElementById("usname").value;
    let emailNew = document.getElementById("email").value;
    let params = "ytTog="+ytTog+"&unameNew="+unameNew+"&usnameNew="+usnameNew+"&emailNew="+emailNew;
    let xhr = new XMLHttpRequest();
    xhr.open('POST', 'pref_update.php', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            let return_data = xhr.responseText;
            if(return_data) {
               <do stuff>
            }
        }
    }
    xhr.send(params);
}

So here I grab a button, first and less name along with email address and pass it to a php file which returns some data that I can do stuff with.

I feel it could be the 'application/x-www-form-urlencoded' that's causing it, but googling around that hasn't given me much of a way of resolving it.

[edit] just to say I tried multipart/form-data in place of application/x-www-form-urlencoded but this just broke things entirely.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're right. For Content-Type: application/x-www-form-urlencoded all '+' signs mean ' ' (space). So it should be encoded by function encodeURIComponent().

let params = Object.entries({ ytTog, unameNew, usnameNew, emailNew })
  .map(pair => pair.map(encodeURIComponent).join('='))
  .join('&');

Form MDN:

For application/x-www-form-urlencoded, spaces are to be replaced by +, so one may wish to follow a encodeURIComponent() replacement with an additional replacement of %20 with +.

about 4 years ago · Juan Pablo Isaza Report

0

You have to encode your data properly when making an ajax call.
You can use a URLSearchParams object to do this for you.

let params = new URLSearchParams({ytTog, unameNew, usnameNew, emailNew});
...
xhr.send(params);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!