Topic: avoid double invocation of a page

Hi all, it's the first time I post here, today I went trough an issue that take me some effort.
I wrote a php page (let say index.php) that fulfill different kind o actions depending on the value of 'action' parameter (i.e. index.php?action=new does something different than index.php?action=edit). This page use xajax capabilities to list items the user can search. My main trouble is that my xajax_listItems() method that is invoked as soon as the page is loaded cause the index.php page to be invoked again. As you may figure when I invoke index.php?action=new I don't want that action to be performed again becouse new objects will be created in the db.

The only answer I found is to detect which method is used by the request:

Code: PHP

if ($_SERVER['REQUEST_METHOD']=='GET'){...} else {...}

 

in facts i noticed that while I'm invoking index.php with get method xajax will use post.

I hope that someone will find this post usefull. Maybe someone can suggest a better answer.

Last edited by marco.gonnelli (2010-03-11 4:43:06 PM)

Re: avoid double invocation of a page

Hi marco,

This sounds rather strange. Could you post the offending code?

Ed

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

Re: avoid double invocation of a page

Ok I'll try to extract and translate it from italian and I'll post.

Last edited by marco.gonnelli (2010-03-12 9:13:05 AM)

Re: avoid double invocation of a page

A lot of cut, some paste and some translation. I tried to be as concise as possible.

Code: PHP

-- top.php

<html>

<head>

<?php

if (isset($needXajax)&&$needXajax) $xajax->printJavascript();

?>

</head>

<body>



-- menu.php

<a href='index.php?action=new' >new</a><br>

<a href='index.php?action=edit' >edit</a><br>



-- bottom.php

<?php

?>

</body>

</html>



-- wellcome.php

<br>hi<br>



-- form.php

    <form method=post>

        message:<div id='message'> </div>

    </form>

<script>

function init(){

xajax_myfunction();

}

window.onload = init

</script>



-- form.xajax.php

<?php

$xajax = new xajax();

$xajax->configure('debug', true);

$xajax->configure('javascript URI', '.');

$xajax->registerFunction('myfunction');

//-------------------------------------------------------------------------

function myfunction() {

    $result="result";

    $objResponse = new xajaxResponse();

    $objResponse->assign('message', 'innerHTML', $result);

    return $objResponse;

}

//-------------------------------------------------------------------------

$xajax->processRequest();

?>



-- index.php

<?php

require_once 'xajax_core/xajax.inc.php';

session_start();

ob_start();

$action=$_REQUEST['action'];

if ($action=="new") {

    $fd = fopen('example.log', "a");

    fwrite($fd, "something\n");

    fclose($fd);

    $content="form";



/*    

    if ($_SERVER['REQUEST_METHOD']=='GET'){

        //does

        $fd = fopen('example.log', "a");

        fwrite($fd, "something\n");

        fclose($fd);

        $content="form";

    } else {

        //doesn't

        //$fd = fopen('example.log', "a");

        //fwrite($fd, "something\n");

        //fclose($fd);

        $content="form";

    }

*/





} elseif ($action=="edit") {

        $fd = fopen('example.log', "r");

        $message = fread($fd, filesize('example.log'));

        fclose($fd);

        $content="form";

} else {

    $content='wellcome';

}



if (file_exists($content.".xajax.php")) {

    $needXajax=true;

    include $content.".xajax.php";

}

include 'top.php';

if (file_exists($content.".php")) {

    include $content.".php";

}

include 'menu.php';

include 'bottom.php';

ob_end_flush();    

?>

 

You should see that the invocation of index.php?action=new will cause two lines to be written in example.log
My original code has been replace with the commented out one.

I needed to act this way because in my project this behaviour was causing double insertion of records in my DB.
Thank you very much for your attention

Marco

Last edited by marco.gonnelli (2010-03-12 10:43:08 AM)

Re: avoid double invocation of a page

Marcos,

I'm not sure I follow your process but, if you need to differentiate the calls for xajax or not then try

Code: PHP

 $xajax->canProcessRequest()

It returns true if this is an xajax call otherwise false.

Ed

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

Re: avoid double invocation of a page

Hi Ed,
thank you so much, you're definitely right. I wasn't aware of the existance of this method, btw, i looked it up in the API ( http://xajaxproject.org/en/docs-tutoria … php/xajax/ ) but I didn't find it. I could find something about it only via the search function of the documentation. Probably I need a deeper insight of xajax.

Marco

Re: avoid double invocation of a page

Marco,

It's in xajax.inc.php at around line 455.

Ed

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