Jump to content

trouble with include, function, return and echo


ricmetal
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.