Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery.post: how to make the variable data - [data] - dynamic

    Hi,
    I wonder if i can make the variable data which is [data] in jQuery.post( url, [data], [callback], [type] ) dynamic...

    for instance, this is the form i want to send,

    PHP Code:
    <form action="send_xml.php" method="post" enctype="multipart/form-data" id="form_send">
    <
    input type="checkbox" id="var_1" class="checkbox"/>
    <
    input type="checkbox" id="var_2" class="checkbox"/>
    </
    form
    this is the js code,

    Code:
    $("#form_send").submit(function(){
    	var path = $(this).attr('action');
    
    	$(".checkbox").each(function (b,a) {
        	if(a.id) window[a.id] = a.checked ? 1:"";
    	});
    		
    	$.post(path,{
    		var_1:var_1,
    		var_2:var_2
    		},function(xml){
    			process(xml);
    		});
    	return false;
    	});
    I managed to get advice from someone here to loop through the input list in the form with each() so that i dont have to declare variable for each input manually. it was,

    Code:
    if ($('#var_1').attr('checked')) {
    		var_1 = 1;
    		}else{
    		var_1 = '';
    		}
    	
    	if ($('#var_2).attr('checked')) {
    		var_2 = 1;
    		}else{
    		var_2 = '';
    		}
    each() has solved the issue perfectly,

    Code:
    $(".checkbox").each(function (b,a) {
        	if(a.id) window[a.id] = a.checked ? 1:"";
    	});
    but you can see that i still have to declare the data variables manually in the $.post -

    Code:
    var_1:var_1,
    var_2:var_2
    this is going to be a long list to insert manually, is that anyway to make it dynamic??

    many thanks,
    Lau
    Last edited by lauthiamkok; 08-21-2009 at 03:50 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •