We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

IsPost | Forum

Marcus
Marcus Nov 17 '19
Hi folks is OW::getRequest()->isPost() same as $_Post???? 
Paul
Paul Nov 17 '19
No. isPost is just checking the REQUEST_METHOD and if it is a post request
Marcus
Marcus Nov 17 '19
So is $_POST ain't that right 
The Forum post is edited by Marcus Nov 17 '19
Paul
Paul Nov 17 '19
$_POST get a array of variables. 

OW::getRequest()->isPost() just a true or false

Marcus
Marcus Nov 17 '19
OK I got it now. Thanks so much. I can't use is valid post function case I'm fetching data with Ajax not using ow form. 
dave Leader
dave Nov 18 '19
Use this at the top of your php file that is called by ajax


       //only allow ajax requests 

      if( !OW::getRequest()->isAjax() ) 

      {        

         throw new Redirect404Exception();

         exit; 

       } 

 

So it means if this is NOT a ajax request then redirect to the 404exception 



The Forum post is edited by dave Nov 18 '19
Marcus
Marcus Nov 19 '19
Thanks gave I saw this statement but somehow didn't use it in stead I'm passing with each Ajax call parameter like let say controller:whatever. Then over at the ctrl class I check wheater that parameter is present in $_POST array.


if($_POST['controller']) != 'whatever') 
Ebenezer Obasi
Ebenezer Obasi Nov 27 '19
Marcus, were you able to resolve what you are working on? Why do you need to check for controller? If you are looking to work with different methods within the same class, in addition to Dave's comments, you can do the following:


//magic method to handle ajax requests

public function ajaxResponder( )

{

    //only allow ajax requests

    if ( !OW::getRequest()->isAjax() )

    {

        throw new Redirect404Exception();

        //there is no need to exit; request is already handled by another controller

        //exit();

    }


    //check if ajax function is specified

    if ( isset($_POST['ajaxFunc']) )

    {

        $callFunc = (string)$_POST['ajaxFunc'];


        //call ajax function within the class

        $result = call_user_func(array($this, $callFunc), $_POST);

    }

    else

    {

        //throw error 404 if ajax function is not specified

        throw new Redirect404Exception();

    }


    //print ajax response

    header('Content-Type: application/json');

    exit(json_encode($result));

}


//test ajax function. You can create as many function as you like and call them the same way we will call this function.

public function test()

{

    return "hello, test";

}


From you JavaScript, you can proceed to do this:


//submit request to our test function and log response

$.post('yourbestite.com

/pluginkey/controller/ajax-responder', {ajaxFunc:'test'}, function(response){

    console.log(response);

});

The Forum post is edited by Ebenezer Obasi Nov 27 '19