0

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.

5
  • 4
    just as you already did in the first echo ? Commented Jun 3, 2013 at 9:10
  • echo '<script type="text/javascript">alert("Error sending form! You are trying to send too large files. Their size is: ', $totalSize, '");</script>';, No ? Commented Jun 3, 2013 at 9:11
  • Well, I have already tried and it seems to break the script somehow - the windows just don't pop up after adding this. Commented Jun 3, 2013 at 9:15
  • change Their size is: ??? ") to Their size is: $totalSize ") Commented Jun 3, 2013 at 9:32
  • I had to replace it with '. $totalSize.' Thanks anyway :) Commented Jun 3, 2013 at 9:34

3 Answers 3

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

There are no errors in the first statement, using , within echo is valid to output a variable number of parameters.
The comma is not used to concatenate strings, that's what the dot is for. Simply the echo function accepts a variable number of parameters so you can list them separated by comma. It is a very common practice to use it like that as you are avoiding an useless operation (the concatenation)
I understand and @mishu and FDL.
Thank you, this works nice :) btw how is it with ' and " ? Is there an order how to use them properly?
yes there is '' is string literal which is not parsed by php, "" is parsed by php that is why you can use php variables there. More on php.net/manual/en/language.types.string.php
|
1

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.

4 Comments

Found out that this can be done also with javascript. stackoverflow.com/questions/16932907/…
Sure it can, but your question was if it is possible to combine JavaScript with PHP, not to solve the particular problem. If you're having trouble understanding the relation between the two, check: programmers.stackexchange.com/questions/171203/…
Yes it was, sorry, I was thinking about more problems in the time I was writing this comment to you. However, you were thinking forward as I was according to your reply. Never mind, I still got you +1 for expanding my horizon :)
No propblem! It's not about me getting the reputation, it's about getting the right answer to the right question
0

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 } 

3 Comments

Thank you. However, I can't divide the PHP because of an instance of PHPMailer ( $mail = new PHPMailer(); ) working with my whole file.
you can divide it may doesn't make any difference in mailer :)
try it once for your knowledge and also for my knowledge :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.