law
law

Reputation: 47

How can I insert a jQuery plugin into PHP code?

I want to insert the jQuery plugin dropdown login form to make my website more pretty, but I am really confused where and how to insert jQuery into my code. For instance the following is my index.php:

<?php
    include ('book_fns.php');
    session_start();
    do_html_header("Welcome to Store");
    echo "<p>Please choose a category:</p>";

    // Get categories out of database
    $cat_array = get_categories();

    // Display as links to cat pages
    display_categories($cat_array);

    //If logged in as admin, show add, delete, edit cat links.
    if(isset($_SESSION['admin_user'])) {
        display_button("admin.php", "admin-menu", "Admin Menu");
    }
    do_html_footer();
?>

And I want to add the jQuery plugin to the top of my index page.

How do I insert this jQuery plugin?

Upvotes: 0

Views: 18670

Answers (3)

hungneox
hungneox

Reputation: 9829

I think you should echo the tag to your index page like this:

echo "<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>"

Or you include an HTML/PHP file that contains jQuery script in the <head></head> tag

include('headScript.php');

Or you write a function that echo the <script></script> tag above:

echo $this->headScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");

protected function headScript($src){
    return '<script type="text/javascript" src="$src"></script>';
}

Upvotes: 1

eciusr
eciusr

Reputation: 163

Yes, just treat the JavaScript and jQuery code as you would HTML. The server reads the PHP code and the PHP code tells the server what to send to the browser, and the browser is what reads the jQuery and HTML.

Upvotes: 0

Jhourlad Estrella
Jhourlad Estrella

Reputation: 3670

Get it straight from the CDN. Paste this code on your page's HEAD :

<HTML>
   <HEAD>    
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
   </HEAD>
</HTML>

Then utilize the library just as you would with your usual javascript code just like this one:

<script>
    $(document).ready(function() {
      alert('Hey! I was called when the document finished loading.');
    });
</script>

Upvotes: 3

Related Questions