'Wordpress Modal form ajax send

Let's see if you can help me, because I'm stuck because I can't get the ajax instruction to save the data to be executed

I have a modal window in the admin of my wordpress plugin, which I did via thickbox. The code for that modal is:

<p>Por favor, elija la configuración para <b><?php echo($_GET['title']);?></b>:</p>

<form method="post" id="ajax-form">
    <label for="tipo">Introduzca el tipo de producto</label>
    <select name="tipo" id="tipo">
        <option value="Subscripción">Subscripción</option>
        <option value="Sesion">Sesion</option>
    </select><br><br>
    <label for="cantidad">Introduzca el número de sesiones o de meses de subscripción</label>
    <input name="cantidad" id="cantidad"></input>
    <input id="id" name="id" type="hidden" value="<?php echo($_GET['id']);?>"><br>
    <input type="submit">
    
    <script type="text/javascript">
        jQuery(document).ready(function(event) {
            
            jQuery('#ajax-form').submit(ajaxSubmit);
            
                function ajaxSubmit() {   
                    var ajaxform = jQuery(this).serialize();
                    jQuery.ajax({ 
                        action: 'grabardatos_desde_modalform',
                        data: ajaxform,
                        type: 'POST',
                        url: '<?php echo admin_url('admin-ajax.php'); ?>',
                        success: function(data) {
                            alert("GUAYYYYYY"); 
                        }
                    });

        }); }
    </script>
</form>

The php code that should process the data is:

add_action('wp_ajax_grabardatos_desde_modalform' , 'grabardatos_desde_modalform');
add_action('wp_ajax_nopriv_grabardatos_desde_modalform','grabardatos_desde_modalform');

function grabardatos_desde_modalform(){
    $datos=array(
        $_POST['id'] => array(
            'cantidad' => $_POST['cantidad'],
            'tipo' => $_POST['tipo']
        ));
    error_log(print_r($datos));
    update_option('arrayproductos',$datos);
}

But I don't know why the query is not executed, because when I hit send it is as if the modal was reloaded.

On the other hand, I also need that after processing the data the main page is redirected.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source