Register form not working sweetalert message?

the registration form is being sent, the database is being registered, but the sweetAlert message does not work, where am I doing it wrong, I am already grateful for solving the problem.



    $(document).ready(function(){

     $('form[name=my_form]').submit(function(reg_haber) {
        var post_url = $(this).attr("action"); 
        var request_method = $(this).attr("method"); 
        var form_data = new FormData(this);
        SwalReg_Haber(form_data);
        reg_haber.preventDefault();
        readHaber();
    });

 });    

    function SwalReg_Haber(form_data){
        return new Promise(function(resolve) {
         $.ajax({
            url: './islem/register-haber.php',
            type: 'POST',
            data: form_data,
            dataType: 'json',
            contentType: false,
            cache: false,
            processData:false
        })
         .done(function(response_reg_haber){
            swal(response_reg_haber.titles, response_reg_haber.message, response_reg_haber.status);
            readHaber();

        })

         .fail(function(){
            swal(response_reg_haber.titles, response_reg_haber.message, response_reg_haber.status);
            readHaber();
        });
     });

    }
    function readHaber(){
        setTimeout(function(){
           window.location.reload(1);
       }, 1500);   
    }

register-haber.php

include_once "/connect.php";

header('Content-type: application/json; charset=UTF-8');
$response_reg_haber = array();

$haber_title    = $_POST['haber_title'];
$haber_desc     = $_POST['haber_desc'];
$haber_kategori = $_POST['haber_kategori'];
$haber_tags     = $_POST['haber_tags'];
$haber_url      = $_POST['haber_title'];


$query_haber = "INSERT INTO tb_haber (haber_title, haber_url, haber_desc, haber_kategori, haber_tags ) VALUES ('$haber_title', '$haber_url', '$haber_desc', '$kategori', '$haber_tags')";


$register=mysqli_query($con, $query_haber);
if ($register>0) {

    $response_reg_haber['status']  = 'success';
    $response_reg_haber['titles']  = 'KAYIT BAŞARILI';
    $response_reg_haber['message'] = 'Haber başarı ile kaydedildi ...';
}else{

    $response_reg_haber['status']  = 'error';
    $response_reg_haber['titles']  = 'KAYIT BAŞARISIZ';
    $response_reg_haber['message'] = 'Bir sorun oluştu ...
    <br>Lütfen bu durumu ProPANEL yetkililerine bildirin..!';
}

echo json_encode($response_reg_haber);

ob_end_flush();
.fail(function(){ swal(response_reg_haber.titles, response_reg_haber.message, response_reg_haber.status); readHaber(); });

Well for starters, response_reg_haber is not defined in this scope. Also note that the fail command has a slightly different structure, as it receives the jqXHR object, not the data [it failed, so there is no ‘data’ per se] as its first parameter. Note that the ajax request considers anything rendered back to the requester (anything with status code 200) a success, so the only time it would fire the fail() command is if the server was down, or you changed page addresses, or something like that.

Second, if you’re going to do the same thing in both cases, change the code up slightly and use an .always()

Third, there’s no point in wrapping an ajax request in a Promise; jQuery’s AJAX returns a jqXHR object, which is already a deferred promise object. That’s why you can write .done and .fail in the first place :wink:

Fourth,

        reg_haber.preventDefault();
        readHaber();

This bottom line will mean the page reloads 1.5 seconds later - regardless of whether or not the form has finished processing. If the page reloads, the javascript triggers for what comes back are destroyed, as the AJAX request gets orphaned.

1 Like

m_hutley, thanks for your interest, I updated my codes, the problem is still going on!

 $(document).ready(function(){
        $("form[name=my_form]").submit(function(reg_haber) {
            var form_data = new FormData(this);
            reg_haber.preventDefault();
            SwalReg_Haber(form_data);
        });
    }); 

    function SwalReg_Haber(form_data){

        $.ajax({
            url: "./islem/register-haber.php",
            type: "POST",
            data: form_data,
            dataType: "json",
            contentType: false,
            cache: false,
            processData: false,
        })

        .always(function(response_reg_haber) {
            swal({
                title: response_reg_haber.titles, 
                text: response_reg_haber.message,
                type: response_reg_haber.status
            });
        });
    }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.