admin管理员组文章数量:1336632
I have some question about concatenation php string to javascript string ...
for example:
php variable
$man = "Jamse";
and have javascript function
<script>
if (document.getElementById("fname").value == "") {
q = false;
msg = <?php echo 'Please fill first name'.$formErrors['fname'].'\n' ?>;
}
</script>
i want to do something like this can anyone help me ?
I have some question about concatenation php string to javascript string ...
for example:
php variable
$man = "Jamse";
and have javascript function
<script>
if (document.getElementById("fname").value == "") {
q = false;
msg = <?php echo 'Please fill first name'.$formErrors['fname'].'\n' ?>;
}
</script>
i want to do something like this can anyone help me ?
Share Improve this question edited Mar 21, 2014 at 15:42 VostanAzatyan asked Mar 21, 2014 at 15:37 VostanAzatyanVostanAzatyan 6472 gold badges9 silver badges23 bronze badges4 Answers
Reset to default 6alert('my name is: <?php echo $man; ?>' );
alert('my name is: <?= $man; ?>');
Since PHP will insert $man
on the server side, it's not a separate string that must be bined by JS. All the browser will see is
alert('my name is: Jamse');
Why not write it all in php?
<?php
$man = "Jamse";
echo "<script>
function alertMyName() {
alert('my name is:" . $man . "');
}
</script>";
?>
The other answers are true, I just forgot to write quotes and javascript did not understand it was a String and gave me an error. The correct code:
if (document.getElementById("fname").value == "") {
q = false;
msg = "<?php echo 'Please fill first name'.$formErrors['fname'].'\n'; ?>";
}
本文标签: Concatenation php string to javascript stringStack Overflow
版权声明:本文标题:Concatenation php string to javascript string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742413153a2470192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论