Site icon Hip-Hop Website Design and Development

WordPress + JavaScipt + AJAX + MySQL: insert query for form

I’m trying to make booking – plugin with form on WordPress-admin site, where I can add new event in calendar (made with JavaScript).
I have to submit form with all data and insert data to MySQL Database.

My ajax.js file:

function ajaxSubmit(e){
var nazwa_szkolenia = document.getElementById("nazwa_szkolenia").value;
var Data_szkolenia = document.getElementById("Data_szkolenia").value;
var Godzina = document.getElementById("Godzina").value;
var Max_pacjent = document.getElementById("Max_pacjent").value;

// Returns successful data submission message when the entered information is stored in database.
var dataString = 'nazwa_szkolenia1=' + nazwa_szkolenia + '&Data_szkolenia1=' + Data_szkolenia + '&Godzina1=' + Godzina + '&Max_pacjent1=' + Max_pacjent;
if (nazwa_szkolenia == '' || Data_szkolenia == '' || Max_pacjent == '') {
    alert("fill all fields");
} else {
    // prevent the default action.
    e.preventDefault();

    var myform= jQuery(this).serialize();

    jQuery.ajax({
        type:"POST",
        // Get the admin ajax url which we have passed through wp_localize_script().
        url: ajax_object.ajax_url,
        action: "submitAjaxForm",
        data: dataString,
        error: function(jqXHR, textStatus, errorThrown){                                        
        console.error("The following error occurred: " + textStatus, errorThrown);                                                       
    },
        success:function(data){
            alert("process OK");
        }
    });
}
return false;
}

arcode-functions.php:

<?php
add_action('init', 'registerFormAction');

    function registerFormAction(){
        add_action('wp_ajax_nopriv_submitAjaxForm','submitAjaxForm_callback');
        add_action('wp_ajax_submitAjaxForm','submitAjaxForm_callback');

    }
    
    function submitAjaxForm_callback(){
        global $wpdb;
    
      
        $nazwa_szkolenia2 = $_POST['nazwa_szkolenia1'];
        $Data_szkolenia2 = $_POST['Data_szkolenia1'];
        $Godzina2 = $_POST['Godzina1'];
        $Max_pacjent2 = $_POST['Max_pacjent1'];
    
    
        $wpdb->insert( $wpdb->modul_terminarz, array( 'proba' => $nazwa_szkolenia2, 'id' => '10', 'data_szkolenia' => $Data_szkolenia2, 'typ_szkolenia' => '1', 'ile_miejsc' => $Max_pacjent2, 'pelne' => '1', 'Ile_miejsc_akt' => '4' ) );
   

        wp_die();
    }

?>

I’ve added admin-ajax.php with wp_localize_script function. The form button is made with JavaScript:

var Input_Form_Send = document.createElement('input');
    Input_Form_Send.value = 'Utwórz';
    Input_Form_Send.type = 'submit';
    Input_Form_Send.className = 'input-form-send';  
    Input_Form_Send.onclick = ajaxSubmit;

When I fill all fields from form, click on the button, then comes alert "process ok", but on the Database comes nothing new… nazwa_szkolenia, Data_szkolenia, Godzina, Max_pacjent -those are the names of the form fields.

Maybe you know what I’ve made wrong?