0

This is my functional onClick script in HTML and it works perfectly:

<input type="text" id="t" name="t" value="1">
<a href="testar.php?action=" onclick="window.location=this.href+'&quant='+document.getElementById('t').value;return false;">Click</a>

What i want to achieve is this:

echo '<input type="text" value="1" id="t" name=""/>
<a href="addCarrinho.php?id_produto='.$registo['id'].'&nome='.$registo['nome'].'&preco='.$registo['preco'].'" onclick="window.location=this.href+'&quant='+document.getElementById('t').value;return false;">Add</a>';

All php works, i am receiving php variables on the other page but the onClick javaScript is not working, im getting a sintaxe error. I think its a quotes problem but im not familiarized with javaScript.

1
  • The syntax highlighting in your question shows you the problem - escape the single quotes there using a backslash Commented Sep 9, 2015 at 4:01

3 Answers 3

2

Change to the following:

<a href="addCarrinho.php?id_produto='.$registo['id'].'&nome='.$registo['nome'].'&preco='.$registo['preco'].'" onclick="window.location=this.href+\'&quant=\'+document.getElementById(\'t\').value;return false;">Add</a>';
Sign up to request clarification or add additional context in comments.

Comments

1

You need to escape the single quotes in the last line like following:

...
onclick="window.location=this.href+\'&quant=\'+document.getElementById(\'t\').value;return false;"
...

Comments

0
echo '<input type="text" value="1" id="t" name=""/>
<a href="addCarrinho.php?id_produto='.$registo["id"].'&nome='.$registo["nome"].'&preco='.$registo["preco"].'" onclick="window.location=this.href+'&quant='+document.getElementById("t").value;return false;">Add</a>';

Try with this way, I did some single and double quote change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.