i do loop foreach:

<?php  $myrows = $wpdb->get_results( "SELECT * FROM wp_myprojects" ); ?>
<form  id="aad-form2" action="" method="POST">
<?php foreach($myrows as $a): ?>

<input id="id" type="text" name="id" value="<?php echo $a->id; ?>">
<input id="name" type="text" name="name" value="<?php echo $a->name; ?>">
<input id="header" type="text" name="header" value="<?php echo $a->header; ?>">
<input id="body" type="text" name="body" value="<?php echo $a->body; ?>">
<input id="urls" type="text" name="urls" value="<?php echo $a->urls; ?>">
<input type="submit" name="aad-submit2" id="aad_submit2" class="button- primary"value="<?php _e('Update', 'aad'); ?>"/>
<img src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" class="waiting" id="aad_loading2" style="display:none;"/>
<br />

<?php endforeach ?>

And my jquery code:

jQuery(document).ready(function($) {
$('#aad_submit2').attr('disabled', true);
data2 = {
action: 'aad_get_results2',
form_data2: $('#aad-form2').serialize()
//aad_nonce: aad_vars.aad_nonce

$.post(ajaxurl, data2, function (response) {
$('#aad_submit2').attr('disabled', false);
return false;

And when i do action only last form is update in data base. Example i have 5 records in database. Foreach print me 5 forms, but when i click submit button record 1 or record 2 nothing happen.

Only last record from loop is update. Help

Tell me why? Probaly i don't now jquery saw good :) Help me.

My update function in plugin:

function aad_process2_ajax() {
global $wpdb;
parse_str($_POST['form_data2'], $form_data2);
echo $id = $form_data2['id'];
echo $name = $form_data2['name'];
echo $header = $form_data2['header'];
echo $body = $form_data2['body'];
echo $urls = $form_data2['urls'];
$wpdb->update('wp_myprojects', array('id'=>$id, 'name'=>$name, 'header'=>$header, 'body'=>$body, 'urls'=>$urls), array('id'=>$id));
  • please could you post also aad_get_results2 action in your functions.php?
    – iEmanuele
    Commented Aug 21, 2013 at 16:20
  • 1
    Ok, add too question.
    – user33913
    Commented Aug 21, 2013 at 16:23

1 Answer 1


Each set of form fields has the same set of field names ([...] is the rest of your code):

<?php foreach ( $myrows as $a ): ?>

    <input id="id" type="text" name="id" value="<?php echo $a->id; ?>">


<?php endforeach ?>

If 3 rows are returned in $myrows and the values are fee, fie, foe, the value attribute changes for each field, but the field name is always the same. Here is are the first 3 fields bunched together:

<input id="id" type="text" name="id" value="fee">
<input id="id" type="text" name="id" value="fie">
<input id="id" type="text" name="id" value="foe">

The web server sees only the last value: id = foe fee and fum do not matter.

So, each set of form fields is replaced by the previous set. Define a unique name for each loop. Here is the first form field:

$row_count = 0;
foreach ( $myrows as $a ): ?>

    <input id="id-<?php echo $row_count; ?>" type="text" name="id-<?php echo $row_count; ?>" value="<?php echo $a->id; ?>">

    [ ... ]

endforeach; ?>

Replace [ ... ] with you other code.

Now, those same 3 fields would look like this:

<input id="id-0" type="text" name="id-0" value="fee">
<input id="id-1" type="text" name="id-1" value="fee">
<input id="id-2" type="text" name="id-2" value="fee">

You can define the row count in a hidden form variable (outside the foreach loop).

<input type="hidden" name="row_count" value="<?php echo count( $myrows );">

Then, in aad_process2_ajax() loop through all the names and do as many updates as there are rows in the row_count form field.

  • this is hard too understand too me
    – user33913
    Commented Aug 21, 2013 at 16:54
  • Is any part of it clear? Commented Aug 22, 2013 at 1:52
  • Never mind i do in difrent why. And i i don't use jquery. Thx anywhy i fink u answer is correct.
    – user33913
    Commented Aug 23, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.