TeeJay
TeeJay

Reputation: 1613

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.

Upvotes: 0

Views: 165

Answers (3)

marlonp33
marlonp33

Reputation: 139

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.

Upvotes: 1

Yadav Chetan
Yadav Chetan

Reputation: 1894

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 } 

Upvotes: 0

Bere
Bere

Reputation: 1747

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 )

Upvotes: 1

Related Questions