0

I have a problem this PHP send mail.

I have this form in index.php page:

<form action="">
<input placeholder="Name" class="form" id="name" type="text" required/>
<input placeholder="Email" class="form" id="mail" type="email" required />
<input placeholder="Object" class="form" id="object" type="text" required />
<textarea placeholder="Type here..." id="text" class="form"></textarea>
<input class="formBtn" type="submit" id="submit"/>
<input class="formBtn" type="reset" />

This script on bottom of the index.php

<script>
$('#submit').click(function() {
    var nameform = $('#name').val();
    var mailform = $('#mail').val();
    var objectform = $('#object').val();
    var textform = $('#text').val();
    var mailcomplete = 'Name='+nameform+'Mail='+mailform+'Object='+objectform+'Message='+textform;

    $.ajax({
        type: "POST",
        url: 'php/mail.php',
        data: mailcomplete,
        success: function() {
            alert("Mail send OK!");
        }

    });
});

and this in php/mail.php

 <?php

$name = $_POST["nameform"];
$mail = $_POST["mailform"];

mail($mail, $name, "Hello!");

?>

But don't work... Can you help me? Tnk

antoniodna87
  • 11
  • 2
  • 6
  • 1
    is there any error? What actually happen? looks like the page is refresh when you clicked the button, so it does not run the ajax code. – Afif Zafri Feb 19 '17 at 12:42

2 Answers2

0

Change your php/mail.php to this:

<?php

$name = $_POST["Name"];
$mail = $_POST["Mail"];

mail($mail, $name, "Hello!");

?>

Also in your script in the index.php file, change:

var mailcomplete = 'Name='+nameform+'Mail='+mailform+'Object='+objectform+'Message='+textform;

to

var mailcomplete = 'Name='+nameform+'&Mail='+mailform+'&Object='+objectform+'&Message='+textform;

That should work. The problem was that you are passing the variables Name and Mail in data and not nameform and mailform. Also, the data formatting was incorrectly done in the script. Lemme know if it still doesn't work.

Ketan Malhotra
  • 1,255
  • 3
  • 16
  • 44
0

chane your button to

<input class="formBtn" type="button" id="submit"/>

and

var mailcomplete = 'Name='+nameform+'&Mail='+mailform+'&Object='+objectform+'&Message='+textform;

and also the php script

<?php
$name = $_POST["Name"];
$mail = $_POST["Mail"];
mail($mail, $name, "Hello!");