Topic: function called but no return

I'm a newbie, I don't know Javascript at all, I just wanted to do a function without having to reload the page.

So, I learned from helloworld.php in the examples folder. It was enough for what I want to do.
But I don't understand why helloworld works when my page doesn't.

My function does two thing:
1) a mysql query -> I see my table is updated when I click, so we're sure the server got into the function.
2) put $text ('OK') in the div -> That's the problem, it doesn't happen.

This is the error I got when I use Firebug.
"uncaught exception: [Exception... "'[object Object]' when calling method: [nsIDOMEventListener::handleEvent]"  nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)"  location: "JS frame :: chrome://firebug/content/net/spy.js :: callPageHandler :: line 807"  data: no]
http://www.targetingnow.com/dispatcher.js
Line 1"

Can you help defining the problem? This error is in chinese for me. I don't get why it doesn't happen in helloworld why I changed the code to be exactly the same.
Also, is it normal that xajax doesn't work with IE 9 ? Do I have to do something?

Re: function called but no return

Hello,

Could you post your code please?
I cant make much out of that error either but it doesn't look like an xajax error.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: function called but no return

First, I did not use xajax->printJavascript because it wasn't working and I did not know why. I just copied the <script></script> from the source code of helloworld and change stuff.
This is what I have in my index between head tags (note that I have done the same for helloworld [not using printJavascript] and it works, is it a bad idea?):

<script type="text/javascript" charset="UTF-8">
/* <![CDATA[ */
try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };
xajax.config.requestURI = "http://localhost/IT2/index.php?page=watch&v=27";
xajax.config.statusMessages = false;
xajax.config.waitCursor = true;
xajax.config.version = "xajax 0.5";
xajax.config.legacy = false;
xajax.config.defaultMode = "asynchronous";
xajax.config.defaultMethod = "POST";
/* ]]> */

</script>
<script type="text/javascript" src="xajax/xajax_js/xajax_core.js" charset="UTF-8"></script>
<script type="text/javascript" charset="UTF-8">
/* <![CDATA[ */
window.setTimeout(
 function() {
  var scriptExists = false;
  try { if (xajax.isLoaded) scriptExists = true; }
  catch (e) {}
  if (!scriptExists) {
   alert("Error: the xajax Javascript component could not be included. Perhaps the URL is incorrect?\nURL: xajax/xajax_js/xajax_core.js");
  }
 }, 2000);
/* ]]> */
</script>
<script type='text/javascript' charset='UTF-8'>
/* <![CDATA[ */
xajax_favoris = function() { return xajax.request( { xjxfun: 'favoris' }, { parameters: arguments } ); };
/* ]]> */
</script>

This is some of my humble php code.

@require ('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('javascript URI', 'xajax/');
                    
function favoris($user,$fav,$add)
{
        mysql_query("UPDATE users SET fav_id = '$fav' WHERE user_id = $user");
        mysql_close();
                        
        /*if ($add)
        {
            $text = 'OK';
        }
        else
        {    
            $text = 'NOT OK';
        }*/

                // I was going to test if we add or remove so I could write either adding done or removing done but even a simple $text='ok'; doesn't work.
                        
        $text='ok';
                            
                            
        $objResponse = new xajaxResponse();
        $objResponse->assign('favoris', 'innerHTML', $text);
                        
        return $objResponse;
}
                    
        $reqFav =& $xajax->registerFunction('favoris');
        $xajax->processRequest();
        if(!$add)
        {
                        unset($fav[$pos]);
                        $fav_id = implode(',',$fav);
                        
                        $reqFav->setParameter(0, XAJAX_JS_VALUE, $user_id);
                        $reqFav->setParameter(1, XAJAX_JS_VALUE, $fav_id);
                        $reqFav->setParameter(2, XAJAX_JS_VALUE, $add);
                        $xajax->processRequest();
                        
                        //again I did not use $reqFav->printScript, because $fav_id is a string and it was writing (1,2,3,4,5) in the code instead of (1,'2,3,4',5). So I wrote it manually, is it a bad idea?
                                                  echo '<div id="favoris"><p class="btn success" onclick="xajax_favoris('.$user_id.',\''.$fav_id.'\','.$add.')">Supprimer des favoris</p></div>';
        }
        else
        {
                        $fav[$z] = $v;
                        $fav_id = implode(',',$fav);
                        
                        $reqFav->setParameter(0, XAJAX_JS_VALUE, $user_id);
                        $reqFav->setParameter(1, XAJAX_JS_VALUE, $fav_id);
                        $reqFav->setParameter(2, XAJAX_JS_VALUE, $add);
                        $xajax->processRequest();
                        
                        echo '<div id="favoris"><p class="btn default" onclick="xajax_favoris('.$user_id.',\''.$fav_id.'\','.$add.')">Ajouter aux favoris</p></div>';
        }

Do you need more infos?

Re: function called but no return

Well, you need to put a call to print_javascript() in the head of your page:

<?php $xajax->printJavascript() ?>

The other way would be to do something like this:

//In your php code
$xajaxjs = $xajax->getJavascript(); //Loads the js into the variable

//between your head tags
<$php echo $xajaxjs ?>

To just try to copy it is not good...

Additionally, the code after your call to processRequest() will not get processed after the page load. I do not understand why the code after processRequest() is there.

The processRequest function is the xajax engine and it intercepts the XHR request from the browser, interprets the request and calls the appropriate function. The called function returns an instance of the response bject and process request interprets it and returns to the browser.

I probably need to see the html as well.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: function called but no return

I wrote in the top of my page:

<?php
@require ('../xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('javascript URI', '../');
?>

Then, between <head>

<?php $xajax->printJavascript(); ?>

But now, it's not working anymore. I guess it's because now I have to correct the code inside <body>.
Can you help me with that? This is obviously not in my level, though. I'm considering giving up the idea but it sucks to reload the page while a video is playing...

Basicly, what I want to do is a "favorite" button, when they click on it, there a modification in the database + change the button afterwards.
I changed my code and this is what's inside <body>

<?php
function favoris($user,$fav,$add)
{
    
    //let's say there's a sql request here.
    
    if($add) //if we add
    {
        $text='Added to your favorites.';
    }
    else
    {
        $text='Removed from your favorites.';
    }    
    
    $objResponse = new xajaxResponse();
    $objResponse->assign('favoris', 'innerHTML', $text); //So we put $text in <div id="favoris"></div> right?
    
    return $objResponse;
}

/* These values are for the test */
$user_id = 1;
$add = 1;
$fav_id = '0,9,5,4,27';

$reqFav =& $xajax->registerFunction('favoris');
$reqFav->setParameter(0, XAJAX_JS_VALUE, $user_id);
$reqFav->setParameter(1, XAJAX_QUOTED_VALUE, $fav_id);
$reqFav->setParameter(2, XAJAX_JS_VALUE, $add);
$xajax->processRequest();

if($add)
{
    $t='Add to favorites';
}
else
{
    $t='Remove from favorites';
}
    
?>

<div id="favoris"><button onclick='<?php $reqFav->printScript(); ?>'><?php echo $t; ?></button></div>
/* the source code have
<div id="favoris"><button onclick='xajax_favoris(1, "0,9,5,4,27", 1)'>Add to favorites</button></div>
so it worked
*/ 

But when I run firebug, it says : xajax_favoris undefined, why?
Isn't $reqFav =& $xajax->registerFunction('favoris') doing it?

Re: function called but no return

Here's a sample. You may need to change the xajax include and javascript URI.
See it works for you...

<?php
/**
* 2012-01-19 Ed
* Simple xajax demo page
*
*/
include './xajax/xajax_core/xajax.inc.php';
$xajax = new xajax();
$xajax->configure('javascript URI','./xajax/');

function favoris($user,$fav,$add)
{
    //let's say there's a sql request here. OK
    if($add) //if we add
    {
        $text='Added to your favorites.';
    }
    else
    {
        $text='Removed from your favorites.';
    }    
    
    $objResponse = new xajaxResponse();
    $objResponse->assign('favoris2', 'innerHTML', $text); //So we put $text in <div id="favoris"></div> right?
   
    return $objResponse;
}

$xajax->register(XAJAX_FUNCTION,'favoris');
$xajax->processRequest();
?>
<!DOCTYPE html>
<html>
<head>
<title>Xajax Sample</title>
<?php $xajax->printJavascript() ?>
</head>
<body>
<div id="favoris">
<button onclick='xajax_favoris(1,"0,9,5,4,27",1);'>Add Favorite</button>
<button onclick='xajax_favoris(1,"0,9,5,4,27",0);'>Remove Favorite</button>
</div>
<div id="favoris2"></div>
</body>
</html>

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

7 (edited by ins-man 2012-01-21 8:09:24 AM)

Re: function called but no return

Hello, sorry for the late reply and thanks again for your help. It works now!
Do you know why it was not working? There was space before the <?php ?>...

Thank you, anyway! This is an awesome feature.
I really need to learn more about it.