0

I am building a form that submits via Ajax. If our database updates successfully, the function in the controller returns numerical ids, and I would like to use these ids to change the view based on the result.

I am having difficulty parsing the ids that are returned from the controller. They are returned from the controller here:

foreach($update_pending as $product){
    $success_ids[] = $product['product_id'];
}

if(isset($success_ids)){
    echo json_encode($success_ids);
}else{
    echo json_encode(array('error'=>'Error!'));
}

The ids are returned like this: [129818,129819,129820]

Now I want to parse this so that I can change the view based on which ids were submitted. I referred to this question JQuery Parsing JSON array but I am still missing something.

When I submit the form I get error "Cannot read property 'length' of null "

Here is the jQuery function (EDIT: I added dataType: 'json', no change in result):

$(function () {
    $('form').on('submit', function (event) {
        var product = $(this).attr("data-id");

        event.preventDefault();

          $.ajax({
            type: 'POST',
            url: 'category_typeahead',
            data: $('form').serialize(),
                dataType: 'json',
            success: function (data) {
              if(data.error == undefined){
                alert(data);
                product_ids = $.parseJSON(data);

                for (var i=0, len=product_ids.length; i < len; i++) {
                    $(product_ids[i]).hide();
                }

              }else{
                if($('.error_true').length==0){
                    $('#error').append('<div class="alert alert-error error_true">Error text</div>');   
                }
                $('#error').show();
              } 
            }
          });

    });
  });
9
  • Post the output of console.log(product_ids) pls Commented Feb 18, 2014 at 18:40
  • Hi, first, try add in your ajax dataType: 'json' after data: $('form').serialize(), Commented Feb 18, 2014 at 18:42
  • OK, just tried that but it did not change the result, the console.log(product_ids) returns "null". console.log(data) returns product_ids like this: [129826, 129827, 129828] Commented Feb 18, 2014 at 18:43
  • 1
    not sure why you would stringify it. and you don't need product_ids = $.parseJSON(data);, instead add the datatype as suggested previously. Commented Feb 18, 2014 at 18:48
  • 1
    Get rid of product_ids = $.parseJSON(data). It's not needed and might be causing issues. Commented Feb 18, 2014 at 19:07

1 Answer 1

1

Try with this, ur var product_ids is not necessary

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $('form').on('submit', function (event) {
        var product = $(this).attr("data-id");

        event.preventDefault();// using this page stop being refreshing 

          $.ajax({
            type: 'POST',
            url: 'category_typeahead',
            data: $('form').serialize(),
                dataType: 'json',
            success: function (data) {
              if(data.error == undefined){
                data.forEach(function(entry){
                    $('#'+entry).hide();
                  });

              }else{
                if($('.error_true').length==0){
                    $('#error').append('<div class="alert alert-error error_true">Error text</div>');   
                }
                $('#error').show();
              } 
            }
          });

    });
  });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Yes you also noticed that I was missing the identifier next to the data.hide() variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.