5

I try to understand why my jquery script doesn't work at all in my php included files. index.php:

<?PHP  
session_start();
include_once("./tools/globalFunction.php");
?>
<!doctype html>
<html lang="fr">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <title>Jojo Site</title>
    </head>
    <body>
        <?PHP 
        include_once("application.php");
        include_once("layout/menu.php");
        include_once("layout/loginModal.php");
        include_once("layout/container.php");
        include_once("layout/footer.php");  
        ?>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
        <script type="text/javascript" src="./editor/ckeditor/ckeditor.js"></script>
        <script type="text/javascript" src="./js/javaScriptGlobalFunction.js"></script>
    </body>
</html>

menu.php:

    <div class="collapse navbar-collapse justify-content-end" id="navbarNavConnexion">
        <ul class="navbar-nav">    
            <li id="adminButton" class="nav-item">
                <?php
                if (!$_SESSION['admin']){?>
                <button id="connexion" class="btn btn-dark" href="#" data-toggle="modal" data-target="#loginModal">Connexion</button> <?php ;}
                else {?>
                <button id="deconnexion" class="btn btn-dark" href="#" >Déconnexion</button> <?php ;}    
                ?>
            </li>
        </ul>
    </div>

javaScriptGlobalFunction.php:

$("#Connexion").click(function(){
    alert('connexion')
});
$("#Deconnexion").click( function() {
    alert('déconnexion')
});   

With those code lines it doesn't work whereas if i put the jquery script include and my script at the end of the menu file like :

    <div class="collapse navbar-collapse justify-content-end" id="navbarNavConnexion">
        <ul class="navbar-nav">    
            <li id="adminButton" class="nav-item">
                <?php
                if (!$_SESSION['admin']){?>
                <button id="connexion" class="btn btn-dark" href="#" data-toggle="modal" data-target="#loginModal">Connexion</button> <?php ;}
                else {?>
                <button class="btn btn-dark" href="#" id="deconnexion">Déconnexion</button> <?php ;}    
                ?>
            </li>
        </ul>
    </div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script type="text/javascript">
$("#deconnexion").click(function(){alert('deconnexion')});
</script>

It's run... Is there something i didn't seen well or i have to place jquery and script directly in each file?

5
  • Because the javascript in your include runs before you have included jquery (<script src=..jquery). You need to review the browser console for errors (unknown function / $ not defined) and check the rendered html. Commented Feb 8, 2019 at 9:13
  • Addition, Looks like you are trying to include javaScriptGlobalFunction.js, but judging by your post your file is named javaScriptGlobalFunction.php, when in really should be named javaScriptGlobalFunction.js A typo in the question? Or do they really have different extentions? Commented Feb 8, 2019 at 9:15
  • @EugeneAnisiutkin if it's a php "include" (via include_once) then it should end in .php - it's a php-parsed text file (in this case just containing javascript). If it ended in .js then it probably would be included with a <script src= tag. However, you are correct in that both javaScriptGlobalFunction.php (via include_once) and javaScriptGlobalFunction.js (via script src) are included - so it's not clear which it should be, but ultimately shouldn't matter (except that the .js script is after the jquery one, so would work...) Commented Feb 8, 2019 at 9:17
  • @freedomn-m, I am referring to this - <script type="text/javascript" src="./js/javaScriptGlobalFunction.js"></script>. And i see no include or include_once with javaScriptGlobalFunction.php in OP's code. Might have missed it Commented Feb 8, 2019 at 9:20
  • @EugeneAnisiutkin yes, spotted that after I started the comment - I updated my comment. Look at the first 3 lines of the first code block for the php include. Edit: a second look shows this is a different filename... the plot thickens :0 Commented Feb 8, 2019 at 9:21

2 Answers 2

1

It's probably not working because in javaScriptGlobalFunction.js you're trying to find elements by id with first letter in uppercase, while in html id is all lowercase. In javaScriptGlobalFunction.js change

$("#Connexion").click(function(){
    alert('connexion')
});
$("#Deconnexion").click( function() {
    alert('déconnexion')
});

to

$("#connexion").click(function(){
    alert('connexion');
});
$("#deconnexion").click(function() {
    alert('déconnexion');
});
Sign up to request clarification or add additional context in comments.

9 Comments

If this was the case, it wouldn't work when the file is included at the bottom (after the script include)
Sorry, didn't understand what you mean. Could you explain?
OP stated when he/she defines the scripts at the bottom of his/her page, everything works as is
@DarkBee In OP's last code example, when it's defined at the bottom of page, id is correctly lowercased
I'll retract my comment, I suspect I misinterpreted "if I put my script at the end" as putting the same script at the end, but looks like it is indeed a different script. I can confirm that IDs are case sensitive, so this is a potentially valid solution (though I still believe the initial issue is that $(function.. code appears before <script src=jquery) - so hard to tell with the question as it stands.
|
0

At first korrekt lower/higher key: $("#Deconnexion") != id="connexion"

Also your script will run befor your html is rendered by browser. Put it into

$(function () {
  $("#connexion").click(function(){
    alert('connexion');
  });
  $("#deconnexion").click(function() {
    alert('déconnexion');
  });
});

1 Comment

Doesn't matter if it's in doc.ready, $ still not defined if it's before <script src=..jquery

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.