Syntax error concatenation

Asked

Viewed 48 times

-2

My function returns the following error:

Uncaught Syntaxerror: Unexpected string

How can I correct?

function setarRegistro() {
    campos = "teste="+encodeURI(document.getElementById('teste').value).
    "&nome="+encodeURI(document.getElementById('nome').value).
    "&sobrenome="+encodeURI(document.getElementById('sobrenome').value).
    "&usuario="+encodeURI(document.getElementById('usuario').value).
    "&email="+encodeURI(document.getElementById('email').value).
    "&senha="+encodeURI(document.getElementById('senha').value);
}
  • 3

    Curiosity: why in a moment you use + to concatenate and in others you use the .?

  • 1

    You must use the + to concatenate.

  • 1

    You’re confusing PHP with Javascript?

  • As I said, I was testing, precisely the mistake was concanetation, had seen somewhere the use of . and I thought it would work, I appreciate the answers.

3 answers

0

In javascript the concatenation operator is +. Just replace the . by + as below.

function setarRegistro() {
   campos = "teste="+encodeURI(document.getElementById('teste').value)+
   "&nome="+encodeURI(document.getElementById('nome').value)+
   "&sobrenome="+encodeURI(document.getElementById('sobrenome').value)+
   "&usuario="+encodeURI(document.getElementById('usuario').value)+
   "&email="+encodeURI(document.getElementById('email').value)+
   "&senha="+encodeURI(document.getElementById('senha').value);
}

0

Answer

In php you can concatenate with "." in javascript you must use "+", your code is mixed.

function setarRegistro() {
    campos = "teste=" + encodeURI(document.getElementById('teste').value) +
    "&nome=" + encodeURI(document.getElementById('nome').value) +
    "&sobrenome=" + encodeURI(document.getElementById('sobrenome').value) +
    "&usuario=" + encodeURI(document.getElementById('usuario').value) +
    "&email=" + encodeURI(document.getElementById('email').value) +
    "&senha=" + encodeURI(document.getElementById('senha').value);
}

0


Use "+" to concatenate into Javascript, "." is used to access the property or function of an object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.