0

How do I make this work? I'm trying to do AJAX post. i'm use to doing .serialize but i want to add two more values and keys to the array. how can I do this easily?

        $('#moreprojects').click(function(){
            var skip = $(this).attr('name');
            var more = $(this).attr('rel');
            var param = $('#companies').serializeArray();
            param.push({name: 'skip', value: skip})
            param.push({name: 'more', value: more})
            $.post('projectsmore.php', {param}, function(response){
                $('#projects tbody').append(response);
            })  
        })
Clay Smith
  • 189
  • 1
  • 5
  • 14
  • view answer @ http://stackoverflow.com/questions/4449695/adding-push-values-to-ajax-post-in-jquery-serialize-or-serializearray – Book Of Zeus Aug 24 '11 at 02:05

2 Answers2

1

The way you add the values should be fine. But your call to $.post should be:

$.post('projectsmore.php', param, function(...

(no {} around param).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • i tried alert(param) and it just returns object Object object Object etc. it's not working with the php. – Clay Smith Aug 24 '11 at 02:10
  • *I tried alert(param) and it just returns object Object object Object etc*: That is to be expected, as you have an array of *objects*. When you use `alert`, the argument is converted to a string and the default string representation for an object is `[object Object]`. `alert` is **not** for debugging, use `console.log` instead. The JS by itself is correct. Maybe the error is in your PHP script. – Felix Kling Aug 24 '11 at 08:05
0

You're experiencing problems because of the way that you're injecting the param variable into the $.post. Because the variable param is already an object you don't need to wrap it with brackets.

So instead of:

$.post('projectsmore.php', {param},

it should be:

$.post('projectsmore.php', param,