views:

52

answers:

2

hey all

i am trying to aggregate form elements into object and then send it via ajax here is the code that i start using but i cant figure out how to do the rest

$('.jcart').live('submit', function() {

});

Update 1:

html form

http://pasite.org/code/572

Update 2:

I have successfully submit the form using ajax but it still refreshes the page after submiting

this what i did

function adding(form){
$( "form.jcart" ).livequery('submit', function() {var b=$(this).find('input[name=<?php echo $jcart['item_id']?>]').val();var c=$(this).find('input[name=<?php echo $jcart['item_price']?>]').val();var d=$(this).find('input[name=<?php echo $jcart['item_name']?>]').val();var e=$(this).find('input[name=<?php echo $jcart['item_qty']?>]').val();var f=$(this).find('input[name=<?php echo $jcart['item_add']?>]').val();$.post('<?php echo $jcart['path'];?>jcart-relay.php',{"<?php echo $jcart['item_id']?>":b,"<?php echo $jcart['item_price']?>":c,"<?php echo $jcart['item_name']?>":d,"<?php echo $jcart['item_qty']?>":e,"<?php echo $jcart['item_add']?>":f}                                            
});
 return false;                                                  
}
A: 

Depending on how many of the values you need (and whether you have things like radio buttons) you can start with the :input selector to grab the elements. Assuming .jcart is your form or container, something like this:

var data = {};
$('.jcart').find(':input').each(function (i, field) {
  if ($(field).is('input:checkbox') {
    if (field.checked) { 
      data[field.name] = true; 
    } else {
      data[field.name] = false;
    }
  } else {
    data[field.name] = $(field).val();
  }
});

That should get you started.

g.d.d.c
+3  A: 

jQuery has a method called .serialize() that can take all the form elements and put them into an array for just what you are trying to do. Without seeing your html, we really can't tell you much more though.

http://api.jquery.com/serialize/

Something like this might work:

$('.jcart').submit(function() {
   $.ajax({
      url : form.php,
      type : "POST",
      data : $(this).serialize(),
   });
});

Obviously it would need a little more for full functionality, but that should get you started.

Rookwood
@Rookwood i have update the question or i have add a link for my html form
Mahmoud
That link doesn't really help much. I have no idea what you're pulling from the database. Can you update it with the actual output?
Rookwood
can you visit this link secure.sabayafrah.com `username = mahmud password = mahmud` there you can see a live demo
Mahmoud
You have some seriously malformed html there. I assume that is all being generated by jcart, but you don't even have an opening form tag in there. I would look over your settings and make sure everything is right there first, but until you have a proper form, what you are trying to do is going to be nigh impossible.
Rookwood
this malformed form was created because i combined jcart to work with lightbox which is working but it keeps on refreshing the page, that was my problem at the beginning when i asked for consultation they informed me i had to use ajax to disallow page refreshment so what do u suggest on doing?
Mahmoud
@Rookwood i have updated the question
Mahmoud