1

I have a simple ajax request in my view page that i want to bind to an action in my controller :

function updateDates(id,val){

$.ajax({ 
            type: "POST", 
            url: "<?=APPLICATION_URL_ADMIN?>campaign/changedate", 
            data: ({id:id,duree:val}),                              
            cache: false,
            error: function(msg){
                alert('Il y a eu une erreur.');
            },
            success: function(){
                console.log('ok');
              window.location.href=window.location.href;
            }
      });


}

and in my controller :

public function changedateAction()
{

    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('changedate', 'html')->initContext(); 

    global $mySession;
    $db = new Db();

    $data = $this->_request->getPost(); 


    $data_update['campaign_length'] = $data['duree'];
    $data_update['launch_date'] = date("Y-m-d H:i:s");
    $condition = "campaign_id='" . $data['id'] . "' ";
    $db->modify(LAUNCHCAMPAIGN, $data_update, $condition);

}

Here's the request string rendered in the chrome console, which is fine : http://www.mydomain.ndd/admin/campaign/changedate?id=4&duree=45

It's throwing an error 500 and i don't know why. Can anyone help me ?

Edit:

I changed my action in the controller, but my db is still not updated

public function changedateAction()
{       

    global $mySession;
    $db = new Db();

    if ($this->getRequest()->isXmlHttpRequest()) {

        // makes disable renderer
        $this->_helper->viewRenderer->setNoRender();

        //makes disable layout
        $this->_helper->getHelper('layout')->disableLayout();

        if ($this->getRequest()->isPost()){ 

            $data = $this->_request->getPost();                 

            $data_update['campaign_length'] = $data['duree'];
            $data_update['launch_date'] = date("Y-m-d H:i:s");
            $condition = "campaign_id='" . $data['id'] . "' ";
            $db->modify(LAUNCHCAMPAIGN, $data_update, $condition);


            $this->view->text =  $data['duree'];.' processed';
        }
     }

}

Can somebody help me ? i'am stuck.

Thanks

Stéphane Joos
  • 743
  • 1
  • 6
  • 13
  • http code 500 is an indicator that something went wrong on the server. it could be a configuration error in the webserver setting, but usually it is an error in the php code (ZF returns 500 error if it encounters a recoverable error/exception). Inspect the response of your ajax call to see additional informations regarding the error. check for example http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php to learn about ways to get more meaningful error messages – cypherabe Feb 03 '16 at 13:39

2 Answers2

1

Your controller populates $data from POST via:

$data = $this->_request->getPost(); 

but your request passes the id and duree values via GET in the query-string:

http://www.mydomain.ndd/admin/campaign/changedate?id=4&duree=45

The result is that $data['id'] and $data['duree'] are not defined when you attempt to use them to build $data_update.

A var_dump/die of the $data should confirm that those keys are unset.

Instead, try pulling the querystring data using:

$request = $this->getRequest();
$id = $request->getParam('id');
$duree = $request()->getParam('duree');

Not tested, just a thought...

UPDATE

There is some inconsistency of request method in your description. Of course, the AJAX request you describe is POST and you seem to be passing the id and duree keys properly. My answer focused on your reference to the url:

http://www.mydomain.ndd/admin/campaign/changedate?id=4&duree=45

which clearly passes the data via GET.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
0

Your logic on the Controller side looks fine.

Assuming the controller you trying to read is called "campaign" , Try the following:

function updateDates(id,val){

   $.ajax({
        type: "POST",
        // DUE TO THE NATURE OF HOW ZEND WORKS YOU NEED ONLY NEED TO PROVIDE /CONTROLLER/ACTION
        url: "/campaign/changedate",

         // I USE THIS DATA TYPE WHEN SENDING AJAX REQUESTS IN ZEND
          dataType:  'json',

        // YOU DO NOT NEED THE () ONLY {}
        data: { 
            id:id,
            duree:val
        },
        cache: false,
        error: function(msg){ 
            alert('Il y a eu une erreur.');
        },
        success: function(){
            console.log('ok'); 

            // IF YOU TRYING TO RELOAD THE CURRENT PAGE USE THIS
            location.reload();
        } 
    });
}

add this at the end of your action so that it returns back to your ajax request;

echo Zend_Json_Encoder::encode(array(
                        'success' => true
                ));
exit;