javascript - PHP Ajax code unable to update database

525

I am trying to use ajax to update a database column (anert) in a table called items. I have copied the code below from another place in the website where this function is working. There, clicking the values in the drop down - Inert (0) or Anert (1) succeed in updating the database. When I render the current page, however, even though the db $item['anert'] value is currently set at 1 (or Anert) the drop down is defaulting to Inert (or 0). In addition, when I click the drop down's other value, it doesn't do anything.

I should also note that in the other case where this is working, it loops through an array, whereas here, I am only trying to update one value.

Please help. Thanks.


view:

<form action="<?php echo $this->form_action;?>">
  <table cellspacing="0" cellpadding="0" >
    <?php if($this->item) { ?>

  <tr id="<?php echo $item['id'];?>">
    <td align="center" >
      <?php if(in_array($item['anert'], array(0,1))) { ?>
        <select >
          <?php if($item['anert']) { ?>
            <option value="0"><?php echo $this->translate('Inert');?></option>
            <option selected value="1"><?php echo $this->translate('Anert');?></option>
          <?php } else { ?>
            <option selected value="0"><?php echo $this->translate('Inert');?></option>
            <option value="1"><?php echo $this->translate('Anert');?></option>
          <?php } ?>
        </select>
      <?php } else { ?>
        <?php echo $item['status'];?>
      <?php } ?>
    </td>
  </tr>

<?php } else { ?>
  <tr >
    <td colspan="12">
      <div ><span><?php echo $this->translate('Attention!');?></span><?php echo $this->translate('No records found ...');?></div>
    </td>
  </tr>
<?php } ?>
</table>
</form>

javascript (at bottom of view page):

<script type="text/javascript">

    $(document).ready(function(){
        $('.event-item-status').live('change', function() {
            var status = this.value;
            var id = $(this).parents('tr').attr('id');

            $.ajax({
                type: 'post',
                url: "?module=items&controller=index&action=changeInertia",
                data: 'id=' + id + '&status=' + status,
                beforeSend: function () {
                    $('#'+id).animate({
                        'backgroundColor': '#FFBFBF'
                    }, 400);
                },
                success: function (result) {
                    if(result == 'ok') {
                        $.get(window.location.href, function(data){
                            $('#'+id).html($(data).find('#'+id).html());
                            setTimeout(function(){
                                $("#"+id+"").animate({'backgroundColor': 'transparent'}, 400).find('.tooltip').simpletooltip();
                                deletePage();
                            }, 500);
                        });
                    } else {
                        alert(result);
                        $("#"+id+"").animate({'backgroundColor': 'transparent'}, 400);
                    }
                }
            });

        });

    });

</script>

controller

public function indexAction() {

    if( !WM_Users::allow($this->getRequest()->getModule(),$this->getRequest()->getController(), $this->getRequest()->getAction()) ) {
    $this->forward('admin', 'error', 'noPermission');
    }

    if($this->session->get('successfu_edite')) {
    $this->view->successfu_edite = true;
    $this->session->clear('successfu_edite');
    }
    if($this->session->get('error_permision')) {
    $this->view->error_permision = $this->session->get('error_permision');
    $this->session->clear('error_permision');
    }


    $ses = JO_Session::getAll();
    $user = $this->getUser();
    $cart = $this->getCart();
    $request = $this->getRequest();
    $item_id = $request->getParam('item_id');
    $item = Items_Model_Item::find($item_id);

    $this->view->form_action = WM_Router::create($request->getBaseUrl() . '?module=items&controller=index');
    $item['status'] = Items_Model_Item::itemStatuses($item['anert']);
    $this->view->item = $item;

}


public function changeInertiaAction() {
    $this->noViewRenderer(true);
    if( !WM_Users::allow($this->getRequest()->getModule(),  $this->getRequest()->getController(), $this->getRequest()->getAction()) ) {
    echo $this->translate('You do not have permission to this action');
    } else {
    $item = Items_Model_Item::find($this->getRequest()->getPost('id'));
    if($item) {
    $item->anert = $this->getRequest()->getPost('status');
    if($item->save()) {
    echo 'ok';
    } else {
    echo $item->getErrors();
    }
    } else {
    echo 'error: item not found';
    }
    }
}

model

public static function itemStatuses($status = null)
{
$data = array(
0 => WM_Translate::t('Inert'),
1 => WM_Translate::t('Anert'),
2 => WM_Translate::t('Expired'),
3 => WM_Translate::t('Sold out')
);
if ($status !== null) {
return isset($data[$status]) ? $data[$status] : null;
}
return $data;
}

People are also looking for solutions to the problem: How do I record JSON data to file using PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.