www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Getting an 'is undefined' error when working with php

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    resolved [RESOLVED] Getting an 'is undefined' error when working with php

    I've written a javascript function that checks for a php session. When there is no session, it works okay. But when there is a session, I get an "undefined" javascript error.

    This is my js in question. I call sessionCheck() in body onLoad. If there is no session, I will get my alert of "it is null"
    Code:
    			function getUser() {
    				return "<?php getSessionUser(); ?>";
    			}
    	
    			function sessionCheck() {
    				var ssuser = eval(getUser());
    				if (ssuser == null) {
    					alert('it is null');
    				} else {
    					alert(ssuser);
    				}
    			}
    After creating the session and receiving the error, I view source to make sure that my php executed properly, and it did. The session var I'm pulling over in this case has a value of "admin" and it shows up in getUser() correctly, but I receive no alert and instead get the " 'admin' is undefined " error.
    Code:
    			function getUser() {
    				return "admin";
    			}
    	
    			function sessionCheck() {
    				var ssuser = eval(getUser());
    				if (ssuser == null) {
    					alert('it is null');
    				} else {
    					alert(ssuser);
    				}
    			}
    Any idea what could be causing the error? Thanks for your help.

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    NULL is not the same thing as ""; you are always going to return a value, whether it is "admin" or "". Remove the eval() from around the getUser() function call, I don't think you need it.

    Is this inside the head of a PHP page? Or is it in a remote .js file?

  3. #3
    Join Date
    Dec 2006
    Posts
    2
    Thank you WolfShade. I have a habit of trying to use null everywhere and in order to use it here, I had to use eval. This was my error. I simply changed to this and all is well.
    Code:
    			function getUser() {
    				return "<?php getSessionUser(); ?>";
    			}
    	
    			function sessionCheck() {
    				var ssuser = getUser();
    				if (ssuser == "") {
    					alert('it is null');
    				} else {
    					alert(ssuser);
    				}
    			}
    This was inside of the head of a .php, but there was no actual php code in it except that line, I included another php file that has my php functions inside of it. Thanks again!

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Glad I could help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles