Page 1 of 1
Capture an array of post data
Posted: Tue Sep 25, 2012 3:08 am
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
Re: Capture an array of post data
Posted: Tue Sep 25, 2012 5:37 am
by Celauran
Why not just
serialize the array and let your PHP script get the values as usual?
Re: Capture an array of post data
Posted: Tue Sep 25, 2012 4:10 pm
by bob_the _builder
Just new to the jquery side of things.. and not really sure how to write the function fully..
Re: Capture an array of post data
Posted: Tue Sep 25, 2012 5:58 pm
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.
Re: Capture an array of post data
Posted: Wed Sep 26, 2012 5:04 pm
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>';