所以我像Ajax一样提交我的表单
$("#submitform").click(function(e){
e.preventDefault();
var form_data = $("#contactfrm").serialize();
$.ajax({
type: "POST",
url: "/ltlcreation-new/wordpress/wp-content/themes/LTLCreation/includes/form-handler.php",
data: form_data,
error: function(){
alert("failed");
},
success: function(json_data){
console.log(json_data);
alert("success");
},
})
});
在我的form-handler.php中,我捕获了来自错误
<?php
if(isset($_POST['submit'])) {
//include validation class
include 'validate.class.php';
//assign post data to variables
$name = @($_POST['name']);
$email = @($_POST['email']);
$message = @($_POST['message']);
$phone = @($_POST["phone"]);
//echo $name, $email, $message, $phone;
//start validating our form
$v = new validate();
$v->validateStr($name, "name", 3, 75);
$v->validateEmail($email, "email");
$v->validateStr($message, "message", 5, 1000);
$v->validateStr($phone, "phone", 11, 13);
if(!$v->hasErrors()) {
$to = "lukelangfield001@googlemail.com";
$subject = "Website contact form ";
$mailbody = $message . "\n" . "from " . $name . "\n" . $phone;
$headers = "From: $email";
mail($to, $subject, $mailbody, $headers);
echo "success";
} else {
//set the number of errors message
$message_text = $v->errorNumMessage();
//store the errors list in a variable
$errors = $v->displayErrors();
//get the individual error messages
//$nameErr = $v->getError("name");
//$emailErr = $v->getError("email");
//$messageErr = $v->getError("message");
//$phoneErr = $v->getError("phone");
echo $message_text; echo $errors;
$output = array($message_text);
echo json_encode($output);
}//end error check
}// end isset
这些错误通常看起来像这样
There were 4 errors sending your message! Name must be at least 3 characters long. Please enter an Email Address. Message must be at least 5 characters long. Phone must be at least 11 characters long. ["There were 4 errors sending your message!\n"]
我已尝试对输出进行jSon编码,并在ajax中成功将json数据拉出,但它只是不断返回一个空字符串
(空字符串)
我的问题是你可以将数据从PHP发送回Ajax,如果是这样的话我完全错了吗?
答案 0 :(得分:0)
您可以输出除json字符串以外的任何内容,以便echo "success";
生成t。使用调试器网络响应输出选项卡可以看到这是正确编码的。
也不要使用
$name = @($_POST['name']);
改为使用
$name = isset($_POST['name']) ? $_POST['name'] : '';
如果您仍有空白页,请确保设置了显示错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
答案 1 :(得分:0)
我是一个白痴我仍然在我的PHP文件中有这个,这意味着表单没有触发或返回响应,愚蠢的我,很高兴我终于想通了
if(isset($_POST['submit'])) {
感谢帮助人员
答案 2 :(得分:-1)
以下是您可以使用的Ajax联系表单示例:
Ajax.js
$(document).ready(function(){
$("#btn").click(function(){
var username=$("#name").val();
var email=$("#email").val();
var dis=$("#dis").val();
var process=true;
if(username=="")
process=false;
if(email=="")
process=false;
if(dis=="")
process=false;
if(process){
var dataString="name="+username + "&email="+email+ "&message="+dis;
$("#res").html('<span>Sending...</span><img src="a.gif">');
$.ajax({
url:"b.php",
type:"POST",
data:dataString,
success:function(data){
document.getElementById("name").value='';
document.getElementById("email").value='';
document.getElementById("dis").value='';
$("#res").html(data);
}
});
}else{
alert("fill all fields");
}
});
});
和b.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("ajax") || die("erro");
if(isset($_POST['name'])){
mysql_real_escape_string(htmlentities($name=$_POST['name']));
mysql_real_escape_string(htmlentities($email=$_POST['email']));
mysql_real_escape_string(htmlentities($message=$_POST['message']));
if(!empty($name) && !empty($email) && !empty($message)){
if(mysql_query("INSERT INTO `users` (name,email,message) VALUES('$name','$email','$message') ")){
echo 'The massage has been send';
}else{
echo mysql_error();
}
}
}
?>
享受....
答案 3 :(得分:-2)
您有以下内容:
success: function(json_data){
虽然json_data根本没什么。它应该是
success: function(data){