0

Looking for a way to call a php function that has its include 'functions.php' in the original file.

This is what is in my index.php file, so when i click on a link it will load (new.php) into the (#container) div.

<?php include 'functions.php' ?>

<script type="text/javascript">
$(document).ready(function() {
  $('a').click(function(e) {
    $('#container').load(new.php).hide().fadeIn(500);
    return false;
  });
});
</script>

So in my new.php, i want to call a function that is located in functions.php, but obviously i get an undefined error.

<?php some_func() ?>

I know you can just put another include 'functions.php' inside the new.php

but since the functions.php contains mysql connections/queries and other includes etc.. i dont really want to keep reloading it.

so is there any way i can reload the functions.php into the newly loaded div and call functions from it?

3
  • 2
    place that <?php include 'functions.php' ?> into the new.php file and try again. also make sure the paths to the files are correct. Commented Dec 14, 2012 at 13:43
  • yes i am aware of that way to go about it.. but i was looking for another way to avoid reloading 'functions.php' everytime. thanks anyways. Commented Dec 14, 2012 at 14:15
  • If You use require_once or include_once the functions.php will be included only once (for the first time it is needed) and won't be reloaded everytime ;-) Commented Dec 14, 2012 at 14:36

1 Answer 1

1

When you "load" new.php inside your div, you're only loading the resulting HTML code into the div.

The resulting HTML code, in that case, will be the result of the PHP processing of new.php's, code, which depends on functions inside functions.php.

In that case, acessing the content of a file using javascript, there is absolutely no way to do that but placing include 'functions.php'; inside new.php.

Both files will be processed individually. new.php will not run "inside" index.php and therefore will not be able to access whatever functions are defined there.

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

1 Comment

hmm.. thanks for the info. i get all the logic and reasons behind why it wont work, i was hoping there was some way e.g. using jquery to load it in or something any ideas? thanks for help.

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.