skip to Main Content

im successfuly inserted data to my db with ajax.. but somehow it does not pop up my success alert message. here is a sample of my jquery code.

$('button[name="submitmsgd"]').click(function(e){
    var categorysupAr = [];
    var unitsgdAr=[];
    var idsgdAr=[];
    var idsgdlength= $("input[name='idsgd[]']").val().length;
    var namesgdAr= [];
    var namesgdlength= $("input[name='namesgd[]']").val().length;
    var detailsgdAr = [];
    var qtysgdAr = [];
    var pricesgdAr = [];
    var totalsAr = [];
    var datefield= $("input[name='datetime']").val();
    $("select[name='categorysup[]']").each(function(){
        categorysupAr.push(this.value);
    });
    $("select[name='unitsgd[]']").each(function(){
        unitsgdAr.push(this.value);
    });
    $("input[name='idsgd[]']").each(function(){
        idsgdAr.push(this.value);
    });
    $("input[name='namesgd[]']").each(function(){
        namesgdAr.push(this.value);
    });
    $("textarea[name='detailsgd[]']").each(function(){
        detailsgdAr.push(this.value);
    });
    $("input[name='qtysgd[]']").each(function(){
        qtysgdAr.push(this.value);
    });
    $("input[name='pricesgd[]']").each(function(){
        pricesgdAr.push(this.value);
    });
    $("input[name='totals[]']").each(function(){
        totalsAr.push(this.value);
    });
    if ($.inArray("defaultc", categorysupAr)>-1){
        e.preventDefault();
        alert("Please choose the right category in all fields");

    }
    else if ($.inArray("defaultu", unitsgdAr)>-1){
        e.preventDefault();
        alert("Please choose the right unit in all fields");
    }
    else if ($.inArray("", idsgdAr)>-1||idsgdlength<=2){
        e.preventDefault();
        alert ("ID can't be empty, and must be more than 2 characters in all fields");
    }
    else if ($.inArray("", namesgdAr)>-1||namesgdlength<=2){
        e.preventDefault();
        alert ("Name can't be empty, and must be more than 2 characters");
    }
    else if ($.inArray("", totalsAr)>-1||$.inArray("0", totalsAr)>-1){
        e.preventDefault();
        alert ("Please make sure all quantity and all prices are filled with correct format(Only number allowed)");
    }else{ 
       $.ajax({
           url: "include/insertmsgd.php",
           method: "POST",
           data:{category:categorysupAr, unitsgd:unitsgdAr, idsgd:idsgdAr, namesgd:namesgdAr, detailsgd:detailsgdAr, qtysgd:qtysgdAr, pricesgd:pricesgdAr, totals:totalsAr, datefield:datefield},
           success: function (data) {
               alert(data);
           }
       });
    }
});

here is my php code:

<?php
include "myconfiguration.php";

if (isset($_POST["idsgd"])) {
    $idsgd      = $_POST["idsgd"];
    $category   = $_POST["category"];
    $unitsgd    = $_POST["unitsgd"];
    $namesgd    = $_POST["namesgd"];
    $detailsgd  = $_POST["detailsgd"];
    $qtysgd     = $_POST["qtysgd"];
    $pricesgd   = $_POST["pricesgd"];
    $totals     = $_POST["totals"];
    $date       = date('Y-m-d H:i:s', strtotime($_POST["datefield"]));

    for($index=0; $index<count($idsgd); $index++) {
        $idsgd_unstring     = mysqli_real_escape_string($conn, $idsgd[$index]);
        $category_unstring  = mysqli_real_escape_string($conn, $category[$index]);
        $unitsgd_unstring   = mysqli_real_escape_string($conn, $unitsgd[$index]);
        $namesgd_unstring   = mysqli_real_escape_string($conn, $namesgd[$index]);
        $detailsgd_unstring = mysqli_real_escape_string($conn, $detailsgd[$index]);
        $qtysgd_unstring    = mysqli_real_escape_string($conn, $qtysgd[$index]);
        $pricesgd_unstring  = mysqli_real_escape_string($conn, $pricesgd[$index]);
        $totals_unstring    = mysqli_real_escape_string($conn, $totals[$index]);

        $query= '
        INSERT INTO msgeneralgoods 
        VALUES("'.$idsgd_unstring.'", "'.$category_unstring.'", "'.$unitsgd_unstring.'", "'. $namesgd_unstring.'", 
        "'.$detailsgd_unstring.'", "'.$qtysgd_unstring.'", "'.$pricesgd_unstring.'", "'.$totals_unstring.'", "'.$date.'"); 
        ';
       mysqli_query($conn, $query);
       
    }
    if (mysqli_query($conn, $query)) {
        echo '<script language="javascript">';
        echo 'alert("Data Inserted")';
        echo '</script>';
    } else {
        echo '<script language="javascript">';
        echo 'alert("Error")';
        echo '</script>';
    }
}
?>

i did try this code for my if condition inside my php page

if (mysqli_query($conn, $query)) {
    echo 'Data Saved';
} else {
    echo 'Error'
}

but still no alert message…. im very new to ajax, can some1 please tell me what did i do wrong here?

2

Answers


  1. Chosen as BEST ANSWER

    after thorough search in console and googling, i found out that my ajax call was canceled because my save button is

    button type="submit"
    

    so i change it to

    button type="button"
    

    that way i can see my alert pop up..

    hope this help to whoever have the same problem as me


  2. add error to the ajax call to return error if there is one

     $.ajax({
            url: "include/insertmsgd.php",
            method: "POST",
            data:{category:categorysupAr, unitsgd:unitsgdAr, idsgd:idsgdAr, namesgd:namesgdAr, detailsgd:detailsgdAr, qtysgd:qtysgdAr, pricesgd:pricesgdAr, totals:totalsAr, datefield:datefield},
            success: function (data) {
               alert(data);
            },
            error: function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
       });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search