0

Alrighty,

I have an error module which is basically just a styled div that has a call to a PHP function for printing out errors.

It works perfectly when I include it like this:

<?php if(!empty($errors)){
include 'includes/modules/error.mod.php';           
 }else if(!empty($success)){
include 'includes/modules/success.mod.php';
 }?>

But I am now trying to make it flashier with jQuery and so I'm including it with a jQuery function:

function errorModule(){
$.get('includes/modules/error.mod.php', function(data){
        var errorModule = $(data).hide();
        errorModule.appendTo($('#navMain')).show('blind', 1500);
     });
}

Now, it works most of the way. The error module appears like I want it too. However the PHP function that once worked perfectly is now 'undefined.'

Why?

EDIT: Contents of error.mod.php

<div id="errorModule">
<p class="h2">The Following Errors Have Occured:</p>
<?php
    echo outputArray($errors);      
?>
</div>
7
  • 1
    What's the content of includes/modules/error.mod.php? Commented Jun 10, 2013 at 2:21
  • Updated with error.mod.php contents. :) Commented Jun 10, 2013 at 2:33
  • How does the PHP parser know about $errors when the file is loaded with $.get()? Commented Jun 10, 2013 at 2:33
  • Why would I append the error module to itself? I'm placing it below the navigation bar of my site so that it looks as if it's sliding down from it. Commented Jun 10, 2013 at 2:33
  • 1
    The orignal, served page's PHP environment is irretrievable deep history by the time $.get() requests the error.mod.php. PHP scripts written as includes can't have a life of their own if they depend on being included server-side! Commented Jun 10, 2013 at 2:46

1 Answer 1

2

An AJAX call (the purpose of $.get) is not like an include, it is a separate web request to the URL provided. So in this case, what is being fetched by $.get is the same as what will load if you point your browser at includes/modules/error.mod.php - a good way of debugging AJAX is to do exactly that.

Based on the file name, I suspect this is not what you intended, and that this file is not meant to be loaded on its own.

To use AJAX, what you would need is a new PHP page, say ajax_message_output.php, which performs the necessary processing to output the "module" onto the page. Note that this page won't have any of the data from the "parent" page - so, for instance, $errors will not have the same value in your new ajax_message_output.php that it had on the main page.

If all you're trying to do is display the errors from the main page load, but make them look "flashier", AJAX is not the technology you want. To make them slide down, just have them initially hidden (using <div id="messages" style="display: none;">...</div> or similar) and have $('#messages').show('blind', 1500) run when the page loads.

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

4 Comments

which performs the necessary processing, whatever that might be the key element to the answer lies within a simple sentence.
Thank you for the reply! I'm not sure I'm following completely. I'm still fairly inexperienced at a lot of this. I could use a little more clarity. Also, is this the efficient way for me to achieve my goal or should I be employing a different method of displaying this module?
@Nifty62 I've expanded the answer based on your edits to the question
Hmmm, ok. I have some ideas. I will do some experimenting based on your suggestions and let you know. I really appreciate the help guys!

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.