I want to send a form data with Ajax after validation
About validation all think that's work
but about send data I have some problems
Ajax code :
function submitForm(){
var server = 'http://localhost/test'; // Your PHP file
var commentName = $('#commentName').val(); // The values of your form
var commentMail = $('#commentMail').val(); // The values of your form
var commentPhone = $('#commentPhone').val(); // The values of your form
var comment = $('#comment').val(); // The values of your form
$.ajax({ // Here the magic starts
url: server+"/contact.php", // Where this function will send the values
type:"POST", // To get the status of your php file
data: "action=insertNews&commentName="+commentName+"&commentMail="+commentMail+"&commentPhone="+commentPhone+"&comment="+comment, // The values
success: function (data){ // After sending the values to your php file you will receive number 1 or 2, if you receives number 1 it means sucess, but if you receives number 2 it means fail.
if(data == '1'){
alert("Is send");
}
else{
alert("Oop");
}
}
});
}
Code PHP And HTML :
<?php
if(isset($_POST['submit'])){
$message = '';
$email = '';
$name ='';
$message = $_POST['name'];
$email = $_POST['mail'];
$name = $_POST['phone'];
$to = "test@test.com";
$subject = 'New Message';
$message = " Le nom : ".$name."<br><br>".$message."<br><br> Email : ".$email;
$header = "$email";
if(mail($to, $subject, $message, $header)){
echo '1';
}
else{
echo '2';
}}
?>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8">
<script type="text/javascript" src="js.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body onload="randNums()">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input id="commentName" onkeyup="validateName()" name="name" type="text" placeholder="Name"><label id="commentNamePrompt"></label><br>
<input id="commentMail" onkeyup="validateMail()" name="mail" type="text" placeholder="Mail"><label id="commentMailPrompt"></label><br>
<input id="commentPhone" onkeyup="validatePhone()" name="phone" type="text" placeholder="Phone"><label id="commentPhonePrompt"></label><br>
<textarea id="comment" onkeyup="validateComment()" name="commente" placeholder="Messege here"></textarea><label id="commentPrompt"></label><br>
<span id="digit1"></span> +
<span id="digit2"></span> =
<input id="captcha" size="2" onkeyup="validateCaptcha()"><label id="captchaPrompt"></label><br>
</form>
<button name="submit" onclick="validateCommentForm()" > Send</button><label id="commentFormPrompt"> </label>
</body>
</html>
I want to know is that's right ? and if I have a problem can you fixed ?
</div>