简体   繁体   中英

PHP echo JS which includes PHP variable

I have sendForm.php file, which is php using PHPMailer for sending form. In between tags there is echo and I need to use PHP variable in between tags. Is that possible somehow? I know this is too much of combining PHP and JS, but what can I do... I need a window pop-up, that's why I use also JS. The echo itself prints only to the webpage itself.

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

How can I print $totalSize variable inside of the JS in the place of those question marks in the 2nd echo? Thanks for your help. I'm still a beginner.

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

//I corrected some errors in the first echo (replace comma with dot )

PHP is a server side language , while JavaScript is a client side language . If you are trying to validate your forms on the server side without reloading the page (and let JavaScript pop up a warning), look into 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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM