Question

I have a form in a page that submits to itself. In the PHP portion I check

if($_SERVER['REQUEST_METHOD'] == 'POST')

and then I run few things. I am attempting to show an alert using the Alertify suit if something was posted. But on page load I get error

alertify is not defined

I guess it only does that because the page did not load the include links to the js/css files yet (looking in FireFox's FireBug I see the Alertify script line loading before the <html> tag). What can I do to make it work right?

UPDATE CODE:

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo '<script type="text/javascript">alertify.alert("Hello");</script>';
    }
?>
<html>
    <head>
        <title>Hello There</title>
        <!--  Alertify Includes -->
        <script type="text/javascript" src="alertify.js"></script>
        <link rel="stylesheet" type="text/css" href="alertify.core.css">
        <link rel="stylesheet" type="text/css" href="alertify.default.css">        
    </head>
    <body onLoad="onLoad()">
        <form action="" method="POST" id="CONFIRM">                          
            <input  class="txt" value="" type="text" name="myName" id="myID" onKeyUp="" onChange="" onFocus="" />
        </form>
    </body>
</html>
Was it helpful?

Solution

After you posted the sources, I think I know what is wrong:

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo '<script type="text/javascript">alertify.alert("Hello");</script>';
    }
?>

is located above the script tag that loads your javascript. This means that any code in the PHP segment is executed before that code. In other words, alertify has not yet been defined there.

To resolve this, place a PHP echo inside the head, below the include for alertify, or include the alertify as a separate echo in the PHP, example:

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo '<script type="text/javascript" src="alertify.js"></script>'
    echo '<script type="text/javascript">alertify.alert("Hello");</script>';
    }
?>

OTHER TIPS

Make sure that you include the script tag for alertify in the of your HTML document.

<script src="PATH_TO_FILE/alertify.min.js"></script>

(Code snipped from alertify's main page)

If you have included a script tag, hit f12 and ctrl-f5 the page, make sure the path to the script is not reutrning a 404.

It is because truely, alertify wouldn't have been defined as at the time you called it, because the script file has not been loaded yet. Try putting the code alertify.alert("Hello");';} ?> after linking to the scripts. Put it below tag and it'll work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top