0

I'm trying to create a simple form for sending an email with ajax call. I can get into ajax's success, however I don't get a message. I'll post my code, could you tell me where the problem is?

html

`<input type="text" name="name" id="name">
        <label for="name">Name</label><br>
        <input type="text" name="email" id="email">
        <label for="email">email</label><br>
        <input id="submit" type="submit" value="click me">`

js

  <script>
        $(document).ready(function(){
            $("#submit").click(function(){


              $.ajax({
                 "method": "POST",
                 "url": "test.php" ,
                 "data": {
                     name : $("#name").val(),
                     email : $("#email").val()
                 },
                 success: function(){
                     alert("message send")
                 },
                 error : function(iqXHR, textStatus, errorThrown){
                      alert(
                         "iqXHR.status: " + iqXHR.status + "\n" +
                         "textStatus: " + textStatus + "\n" +
                         "errorThrown: " + errorThrown
                     ) 
                 }
              })
            })
        });
    </script>

php

<?php 

$name = $_POST["name"];
$email = $_POST["email"];

mail("test@gmail.com","Mail from:" .$email,"Test Message");

?>
  • 2
    You shouldn't be using mail() directly unless you fed it with RFC-conform data... most likely, due to the non-conform data, your mail is considered as spam. – Honk der Hase Mar 06 '20 at 21:59
  • How are you getting into Ajax's success function if you're not seeing the alert? Is it that the email isn't being sent? – iJamesPHP2 Mar 06 '20 at 22:23

0 Answers0