Error sending Ajax data to PHP script

Asked

Viewed 156 times

1

I’m trying to take an email by an input, stream to the server through Ajax, and on the server side there’s a script that uses the Phpmailer class, to send an email, and the PHP code without receiving variable works, so the error is probably in Ajax

HTML

form id="emailForm">
        <label for="campoEmail" class="has-information" hidden="false">Email enviado com sucesso</label>
        <input id="campoEmail" class="input-center" placeholder="Digite aqui" type="email" name="email" required="true">
        </br>
        </br>
        <input id="enviar" type="submit">
      </form>

Javascript com Jquery

$('#emailForm').submit(function(e){
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: '/email.php',
        data: 'email=' + $('#campoEmail').val(),
        success:alert("Sucesso")
    });
});

});

PHP has been tested separately and is working

  • And what is the error? The email is not sent or the alert is not displayed?

  • The PHP code is not triggered, through AJAX, your answer fixed the parameter "sucess", agr it no longer appears

  • Take a look here in PHP Code

  • 1

    Dude, if the php code isn’t triggered, it’s your URL that’s wrong. Or, if by chance it is being triggered and does not perform anything, you probably have to check whether the variable email is with some value. Run these tests in PHP and see where specifically the error happens.

  • Like, it’s in the same folder on the server, but if I open 'email.php' separately, it runs smoothly, but when I call through AJAX it doesn’t run

  • If both are in the same folder, there is no reason to use absolute path. This may be the error, I edited my answer. To see what’s happening, use the Network tab of your browser’s Developer Tools.

  • It was the directory of the.php file, already solved, when you put /email php., it searches the root directory

Show 2 more comments

1 answer

3

In function $.ajax([settings]) the parameter settings accepts this key success, but its value must be a function, as documented. You specified a call to a function, which does not return a function - Alert() returns undefined.
The excerpt success:alert("Sucesso") must be: success: function() { alert("Sucesso"); }

Another problem, you said they are in the same folder on the server, and yet you specified an absolute path to email.php. This works if both are at the root, but since you didn’t say this and is giving error, try changing url: '/email.php', for url: 'email.php',.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.