Concatenation php string to javascript string - Stack Overflow

I have some question about concatenation php string to javascript string ... for example: php variable

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 badges
Add a ment  | 

4 Answers 4

Reset to default 6

alert('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'; ?>";
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742413153a4439218.html

相关推荐

  • Concatenation php string to javascript string - Stack Overflow

    I have some question about concatenation php string to javascript string ... for example: php variable

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信