Pao
Pao

Reputation: 13

Insert values in database from array table using ajax and php

help me to insert data from array inputs from my table using ajax and php. I have the separate php file, but it is blank at the moment.

this is my current html + php code:

<form class='form-horizontal form-validate' id="form">
<?php
echo "<table id='example' class='table table-bordered'>
<thead>
<tr>
<th>Category</th>
<th>Last Qtr Average time</th>
<th>Target time</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Category</th>
<th>last Qtr Average time</th>
<th>Target time</th>
</tr>
</tfoot>
<tbody>";
$get_category_query = "select * from daily_checklist_category order by category asc";
$get_category_result = mysql_query($get_category_query) or die(mysql_error());
while($count_rows = mysql_fetch_row($get_category_result)){
echo'<tr>';
echo'<td>'.$count_rows[1].'</td>';
echo'<td id="fucktime[]">'.$count_rows[2].'</td>';
echo'<td><input type="text" value="'.$count_rows[3].'" id="cat_rows[]"/></td>';
echo'</tr>';                                    
}
echo "</tbody>

</table>";
?>
<div class="form-actions">
<input type="submit" id="finish" class="btn btn-primary" value="Submit">
</div>

and this is my ajax :

$(document).ready(function(){

  $('#form').ajaxForm(function(){
    //$("#loading").fadeIn();

    var catRows = $('#cat_rows[]').val();
    $.ajax({
        type: "GET",
        data: $('#form').serialize(),
        url: 'pages/scripts/insert_daily_checklist_category.php',
        cache: false,
        success:function(data){
            alert('Daily Checklist Categories are now updated.');
        }
    });
  });

});

need help.

Upvotes: 0

Views: 1602

Answers (2)

devpro
devpro

Reputation: 16117

Issue in your code is that, you are not using name field in input field:

<input type="text" value="'.$count_rows[3].'" id="cat_rows[]"/>

This Should be:

<input type="text" name="cat_rows[]" value="'.$count_rows[3].'" id="cat_rows"/>

In jQuery:

You need to get this value as:

var catRows = $('#cat_rows').val();

Side note:

I am assuming you are using closing tag in your original file.

Also use mysqli_ or PDO becuase mysql_ extension is deprecated and not available in PHP 7.

Upvotes: 1

Vikash Kumar
Vikash Kumar

Reputation: 1111

First add the name attribute into your input form element like this.

echo '<td><input type="text" name="cat_rows[]" value="'.$count_rows[3].'" id="cat_rows_'.$count_rows[3].'"/></td>';

And your are already using ajaxForm jquery so there is no need of any another write ajax statement into callback. Your ajaxForm jquery would be like this.

jQuery Example-

// bind form using 'ajaxForm' 
$('#form').ajaxForm({
  url: 'pages/scripts/insert_daily_checklist_category.php',
  beforeSubmit: function(formData, jqForm, options) {
    // write code here like loader if needed
  },
  success: function(responseText, statusText, xhr, $form) {
    // write code for response handling
  }
}); 

Upvotes: 0

Related Questions