简体   繁体   中英

How to use JQuery and PHP?

I am creating a PHP App and am using JQuery but have run into problems. For example, below is the code for a page i am writing, the alert() function calls when the pages loads but the second javascript Jquery code does not, I cannot see why one line works but the second does not?

help.php

<?php
    echo "<script>";
        echo "alert(\"Loaded\");";   
        echo "$(\"#newDiv\").draggable().resizable();";
    echo "</script>";
    echo "<div id=\"newDiv\">";
    echo "</div>";
?>

The page calls the alert and creates the div but does not execute the jquery statement.

I think this may be due to have my page is structures and loaded. pages are loaded inside a div using Jquery and AJAX so the only page the user actually "loads" is an index.php page like so:

index.php

<html>
<head>
    <title>BluePrintr Web Application.</title>
    <link rel="stylesheet" type="text/css" href="public.css" />
    <script type="text/javascript" language="javascript" src="jquery.js"></script>
    <script type="text/javascript" language="javascript" src="myLibs.js"></script>
</head>
<body>
<?php
    echo "<div id=\"web_Page\">";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Header\">";
            require("public/templates/header.php");
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Menu\" class=\"horizontalcssmenu\">";
            require("public/templates/menu.php");
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Content\">";
        echo "</div>";
        //////////////////////////////////////////////////////
        echo "<div id=\"web_Footer\">";
            require("public/templates/footer.php");
        echo "</div>";
    echo "</div>";
?>

The menu loads and then any page selected from this menu is then loaded into the "#web_Content" div. So when the help.php page is selected from the menu, it injects the script into the div.

Can anyone see why the javascript code will not execute on help.php?

You need to wrap the jquery to initialize only after the page loads

<?php
    echo "<script type='text/javascript'>";
        echo "alert(\"Loaded\");";  
        echo "$(function(){$(\"#newDiv\").draggable().resizable();});";
    echo "</script>";
    echo "<div id=\"newDiv\">";
    echo "</div>";
?>`

The $(function(){}); will cause the code to wait until after the page has been completely loaded.. I'm assuming you are also loading jQuery UI as you are using .draggable() ?

Do this:

<script type='javascript'>
    $(document).ready (function() {
        alert('Hi');
        // Other initialization
    });
</script>

Also, just write all of that as HTML. Use PHP like this:

<html>
    <body>
        <?php if ($var == true) {?>
            <p>Hello</p>
        <?php } ?>
    </body>
</html>

If $var is false, the page is blank. This makes things much more readable.

your javascript may be executing before the page has finished rendering in the browser have a look at the jquery ready function for examples

http://api.jquery.com/ready/

As mentioned, you need to wrap your jQuery code inside $(document).ready().

You should stop doing it the way you are now and use a templating system, with a proper MVC architecture for serving your pages. It's just gonna be hell for you if you keep doing it this way. You should never have PHP outputting javascript code, this should be written by you in seperate files in a functional way and then included in your HTML file.

There is another good way like below:

$alert = <<<LABEL
<script>
    $(document).ready (function() {
        alert('Hi');
        // Other initialization
    });
</script>
LABEL;

echo $alert;

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