1

In HTML, my FORM action="" and method="POST".

In my call i have:

    var array_dados = [];
    array_dados[0] = $("#form_email").val();
    array_dados[1] = $("#form_nome").val();
    array_dados[2] = $("#form_para").val();
    array_dados[3] = $("#form_assunto").val();
    array_dados[4] = $("#form_mensagem").val();
    var dataParam  = { action: 'enviaemailfoto',
                       dados : array_dados };                              
    var ajaxurl    = '<?php echo admin_url('admin-ajax.php'); ?>';
    alert(dataParam);
    $.ajax({
        type: "POST",
        url: ajaxurl,
        data: dataParam,
        dataType: "text",
    })
        .done(function(res) {
            alert( "sucess " +res);
        })
        .fail(function(res) {
            alert( "error " +res);
        })
        .always(function(res) {
            alert( "complete " +res);
        }); 

On ajax code:

add_action('wp_ajax_enviaemailfoto', 'enviaemailfoto_callback');

function enviaemailfoto_callback(){
    header('HTTP/1.1 200 OK'); /* attempt to make it work */
    require 'wp-load.php';     /* attempt to make it work */
    $dados    = $_POST['dados'];
    $email    = $dados[0];
    $nome     = $dados[1];
    $para     = $dados[2];
    $assunto  = $dados[3];
    $str      = $dados[4];
    $anexo    = $dados[5];
    $mensagem = str_replace("\n", "<BR />", $str);

    /* prepare headers, send emails */

    echo json_encode(array('type'=>'done', 
                           'text' => 'Mensagem enviada com sucesso'.$assunto));
    wp_die();  }

Everything executes correctly. The three alerts are showed: 1 before, and 2 after (success/complete) ajax execute. But after the third one, is open a 404 page not found. I have made other ajax call without this problem, and include header('HTTP/1.1 200 OK') and require 'wp-load.php' here trying make it work. Without success.

2
  • Can you edit your answer with your form code. How did you set your action ? Why did you include wp-load.php, there's no need here ? – Benoti Oct 17 '16 at 18:19
  • Are you stopping the form submission to wait for the AJAX response? It could be possible that your form is actually submitting to an unknown page. – Howdy_McGee Oct 17 '16 at 18:20
1

As noted in the comments, you have a form that may not know where to go. In general, if you have a form to be submitted, you don't need AJAX because the form's action will handle the form submission as well as the next page. If you need something from the database before the form is submitted (like the available sizes and colors of the product the user selected), then AJAX is needed. Since we can't see the code for the entire form, we cannot say for sure, but it's possible you don't need AJAX in this situation, and using AJAX is what is causing your failure.

3
  • Hi, Scott. In my FORM, action="" and method="POST". The action is given in dataParam $.ajax. I put more infos in code. Thanks. – Douglas Vargas Oct 18 '16 at 0:32
  • I think that's why you're getting the 404. Your AJAX is processing, but then the FORM is trying to go where you sent it: NULL. Try giving an action to the FORM (maybe "enviar.php"). The file that handles the action does two things: 1. processes everything your AJAX file does, and 2. loads a new page for post-submission of the form. – scott Oct 18 '16 at 15:42
  • AJAX is very sexy, but it's only needed when you need data without a page load. Submitting a form is not one of those times, so do all your work in the form-submission handler and you'll be OK. Either that, or scrap the FORM structure: it's the FORM that needs to load a new page when it's all done. – scott Oct 18 '16 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.