PHP echo JS包含PHP变量

时间:2013-06-03 09:09:18

标签: php javascript echo

我有sendForm.php文件,这是使用PHPMailer发送表单的php。在标签之间有回声,我需要在标签之间使用PHP变量。这有可能吗?我知道这太结合了PHP和JS,但是我能做什么......我需要一个窗口弹出窗口,这也是我使用JS的原因。 echo本身仅打印到网页本身。

$totalSize = ($totalSize/(1024*1024));
$totalSize = round($totalSize,2);

if(!$mail->Send()) {
   echo "Error sending form! You are trying to send too large files. Their size is: ", $totalSize, " MB";
   echo '<script type="text/javascript">alert("Error sending form! You are trying to send too large files. Their size is: ??? ");</script>';
   }

如何在第二个回声中的那些问号的位置打印JS里面的$ totalSize变量? 谢谢你的帮助。我还是个初学者。

3 个答案:

答案 0 :(得分:1)

if(!$mail->Send()) {
   echo "Error sending form! You are trying to send too large files. Their size is: ". $totalSize. " MB";
   echo '<script type="text/javascript">alert("Error sending form! You are trying to send too large files. Their size is: '. $totalSize. '");</script>';
   }

//我纠正了第一个回声中的一些错误(用点替换逗号)

答案 1 :(得分:1)

PHP是服务器端语言,而JavaScript是客户端语言。 如果您尝试在服务器端验证表单而不重新加载页面(并让JavaScript弹出警告),请查看AJAX

答案 2 :(得分:0)

试试这个

  if(!$mail->Send()) {
    echo "Error sending form! You are trying to send too large files. Their size is: ".$totalSize." MB";
        ?>
<script type="text/javascript">
alert("Error sending form! You are trying to send too large files. Their size is: <?php echo $totalSize;?> ");
</script>
<?php }