0

I'm trying to make another ajax call when one is executed successfully to pass the post variable to another controller action. However, it is returning null when I check the console log message. I'm not sure why.

Here is my code:

jQuery:

$('#modify-store-name').on('change', function() {
  $.ajax({
     type: "POST",
     url: "/user/get-one-store",
     dataType: "json",
     data: {
          store_name: $(this).val()
     }
  }).done(function (msg) {
      $.each(msg, function (i) {
          $('#modify-store-label').attr('style', '');
          $('#modify-store-desc').attr('style', '');
          $('#modify-store-category-label').attr('style', '');
          $('#modify-store-category').attr('style', '');
          $('.upload-btn-wrapper').attr('style', '');

          $('#modify-store-desc').val(msg[i].store_description);
          $('#modify-store-category').html($("<option />").val(msg[i].store_category).text(msg[i].store_category));

          $('#msubmit').attr('disabled', false);
     });

     $.ajax({
        type: "POST",
        url: "/user/modify-store",
        dataType: "json",
        data: {
            store_name2: $('#modify-store-name').val() // why is this sending a null value
        }
     }).done(function(msg) {
        console.log(msg);
     }).fail(function(msg) {
        console.log(msg);
     });
   }).fail(function (msg) {
     $("#msg").html(msg.failure);
   });
});

and my php code:

public function getonestoreAction()
{
    $layout = $this->layout();
    $layout->setTerminal(true);

    $view_model = new ViewModel();
    $view_model->setTerminal(true);

    try {
        $store_name = $this->params()->fromPost('store_name');

        echo  json_encode($this->getUserService()->getAStore($store_name));
    } catch (\Exception $e) {
        echo json_encode(array('failure' => $e->getMessage()));
    }

    return $view_model;
}

public function modifystoreAction()
{
    $layout = $this->layout();
    $layout->setTerminal(true);

    $view_model = new ViewModel();
    $view_model->setTerminal(true);

    if ($this->getRequest()->isPost()) {
        try {
            $store_name = $this->params()->fromPost('store-name2');
            echo json_encode($store_name); // returning null
            $mstore_name = $this->params()->fromPost('modify-store-name');
            $mstore_description = $this->params()->fromPost('modify-store-description');
            $mstore_category = $this->params()->fromPost('modify-store-category');
            $mstore_image = $this->params()->fromFiles('modify-store-image');

            if (count($mstore_image) > 0) {
                if ($this->getUserService()->modifyStore($store_name, array('store_name' => $mstore_name, 'store_description' => $mstore_description, 'store_category' => $mstore_category, 'store_image' => $mstore_image, 'tmp_name' => $mstore_image['tmp_name']))) {
                    echo json_encode(array('success' => 'Store was modified successfully.'));
                }
            }
        } catch (\Exception $e) {
            echo json_encode(array('failure' => $e->getMessage()));
        }
    }

    return $view_model;
}

I read that you can make two ajax calls like this but I'm not sure why one is not passing the store name via post.

Any help would be appreciated

Thanks!

user2101411
  • 1,204
  • 2
  • 14
  • 33

0 Answers0