0

When I use the function .load(), it will load the page that I want to load in the div that is selected.

But when I want to use JavaScript/jQuery that I already have coded in other files, it wont work anymore.

It does work when I just include ''; the page static in the div.

Does anyone have an idea why this works like this?

Static:

<div id="loginCont" class="modal-body">
    <?php
    include 'login.php';
    ?>
</div>

by Javascript/jQuery

$("#memberFalse").on("click", function() {
    $('#loginCont').load("/213server/_inc/login.php");
});
14
  • May be path not taking correctly. .load("/213server/_inc/login.php"); Commented May 17, 2016 at 14:02
  • when building the page do both the selectors get their respective elements correctly? try log their size. Commented May 17, 2016 at 14:08
  • 1
    does #memberFalse exists before you want to add the clickhandler? where is the js located?Is it in $(document).ready(.. or after the #memberFalse element Commented May 17, 2016 at 14:11
  • 1
    Remember that include() starts from the root of your server, and load() starts from the root of your WEB server. Commented May 17, 2016 at 16:26
  • 2
    php include that starts with "/" refers to the root of the server. Load() starting with "/" starts at the root of the web server. Javascript (or jQuery) has no access outside the web root. Commented May 18, 2016 at 14:19

1 Answer 1

1

A PHP include that starts with "/" refers to the root of the server, unless specified otherwise in your php.ini file. Your code:

<div id="loginCont" class="modal-body">
  <?php
  include 'login.php';
  ?>
</div>

fetches "login.php" from the folder where the current page is located. You can display that location with:

<?php echo __DIR__  ?>

Your jQuery load() method should fetch the page "login.php" from that location too. There is obviously a mistake in the following path:

$('#loginCont').load("/213server/_inc/login.php");

Remember that Javascript/jQuery CANNOT fetch files outside the web root, but PHP can. For example, if your login.php file is located at "/var/_inc/login.php" and your web root is at "/var/www/", jQuery will never have access to login.php. If your login.php file is located at "/var/www/myproject/_inc/login.php", then your load() function should fetch it like this:

$('#loginCont').load("/myproject/_inc/login.php");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.