ricmetal Posted November 24, 2011 Share Posted November 24, 2011 hi guys i am including a function in my main script, and i'm calling it aftwerwards. except i cannot echo successfully the data in the $result variable, why? function echoResult(){ $result = 'dan'; return $result; } // 1 echo echoResult(); // don't work. i get empty data. // 2 $fnResult = echoResult(); echo $fnResult; // don't work. get empty data. i have ran get_included_files and the include is being made successfully. i read on php.net and these are the examples they use (user comments!) how do i get the data out of the function and echo it, using this way? Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/ Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 Please post full code. If you just run your posted code, it should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290851 Share on other sites More sharing options...
ricmetal Posted November 24, 2011 Author Share Posted November 24, 2011 hi the code is lengthy and spread out through several pages...i cannot post it all. is there any test i can make to check *anything* out as to why i cannot get the variables from the function? Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290854 Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 Where is function echoResult defined and where are you calling it from? You need to be willing to provide more information if you expect helpful responses. Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290856 Share on other sites More sharing options...
ricmetal Posted November 24, 2011 Author Share Posted November 24, 2011 echoResult() is defined in a php file (containing nothing more than the function). this php file is included in another php file that is called in an ajax call, made with jQuery's ajax method. the latter php includes the php with the echoResult() function, and tries to get the variable's data $result from within echoResult() and should echo it. the echoed data is then alerted through jQuery's success function, as follows //javascript ... function getData(){ // this is called with a simple jQuery click method on a href $.ajax({ type:'POST', url:'../site/ajaxrequests/mainFile.php', // this file, when called will include the file where the echoResult function is defined dataType:'text', data:{ comment:comment }, success:function(data){ alert(data); // this should alert 'dan' return false; ... // php from the main file include '../phpincludes/functions/echoResult.php'; // this file appears listed in get_included_files() echo echoResult(); // php for the echoResult fn in the echoResult php file function echoResult(){ $result = 'dan'; return $result; } the only thing i can think of is the amount of functions inside functions, but should't they all bubble up to the top function? nothing is set to global or anything. p.s. the javascript side should not have any problem with receiving and alerting the data, ive used the same script many times before. Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290859 Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 ricmetal, that information changes a lot of things. You have a lot of things working together here, so what you need to do is: Run the "PHP Main File" and make sure that it is outputting the result to the browser. If it is, then you have a problem in your jQuery function. If it isn't, then you need to check that your include path is correct, and that you have no errors in any of your files. You'll need to turn error reporting on to see that. Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290862 Share on other sites More sharing options...
ricmetal Posted November 24, 2011 Author Share Posted November 24, 2011 solved it. long story short, wrong paths. :/ thanks for the tips teynon regardos Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290870 Share on other sites More sharing options...
ricmetal Posted November 24, 2011 Author Share Posted November 24, 2011 solved Quote Link to comment https://forums.phpfreaks.com/topic/251702-trouble-with-include-function-return-and-echo/#findComment-1290987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.