Error sending Ajax data to script in PHP

1

I'm trying to get an email through an input, transmit to the server through Ajax, and server side has a script that uses the PHPMailer class to send an email, and the PHP code without receiving variable works, so the error 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 with 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

    
asked by anonymous 27.05.2016 / 06:03

1 answer

3

In the $.ajax([settings]) function the settings parameter accepts this key success , but its value must be a function, according to the documentation . You specified a call to a function, which does not return a function - alert () returns undefined . The success:alert("Sucesso") excerpt should 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 in the root, but since you did not say that and you're giving the error, try switching url: '/email.php', to url: 'email.php', .

    
27.05.2016 / 07:43