在Javascript中回声$

时间:2017-07-09 11:41:53

标签: javascript php

我正在尝试在这样的java脚本中回显一个字符串

<?php
function showMessage($hello=false){
  echo ($hello)?'hello':'bye';
}
?>

我遇到的挑战是我希望它回显一个指定的变量 echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('User registeration Complete \n User Number: $account_number\n First Name: $first_name\n Last Name: $last_name') window.location.href='index.html'; </SCRIPT>"); ,并在Javascript警告框上打印$ name。

2 个答案:

答案 0 :(得分:1)

试试这个:

echo"
<SCRIPT LANGUAGE='JavaScript'>
   window.alert('User registeration Complete \n User Number: ".$account_number."\n First Name: ".$first_name."\n Last Name: ".$last_name."')
   window.location.href='index.html';
</SCRIPT>
";
  

注意:脚本标记的语言属性已弃用,应为type="text/javascript"

答案 1 :(得分:0)

更改'"的序列:

echo ('
<SCRIPT LANGUAGE="JavaScript">
   window.alert("User registeration Complete \n User Number: '.$account_number.'\n First Name: '.$first_name.'\n Last Name: '.$last_name.'")
   window.location.href="index.html";
</SCRIPT>
');