views:

50

answers:

3

Hi there,

I have an index.php that includes config.php. Now I'm using $.get() when I click a button to load a form:

$.get('form.php', function (form) {
    $(form).insertAfter();
});

This form requires config.php for some input values; however, when the form has loaded, I receive the php error:

Notice: Undefined variable: config in form.php on line 27

Line 27 of form.php: if ($config['spam_protect']) {

I realize the config.php has already been instantiated when the page initially loads, thus I suspect this to be the problem I'm having. I've tried including config.php within the form.php file to no avail. Tips or tricks anyone?

Thanks!

+1  A: 

Notice: Undefined variable: config in form.php on line 27

The message is quite clear: $config isn't loaded, maybe because your config file doesn't load correctly.

Note that if you make an Ajax request, the script requested in that request is an entirely new PHP instance. You may need to include config.php in that one as well.

Pekka
Could you have a peak at my reply on vassilis comment, same answer you gave. Thanks.
Ryan
A: 

Either form.php doesn't include config.php, or config.php doesn't declare $config

adam
A: 

You should be including config.php in form.php if it's needed.

Calling it through ajax has nothing to do with index.php, it's run independently.

That being said, we need some more info on this one.

vassilis
Understandable. I did this successfully now, however it (`form.php`) requires a class which is already being declared and required in `index.php` so yet another error.
Ryan
@Ryan then you need to change the structure of your script. The AJAX request you make is completely independent, as @vassilis says. You'll need to move your classes to a central location and incude them from there
Pekka
Thanks Pekka, will do.
Ryan
@Pekka is right, but you can go around this by using `if class_exists('your_class')` to avoid including it the second time.
vassilis