1

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

3
  • 1
    what errors are you getting?! Commented Oct 27, 2013 at 8:32
  • when the div clicked the jquery isn't work but if i copy that code into a html file is work normally Commented Oct 27, 2013 at 8:34
  • 1
    Thank you for wanting to mark your question as solved. However, we don't edit questions to do that here - we tick answers. So, either tick one of the below answers, or make your own and tick that. Thanks! Commented Oct 27, 2013 at 9:45

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

but i want to add switch case into <?php ..?> that change a title of web
So switch into <?php ... ?> only when you want to deal with something that has to be determined programatically.
0

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>

5 Comments

The browser doesn't care about line breaks in JS if you insert your semicolons correctly.
@ComFreek no, it doesn't; sometimes though, even if just for debugging, it's handy to do. (Plus the online tool adds them automatically).
is $titleVar in the switch case ?
this is example <?php include"db.php"; if(!isset($_GET['page'])) { header("location:index.php?page=home"); } switch($_GET['page']){ case "home": echo" <head><title>TITLE WEB</title> <link rel='stylesheet' href='css/style.css' type='text/css'> </head>
Ah, OK, since you're setting the header in the PHP, you'd need to include some before <HTML>. See edited answer.
0

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

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

become

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

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.