简体   繁体   中英

How to insert data to database using multiple array using POST method with ajax:

I read similar answer here in this question: How to insert into MYSQL row from multiple $_POST arrays and How to insert into MYSQL row from multiple $_POST arrays but the problem is these answers do not work in my code. Is it because im using an ajax? and i only get the value of the first array.

If i also place the variable declaration inside the for loop it is not working too.

Here is my ajax:

var name = []; 
$('input[name="name[]"]').map(function(){ name.push($(this).val()); }); var studid = []; 
$('input[name="studid[]"]').map(function(){ studid.push($(this).val()); }); var nameStr = name != '' ? '&name='+ name : '';
var studStr = studid != '' ? '&studid='+ studid : '';
var dataString = 'subject='+ subject + '&section=' + section + studStr + nameStr;

$.ajax({ type: "POST", url: 'save.php', data: dataString, dataType: "html", 
success: function(data) {
    $('input#subject-field').val('');
    $('input#section-field').val('');
    $('input.record-input-forms').val('');
    $('#status-message').css({"color":"#39b1c6"});
    $('#status-message').html('Save successfully',function(){
    $('#status-message').fadeOut(2000); }); }, 
error:function (xhr, ajaxOptions, thrownError){
    alert(thrownError); } });
    return false;
}); 

Here is my php:

if(isset($_POST['studid']) || isset($_POST['name'])){
    $studid = array_map(mysql_real_escape_string, explode(",",$_POST['studid']));
    $name = array_map(mysql_real_escape_string, explode(",",$_POST['name']));   

    for ($i=0; $i<count($studid); $i++){
        $sql_1 = "INSERT INTO tbl_student(StudentID, StudentName, SubjectID)  VALUES ('".$studid[$i]."', '".$name[$i]."', LAST_INSERT_ID())"; 
        mysqli_query($con,$sql_1);  
    } 
}

use mysql_insert_id(); instead of LAST_INSERT_ID()

You're not sending data correctly from the jQuery and its seems you'r mixing arrays and string together.

This is a simple request that posts studid-array from jQuery

var saveData = $.ajax({
                    type: 'POST',
                    data: {studid: studid},
                    url: 'save.php',
                    dataType: 'html'
                });

saveData.done(function(data) {                                                   
    $('input#subject-field').val('');
    $('input#section-field').val('');
    $('input.record-input-forms').val('');
    $('#status-message').css({"color":"#39b1c6"});
    $('#status-message').html('Save successfully',function(){
    $('#status-message').fadeOut(2000); });

});                        

saveData.fail(function(ts) {
    alert(ts.responseText);
});

When save.php is called, $_POST['studid'] would be set (if there are anything in the array)

If you instead do like this:

var saveData = $.ajax({
                        type: 'POST',
                        url: 'save.php?studid=' + studid,
                        dataType: 'html'
                    });

When save.php is called, $_GET['studid'] would be set (if there are anything in the array). The best way though is to use data-option in the ajax-function call (in my first case). If you choose to use this option you would have to serialize the stuid-array before putting it in as a part of an url.

UPDATE If you want to pass multiple arrays you would have to do something like this:

var saveData = $.ajax({
                        type: 'POST',
                        data: {studid: studid, name_arr2: data_arr2},
                        url: 'save.php',
                        dataType: 'html'
                    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM