简体   繁体   中英

javascript code inside php echo

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.

更改为以下内容:

<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>';

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;"
...
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM