Welcome Guest, Not a member yet? Register   Sign In
jQuery Kills PHP Variables
#1

[eluser]spamguy[/eluser]
This is an odd one.

Let's say I have a page consisting of jQuery UI tabs. One of the tabs fetches a PHP doc that looks like this:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;View&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?= $myvar ?&gt;
    &lt;/body&gt;
&lt;/html&gt;

$myvar is set in the appropriate controller:

Code:
function view()
        {
            $data['myvar'] = 1234;
            
                $this -> load -> view('view', $data);
        }

When view.php is viewed by itself (that is, not through jQuery tabs), $myvar prints properly. When it's viewed as an AJAX tab, though, I get a PHP error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: myvar

Filename: confession/view.php

What can I do to fix or circumvent this?

Thanks!
#2

[eluser]Randy Casburn[/eluser]
Hey,

If this is a view partial, have you tried removing the <!DOCTYPE>?

Randy
#3

[eluser]spamguy[/eluser]
Gave that a try. No go.
#4

[eluser]Randy Casburn[/eluser]
Yea - that was stupid anyway.

Try:

Code:
$this->load->vars($data);
$this -> load -> view('view');

Let's let CI do the work differently for us. I've had this problem with XMLhttpRequest (AJAX) before. I just can't recall the specifics. (and I'm too tired to look).

Randy
#5

[eluser]spamguy[/eluser]
Nope. Thanks for trying, though.
#6

[eluser]Colin Williams[/eluser]
Is that really the full controller, or have you simplified it for the sake of making the example more clear? I guess I have a hunch that something else is causing it.. something we cannot see because you omitted it.

Also, right after the line that sets $data['myvar'], do [color="green"]print $data['myvar'][/color]. Repeat this until you can potentially spot an area of logic where $data['myvar'] is not set properly.
#7

[eluser]Randy Casburn[/eluser]
That was my thought initially too Colin. When I had this problem with AJAX I thought I recalled it was because I was setting the $data variable in the __contstruct() method and calling the setter() method directly that used $this->data. As spamguy described, when called with an HTTP request it would work fine, but with an XMLHttpRequest it wouldn't. I ran up against a deadline, moved the variable to the setter() method for $data and it worked.

That supports your hypothesis. I'd be curious to know "the rest of the story" as Paul Harvey says.

Randy
#8

[eluser]spamguy[/eluser]
[quote author="Randy Casburn" date="1225051506"]That was my thought initially too Colin. When I had this problem with AJAX I thought I recalled it was because I was setting the $data variable in the __contstruct() method and calling the setter() method directly that used $this->data. As spamguy described, when called with an HTTP request it would work fine, but with an XMLHttpRequest it wouldn't. I ran up against a deadline, moved the variable to the setter() method for $data and it worked.

That supports your hypothesis. I'd be curious to know "the rest of the story" as Paul Harvey says.

Randy[/quote]

Sorry for the delay in getting back to this question.

You both are technically right: there is more code than I previously showed. However, I commented it all out and tested before posting it here.

As I double-checked my work just now, the solution to this mess hit me. And it's shamefully simple. I have two controllers that use the same private function: 'view' calls it once, 'random' calls it 10 times. 'random' has its own jQuery tab; 'view' does not. Trouble is, $data['myval'] was defined in 'view' but not in 'random'. So for months -- literally months! -- I had myself believing that jQuery was causing PHP variables to die, since undefined variables only happened when jQuery was around.

D'oh. Big Grin
#9

[eluser]Colin Williams[/eluser]
Good to hear! It's always something shamefully simple
#10

[eluser]Randy Casburn[/eluser]
Cool. That's probably what I did too <<sheepish grin>> but never really found out.

Glad you got it figured out.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB