I believe what you're trying to do is what serializeArray() function
in jQuery already does:
http://docs.jquery.com/Ajax/serializeArray

$("button.save").click(function(){
    var action = $(this).parent("form").attr("action");
    var postThis = $(this).parent("form").serializeArray();

    $.post(action, postThis, function(data){
        alert("done");
    });
    return false;
});


On Aug 12, 1:24 pm, cz231 <cz2...@gmail.com> wrote:
> Yeah sure...here's the code:
>
> $("button.save").click(function(){
>                 var action = $(this).parent("form").attr("action");
>
>                 var i = 0;
>                 var size = $(this).siblings("input, select").size();
>                 var nameArray = [];
>                 var valueArray = [];
>                 for (i=0;i<size;i++){
>                         nameArray[i] = $("[name]:eq("+i+")").attr("name");
>
>                         valueArray[i] = $("[name]:eq("+i+")").val();
>
>                 }
>
>                 var n = 5;
>                 for (n in nameArray){
>                         if(postThis == null){postThis = nameArray[0] + ': "' 
> + valueArray
> [0] +  '"'}
>                         if(n > 0){var postThis = postThis + ',' + 
> nameArray[n] + ': "' +
> valueArray[n] + '"';}
>                         //alert(n);
>                         //alert(nameArray[n]);
>                         //alert(valueArray[n]);
>                 }
>                 postThis = "{" + postThis + "}";
>                 alert(postThis);
>
>                 /*postThis = {Name:"Jimmy", Username:"Something",
> Password:"something", Email:"somethth...@someplace.com"};
>                 $.post(action, postThis, function(data) {
>                     alert('done');
>
>                 });*/
>
>                 $.post(action, postThis, function(data){
>                         alert("done");
>                 });
>                 return false;
>         });
>
> Thanks
>
> On Aug 12, 4:44 pm, James <james.gp....@gmail.com> wrote:
>
> > It's probably possible to not need to use it. But you're not revealing
> > much code to us it's difficult to help.
> > Can you show us how what you put in your variable 'postThis'? How do
> > you create this?
> > If you have postThis as a JSON object to begin with, you don't need
> > the json2.js file.
>
> > On Aug 12, 11:37 am, cz231 <cz2...@gmail.com> wrote:
>
> > > And there's no way to do it without adding another js file?
>
> > > I ask because for this project is pretty important to keep the number
> > > of requests down.
>
> > > On Aug 12, 3:43 pm, James <james.gp....@gmail.com> wrote:
>
> > > > The type you want is JSON (an object).
>
> > > > Include this Javascript file here:http://www.json.org/json2.js
>
> > > > Then use the JSON.parse() function which will convert a String to a
> > > > JSON object. The String has to have a format like a JSON object for it
> > > > to work properly.
>
> > > > var postData = JSON.parse(postThis);
> > > > $.post('somefile.php', postData, function(data) {
> > > >     alert('done');
>
> > > > });
>
> > > > On Aug 12, 9:28 am, cz231 <cz2...@gmail.com> wrote:
>
> > > > > I think I know the problem...My postThis variable ends up being just
> > > > > one big string. How do I convert it to the correct type? (I'm not even
> > > > > sure what type is correct)
>
> > > > > On Aug 12, 2:00 pm, James <james.gp....@gmail.com> wrote:
>
> > > > > > I don't see the problem...
>
> > > > > > Something like this should work:
>
> > > > > > var action = 'somepage.php';
> > > > > > var postThis = {Name:"Jimmy", Username:"Something",
> > > > > > Password:"something", Email:"someth...@someplace.com"};
> > > > > > $.post(action, postThis, function(data) {
> > > > > >     alert('done');
>
> > > > > > });
>
> > > > > > Otherwise, post your real code for us to see what's going on.
>
> > > > > > On Aug 12, 8:52 am, cz231 <cz2...@gmail.com> wrote:
>
> > > > > > > Oops. I'm sorry.
>
> > > > > > > action is the url to be posted to, and postThis is equal to: Name:
> > > > > > > "Jimmy", Username: "Something", Password:
> > > > > > > "something", Email: "someth...@someplace.com"
>
> > > > > > > On Aug 12, 12:51 pm, Jörn Zaefferer 
> > > > > > > <joern.zaeffe...@googlemail.com>
> > > > > > > wrote:
>
> > > > > > > > What values do the variables "action"  and postThis contain? You
> > > > > > > > describe them as "actions", isn't telling me anything.
>
> > > > > > > > Jörn
>
> > > > > > > > On Wed, Aug 12, 2009 at 7:40 PM, cz231<cz2...@gmail.com> wrote:
>
> > > > > > > > > Hi,
>
> > > > > > > > > I'm using AJAX to submit a form. I'm using the POST method. 
> > > > > > > > > Example:
>
> > > > > > > > > $.post(action, postThis);
>
> > > > > > > > > Both action and postThis are actions. Action is the URL and 
> > > > > > > > > postThis
> > > > > > > > > is the data to be submitted. Right now, this isn't working. I 
> > > > > > > > > know I
> > > > > > > > > can pass the action variable because that has always been 
> > > > > > > > > working. But
> > > > > > > > > how do I put the parameters there as a variable? It will work 
> > > > > > > > > if I
> > > > > > > > > express the parameters like this:
>
> > > > > > > > > $.post(action, {Name: "Jimmy", Username: "Something", 
> > > > > > > > > Password:
> > > > > > > > > "something", Email: "someth...@someplace.com" });
>
> > > > > > > > > Any help would be greatly appreciated.
>
>

Reply via email to