0

I have a PHP code:

  if($billing_total>$limit_to_send){

    echo '<script type="text/javascript">
    window.onload = function() {
    alert("Sorry, you do not have enough credit");
 }
   </script>';

When I am printing this message, it is being printed at the beginning of the PHP page as below:

  <script type="text/javascript">
                window.onload = function() {
                alert("Sorry, you do not have enough credit");
             }
               </script>

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        -----------------------------------------------------------

This caused the header (logo) of my page in the browser to move down one line.

and the page will look very bad because all the items there will be moved down one line.

I hope it is clear to you. Please any solution ?

===========================================================================

Thanks for All ...

Solution:

$alert_message=<script type="text/javascript">
    window.onload = function() {
    alert("Sorry, you do not have enough credit");
 }
   </script>

Printing $alert_message somewhere in the HTML code before the body tag ^_^

6
  • 3
    Try to move it into the <head> tag Commented Mar 12, 2012 at 8:40
  • How, I am just printing in PHP , how to control the place of printing in the HTML page ? Commented Mar 12, 2012 at 8:44
  • there is a closing curly bracket missing. Commented Mar 12, 2012 at 8:46
  • All of the output is generated somewhere in your script, or one of its parents/children (includes). You'll have to either move the snippet to a different context so it will be in the appropriate location, or store the output somewhere so it can be written to output at a later time. Commented Mar 12, 2012 at 8:48
  • looks like you are using some PHP framework, where you have to use the frameworks method for returning text. Commented Mar 12, 2012 at 8:49

3 Answers 3

2

Make sure you never output anything before the DTD (doctype declaration).

See this question for more information...

The doctype declaration must be the first element of your html page, it's from what the browser decides how to handle the rest of the html code. Outputting anything before that will probably put your browser in quirks mode so you can't be sure how the browser will render your page.


How to avoid this?

The echo command gets executed as its line is reached, and it seems that the rest of your html code follows after that.

You could either

  • move the html DTD and header to the top of your php (but sometimes that is not possible) OR
  • store the error html in a variable, so instead of echo '<script ... do $errorhtml = '<script ... and output that string, if not empty, at a specific place in the head or body generating code of your php.
  • If you have no control over the original source, you could consider redirecting to an error page with its own html DTD, header and body which you can design as fits you best.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martin, I can't control the printing of the javascript code, It is coming up there before the doctype declaration.Any idea how to control this?
Thank you Martin, I have defined $alert_message="<script type="text/javascript"> window.onload = function() { alert("Sorry, you do not have enough credit"); } </script> " and printed it somewhere before <body>
1

Either append die() into the if codeblock or have your php print the script somewhere in the body or head.

1 Comment

Place the php code in a portion of the php after <head> has been echoed/printed/displayed.
1

This shows a bad design of your application. I would suggest you change it to something like:

$errors = array();
if($billing_total>$limit_to_send){
  $errors[] = 'Sorry, you do not have enough credit'; 
}

Then on your HTML file, before the <body> tag closes, read your array and display any errors

<?php if(is_array($errors)): ?>
  <script type="text/javascript">
   <?php foreach($errors as $error): ?>         
        alert('<?php echo $error; ?>');
   <?php endforeach; ?>
  </script>
<?php endif; ?>

Comments

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.