Mamen
Mamen

Reputation: 1436

How to add JQuery into PHP file

I have a problem to add JQuery in file this is my code:

        <html>
        <head>

        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
        </script>

        <script> 


 $(document).ready(function(){
      $('#k1').click(function(){
            $('#k2').animate({marginLeft:'-50px'});
        $('#k3').animate({marginLeft:'-450px'});
        $('#k4').animate({marginLeft:'-450'});

      });



    });
        </script> 
            </head>
                <body>
                <div class='wrapper'>
                    <div class='mainKotak'>
                        <div class='wKotak'>

                                <div class='kotak' id='k1' ></div>

                        </div>
                    </div>
                </div>
                </body>
                 </html>

the code above is in

<?php 
echo" ";
?>

the jquery isn't work , please someone help me because i'm newbie in jquery thanks

SOLVED i'm just use jQuery with external javascript

Upvotes: 1

Views: 45910

Answers (3)

Imam Mubin
Imam Mubin

Reputation: 294

You need to add quotes character (') on css attribute and write correctly

$('#k2').animate({marginLeft:'-50px'});

become

$('#k2').animate({'margin-left':'-50px'});

Upvotes: 0

brandonscript
brandonscript

Reputation: 72855

If I'm understanding you correctly, you need to echo out that jquery code via php? You can do this easily by escaping out any double quotes or backslashes (" , \) with a backslash. Since your code doesn't appear to have either, you should be fine.

You may also find that because javascript is particular about line breaks sometimes, adding the white-space line break character to the end of your echo is beneficial: \n

e.g.

echo "<html>\n";
echo "<head>\n";
echo "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>\n";
echo "</script>\n";
echo "<script>\n";

There is an online tool for easily wrapping raw HTML in php echo that may help you too: http://www.andrewdavidson.com/convert-html-to-php/

If it still doesn't work after doing that, you likely have a syntax error. Check the source of your php-generated code as well -- compare it to the code you've pasted above to ensure they match up.

@Sanjay's answer above also makes a lot more sense if you can do it that way instead. You can switch between php easily:

<?php 

include"db.php"; 
if(!isset($_GET['page'])) 
{ 
    header("location:index.php?page=home"); 
} 
switch($_GET['page'])
{
     case "home": $title = "TITLE WEB";
}
?>

<html>
<head>
<title><?php echo $titleVar; ?></title>
<link rel='stylesheet' href='css/style.css' type='text/css'>
<body>

...

<php echo "Other content"; ?>

</body>
</html>

Upvotes: 0

Sanjay
Sanjay

Reputation: 2008

don't use echo like that. You can write anything outside <?php ... ?>, in simple HTML

Upvotes: 3

Related Questions