Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code which insert book details , but im currently having a problem in passing array values to my ajax function. I'm kinda new to ajax so im still getting the hang of it .

MY HTML INPUT TEXTBOX
 <input type="text" class="form-control text-center" id="author_lname[]" name="author_lname[]" placeholder="Last Name" required>
<input type="text" class="form-control text-center" placeholder="First Name" id="author_fname[]" name="author_fname[]" required>
<input type="text" class="form-control text-center" id="author_mname[]" name="author_mname[]" placeholder="Middle Name / Initial" required>

MY AJAX FUNCTON
var getauthor_lname = $("#author_lname").val();
     var getauthor_fname = $("#author_fname").val();
     var getauthor_mname = $("#author_mname").val();
     var whatprocess = "ADDBOOK";
 $.ajax({
        url: "adminfunctions.php",
        method: "POST",
       data: {getauthor_lname:getauthor_lname, 
              getauthor_fname:getauthor_fname,
              getauthor_mname:getauthor_mname , 
              whatprocess : whatprocess
        },
        success: function(data) {
               var getdata = data.trim();
               if (getdata == "SUCCESS") {
                swal({
                      title: 'Success!',
                      text: '',
                      type: 'success',
                      confirmButtonClass: "btn btn-success",
                      buttonsStyling: false
                    }).then(function() {
                      $("#datatables").load(window.location + " #datatables");
                                  });
                                }
                                else {
                                    swal({
                                        title: 'Sorry for the inconvenience!',
                                        text: "There's a problem. Please contact the technical support for any concerns and questions.!",
                                        type: 'error',
                                        confirmButtonClass: "btn btn-danger",
                                        buttonsStyling: false
                                    }).catch(swal.noop)
                                }
                            },
                            error: function(jqXHR, exception) {
                                console.log(jqXHR);
                            }
                        });


AND MY PHP FOR INSERTING DATA
$getauthor_lname = $_POST["getauthor_lname"];
$getauthor_fname = $_POST["getauthor_fname"];
$getauthor_mname = $_POST["getauthor_mname"];

for ($i = 0; $i < count($getauthor_fname); $i++) {
        if ($getauthor_fname[$i] != "" && $getauthor_mname[$i] != "" && $getauthor_lname[$i] != "") {
            $query = "INSERT INTO tbl_author (book_isbn, author_firstname, author_middlename, author_lastname) VALUES (? , ? , ? , ?)";
            $stmt = $mysqlconnection->prepare($query);
            $getauthor_lname[$i] = htmlspecialchars(strip_tags($getauthor_lname[$i]));
            $getauthor_fname[$i] = htmlspecialchars(strip_tags($getauthor_fname[$i]));
            $getauthor_mname[$i] = htmlspecialchars(strip_tags($getauthor_mname[$i]));
            $stmt->bind_param("ssss", $getbook_isbn, $getauthor_fname[$i], $getauthor_mname[$i], $getauthor_lname[$i]);
            $stmt->execute();
        }else{

            echo "ERRORauthor";
        }
    }


What I have tried:

I havent tried anything yet , but any help would be great !
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900