Capture an array of post data

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Capture an array of post data

Post by bob_the _builder »

Hey,

What the correct format to capture an array of posted data..


Code: Select all

$("#trans-process").live('click',function(){	

	var data = {utotal: $('#utotal').val(), total: $('#total').val(), method: $('#method').val(), firstname: $('#firstname').val(),  };
		$("#shop").html('<center><br /><br /><img src="./loading.gif"></center>');	
		$.post("includes/cart-trans-process.php", data, function(response){
		$("#shop").html(response);
		},'text');
}); 
Thats what I have, but have also tried utotal: $('#utotal[]').val(),

The data is being sent via <input id="utotal" name="utotal[]" type="text" size="8"> etc..

Thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Capture an array of post data

Post by Celauran »

Why not just serialize the array and let your PHP script get the values as usual?
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Re: Capture an array of post data

Post by bob_the _builder »

Just new to the jquery side of things.. and not really sure how to write the function fully..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Capture an array of post data

Post by requinix »

You've given your fields an ID but you can't do that. IDs are unique. If you do a #utotal then you'll only get one.

Celauran pointed out the function you should be using - one specifically made for this exact situation.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Re: Capture an array of post data

Post by bob_the _builder »

This is what I have so far.. but the the form isnt linking to the jquery function, the entire page is just reloading..

Code: Select all

$('#myForm').submit(function() { 
        var data = $('#myForm').serialize(); 
 	$("#shop").html('<center><br /><br /><img src="./loading.gif"></center>');	
		$.post("includes/cart-trans-process.php", data, function(response){
		$("#shop").html(response);
		},'text');
}); 
Form:

Code: Select all

	echo '<form name="myForm" id="myForm">';
     
    $sql = mysql_query("SELECT FROM trans ORDER BY firstname ASC"); 
    	while($row = mysql_fetch_array($sql)) { 
    		foreach($row as $key=>$value){ 
    			$$key = ValidateOutput($value); 
    }
    
	echo '<tr> 
    <p><a href="includes/cart-trans-delete.php?transid='.$transid.'" id="cart-trans-delete">x</a> | '.$firstname.' '.$lastname.' - <b>$'.$total.'</b></p>
    <p><input id="utotal" name="utotal[]" type="text" size="8"></p>
    <p><input type="hidden" id="method" name="method[]" value="'.$method.'"></p> 
    <p><input type="hidden" id="firstname" name="firstname[]" value="'.$firstname.'"></p>
    <p><input type="hidden" id="lastname" name="lastname[]" value="'.$lastname.'"></p>
    <p><input type="hidden" id="email" name="email[]" value="'.$email.'"></p>
    <p><input type="hidden" id="total" name="total[]" value="'.$total.'"></p>
	<p><input type="hidden" id="transid" name="transid[]" value="'.$transid.'"></p>
	<input type="submit" id="myForm" value="Submit">';
	
    }
	
	echo '</form>';
Post Reply