使用Ajax发送表单(PHP)数据

时间:2016-04-08 05:00:45

标签: javascript php jquery ajax

我想在验证后用Ajax发送表单数据

关于验证,所有人都认为该工作

但是关于发送数据我有一些问题

Ajax代码:



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");
            }
        }
    });
}




代码PHP和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>
&#13;
&#13;
&#13;

我想知道那是对的吗?如果我有问题你能解决吗?

3 个答案:

答案 0 :(得分:1)

在回应响应后插入die

if(mail($to, $subject, $message, $header)){
  echo '1';
}
else{
  echo '2';
}
die;
}

并在ajax响应中修剪你得到的数据字符串:

if(data.trim() == "1"){
  alert("Is send");
}

答案 1 :(得分:0)

只使用此格式的数据发送

 data:{action:'insertNews',commentName:commentName,commentMail:commentMail},

答案 2 :(得分:0)

尝试更改您的PHP代码:

<?php
if(isset($_POST['submit'])){
$phone= '';
$email = '';
$name ='';
$commente= $_POST['commente']; // not used anywhere

$name= $_POST['name'];
$email = $_POST['mail'];
$phone= $_POST['phone'];

$to = "test@test.com";

$subject = 'New Message';

$message = " Le nom : ".$phone."<br><br>".$name."<br><br> Email : ".$email;

$header = $email;

if(mail($to, $subject, $message, $header)){
  echo '1';
}
else{
  echo '2';
}}
?>
相关问题