PHP echo JS,其中包含PHP变量


PHP echo JS which includes PHP variable

我有sendForm.php文件,这是php使用PHPMailer发送表单。在标签之间有echo,我需要在标签之间使用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>';
   }

我怎么能打印$totalSize变量在JS内部的地方在那些问号在第二次echo?谢谢你的帮助。我还是个初学者。

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

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

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

try this

  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 }