J.Doe J.Doe - 5 months ago 9
jQuery Question

how to make a contact from html form and php page by ajax

I make this form to send data to a php page in another domain but always it results error. can someone explain my problem
I search in Internet many times but exactly I didnt find my answer
here is my code

html:

<form action="#" id="smail" method="post" class="form">
<input type="text" name="name" value="Your Name *">
<input type="text" name="mailadd" value="Your E-mail *">
<textarea name="message" cols="0" rows="0">Your Message *</textarea>
<input type="submit" value="send message">
</form>


js:

$('#smail').submit(function(event) {
event.preventDefault();
var mail = $("#smail input[name=name]").val();
var message = $("#smail input[name=mailadd]").val()+' '+$("#smail textarea[name=message]").val();
$.ajax({
type: "POST",
url:"http://cofeebeen.dx.am/email.php",
crossDomain: true,
data:{
"mail": mail,
"message": message,
},
dataType: "text",
error: function(){
alert("error")
}
}).success(function(result){
alert(result)
});
});


php:

<?php
$subject = $_POST["name"];
$msg = $_POST["mailadd"]+"\n"+$_POST["message"];
mail("someone@example.com",$subject,$msg);
?>

Answer

Your PHP code is not correct, we can get data at your PHP page like below.

Correct code:

$subject = $_POST["mail"];
$msg = $_POST["message"]

Incorrect code:

$subject = $_POST["name"];
$msg = $_POST["mailadd"]

I hope it will work now.