0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks for that. i tested that in ajax and json, tried to output it, but it alerts, "undefined" instead of its value.
Undefine in ajax success @pao
yes. how can i get atleast 1 row to be outputed in my ajax success? using json?
@pao if u r sendind response in ajax than use datatype in ajax param dataType:"json",
this is my ajax code: $(document).ready(function(){ $('#form').ajaxForm(function(){ //$("#loading").fadeIn(); var catRows = $('#cat_rows').val(); $.ajax({ type: "GET", data: {'cat_rows':catRows}, url: 'pages/scripts/insert_daily_checklist_category.php', cache: false, dataType: 'json', success:function(data){ alert(data.catrows); //alert('Daily Checklist Categories are now updated.'); } }); }); });
|
0

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
  }
}); 

3 Comments

how i do retrieve array input rows using php? var catRows = $('#cat_rows').val(); this is my variable in ajax call to get values from my array input boxes, but it seems that its getting nothing.
@Pao, Why are you doing this, I can't understand? There is no need of this, you can retrieve form input value at php server just using $_REQUEST['cat_rows'] and this will return an array.
its ok now, thanks for the advices sir. i used $('#form').serialize() instead of var catRows = $('#cat_rows').val(). and i used while loop and if else statement inside it to update my database rows from my array inputs.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.