[英]Can I use POST to connect AJAX with PHPMailer?
I'm trying to make a contactform and I have js and php but no connection between them. 我试图做一个contactform,我有js和php,但它们之间没有任何联系。 Am I correct to use $_POST['Name'] when receiving a string as data from AJAX?
从AJAX接收字符串作为数据时,我是否正确使用$ _POST ['Name']? Is there a way for me to find out what that string looks like?
有没有办法让我找出该字符串的样子?
EDIT: Ok, I've managed to find out that I constantly get NULL or empty array. 编辑:好的,我设法找出我不断得到NULL或空数组。 I've also found out that it might be the header from jQuery and stringify that isn't a match with PHP.
我还发现,这可能是jQuery和stringify中的标头与PHP不匹配。 But that's too advanced for me to get.
但这对我来说太先进了。 But I think a better question here is: How do I get a read out from PHP of the data I send?
但是我认为这里有一个更好的问题:如何从PHP中读取发送的数据?
EDIT2: var_dump($_POST); EDIT2:var_dump($ _ POST); gives "array(0) { }".
给出“ array(0){}”。 How do I get all raw data that's sent?
如何获取所有已发送的原始数据?
My javascript looks like this (found it online inorder to learn). 我的JavaScript看起来像这样(在网上找到它以便学习)。
/*-------------
//lots of code to check validity of form
//-------------*/
});
if( ferror ) return false;
else var str = $(this).serialize();
$.ajax({
type: "POST",
url: "contactform.php",
data: str,
success: function(msg){
// alert(msg);
if(msg == 'OK') {
$("#sendmessage").addClass("show");
$("#errormessage").removeClass("show");
}
else {
$("#sendmessage").removeClass("show");
$("#errormessage").addClass("show");
$('#errormessage').html(msg);
}
}
});
return false;
});
});
And my PHP looks like this. 我的PHP看起来像这样。
<?php
parse_str($_POST['str'], $var1);
var_dump ($var1);
$data = json_decode($_POST['data'], true);
var_dump($data);
?>
A data object needs to be sent with the ajax request so that you can access it in the php code. 数据对象需要与ajax请求一起发送,以便您可以通过php代码访问它。 Which will generally look like-
通常看起来像-
data: {name: name, email: email}
In your code you are using str
variable in the data object. 在代码中,您正在数据对象中使用
str
变量。 So ensure that str
represents the data object. 因此,请确保
str
代表数据对象。 I would say probably that is causing your problem. 我想可能是造成您的问题的原因。
Your PHP code needs an update. 您的PHP代码需要更新。
Try this: 尝试这个:
<?php
parse_str(file_get_contents("php://input"), $var1);
var_dump ($var1); //this will give you the array.
?>
You're close with your thoughts about jQuery and headers so I'll let you finish reading up and not go into detail. 您对jQuery和标头的想法很接近,因此我将让您完成阅读,而不赘述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.