Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emmanuelfk's avatar

submit button brings Sweetarlert popup without saving the data in the database

function saveCategory() {

        var category = new Object();
        category.CategoryId = $("#categoryId").val();
        category.CategoryName = $("#categoryName").val();
        category.Status = $("#status").val();
        var data = JSON.stringify({
            category: category
        });

        document.getElementById('success-message').onclick = function () {
            swal('Success!', 'Category is Saved!', 'success')
        };
        swal({              
            
                Position:'top-end',
                icon: "success",
                title: 'Your work has been saved',
                showConfirmButton: false,
                timer:1500
           
        }).then(function (isConfirm) {
            if (isConfirm) {

                return $.ajax({
                    contentType: 'application/json; charset=ut-8',
                    dataType: 'json',
                    type: 'POST',
                    url: "/Home/SaveCategory",
                    data: data,
                    success: function (result) {
                        if (result == true) {
                            GetAllCategory();
                            dataTable.ajax.reload();
                           
                        }
                    }
                });
            } else {
                
                swal("Cancelled", "You have Cancelled Form Submission!", "error");
                $("#categoryModal").modal('hide');

            }


        });

    }
0 likes
9 replies
Snapey's avatar

@emmanuelfk so you are going to have to do some digging and find out

  • is a network request sent to the server?
  • can you see a response? what does it say?
  • anything added to the Laravel log gile?
emmanuelfk's avatar

When I implement the sweetarlert this way in the JSON AJAX, it works and the data get saved in the database but it come as a popup and what I am trying to do is to make it come like mark success when the item get save in the database.

function saveProduct() { var product = new Object(); product.ProductId = $("#productId").val(); product.CategoryId = $("#categoryId").val(); product.ProductName = $("#productName").val(); product.Status = $("#status").val(); var data = JSON.stringify({ product: product

        });
        return $.ajax({
            contentType: 'application/json; charset=ut-8',
            dataType: 'json',
            type: 'POST',
            url: "/Home/SaveProduct",
            data: data,
            success: function (result) {
                if (result == true) {
                    GetAllProduct();
                    Reset();
                    swal("1 Product Save Successfully")

                }
                else {

                    swal("Save Failed!")
                }

            },
            error: function () {
                alert("Error!")
            }

        });
    }
Snapey's avatar

kindly perform the steps I suggested

emmanuelfk's avatar

`I am trying to save data from mvc controller with json ajax to sqlserver but it pass empty form to the database

                    <input type="text" class="form-control"
                           id="companyName" aria-describedby="emailHelp"
                           placeholder="Enter Company name" required>
                    <div class="error"></div>
                </p>
                <p>
                    <input type="number" class="form-control"
                           id="phoneNo" aria-describedby="emailHelp"
                           placeholder="024..." required>
                    <div class="error"></div>
                </p>

                <p>
                    <input type="text" class="form-control"
                           id="email" aria-describedby="emailHelp"
                           placeholder="Enter Email" required>
                    <div class="error"></div>
                </p>

                <p>
                    <input type="text" class="form-control"
                           id="location" aria-describedby="emailHelp"
                           placeholder="Enter Location" required>
                    <div class="error"></div>
                </p>

                <p>
                    <input type="text" class="form-control"
                           id="contactPerson" aria-describedby="emailHelp"
                           placeholder="Enter Contact Person" required>
                    <div class="error"></div>
                </p>
                <p>
                    <input type="text" class="form-control"
                           id="product" aria-describedby="emailHelp"
                           placeholder="Enter Product" required>
                    <div class="error"></div>
                </p>
            </div>

function saveSupply() { var supply = new Object(); supply.SupplyId = $("#supplyId").val(); supply.CompanyName = $("#companyName").val(); supply.PhoneNo = $("#phoneNo").val(); supply.Email = $("#email").val(); supply.Location = $("#location").val(); supply.ContactPerson = $("#contactPerson").val(); supply.Product = $("#product").val(); var data = JSON.stringify({ supply: supply

    });
    swal("Save!", "Supply Info save successfully", "success");
    $("#success-message").click(function (e) {
        Swal.fire({
            title: "Good job!",
            text: "You clicked the button!",
            icon: "success",
            buttonsStyling: false,
            confirmButtonText: "Confirm me!",
            customClass: {
                confirmButton: "btn btn-primary"
            }
        });
    });

    return $.ajax({
        data: 'POST',
        dataType: 'json',
        url: "/Home/SaveSupply",
        contentType: 'application/json; charset=UTF-8',           
        data: data,
        success: function (result) {
            if (result == true) {
                //dataTable.ajax.reload();
                console.log(data)
                $("#SupplyModal").modal('hide');
            }
            else {
                swal("Save Fail!");
            }
        },
        error: function () {
            swal("Error")
        }
    });
}

public JsonResult SaveSupply(tblSupplyInfo supply) { ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); bool isSuccess = true; if (supply.SupplyId > 0) { db.Entry(supply).State = EntityState.Modified;

        }
        else
        {
            db.tblSupplyInfoes.Add(supply);
        }
        try
        {
            db.SaveChanges();
        }
        catch (Exception)
        {
            isSuccess = false;
        }
        return Json(isSuccess, JsonRequestBehavior.AllowGet);
    }

`

emmanuelfk's avatar

Hello team, I have notice that I didn't include object in my code, it working now thanks

Please or to participate in this conversation.