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();
              } 
            }
          });

    });
  });
Community
  • 1
  • 1
Dan
  • 9,391
  • 5
  • 41
  • 73
  • Post the output of console.log(product_ids) pls – Hackerman Feb 18 '14 at 18:40
  • Hi, first, try add in your ajax dataType: 'json' after data: $('form').serialize(), – Netzach Feb 18 '14 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] – Dan Feb 18 '14 at 18:43
  • Also `console.log(data);`. – Rikesh Feb 18 '14 at 18:43
  • Thanks for all of the suggestions so far... JSON.stringify(data) produces error: Syntax error, unrecognized expression: [ – Dan Feb 18 '14 at 18:46
  • 1
    not sure why you would stringify it. and you don't need `product_ids = $.parseJSON(data);`, instead add the datatype as suggested previously. – Kevin B Feb 18 '14 at 18:48
  • what shows alert(data) ? – Netzach Feb 18 '14 at 18:50
  • I want to be able to parse each of the ids individually so that I can remove the associated inputs from the page. I was trying stringify based on a suggestion above (might have since been deleted)...alert(data) shows same thing as console.log(data) but without the brackets – Dan Feb 18 '14 at 18:50
  • 1
    Get rid of `product_ids = $.parseJSON(data)`. It's not needed and might be causing issues. –  Feb 18 '14 at 19:07

1 Answers1

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>
Netzach
  • 321
  • 2
  • 13
  • Thanks! Yes you also noticed that I was missing the identifier next to the data.hide() variable – Dan Feb 18 '14 at 19:19