问题描述
我的格式如下:
<form id="f-comment" class="form" method="post" action="submit_img_comment.php">
<textarea name="comment"></textarea>
<input type="submit" value="Publish" data-params='{"imageid":<?php echo $imageid; ?>}'>
</form>
和以下javascript:
and the following javascript:
$(document).on("submit", ".form", function(e) {
e.preventDefault();
// what form are you submitting?
var form = $("#" + e.target.id);
var data = new FormData(this);
var params = $("input[type=submit]", this).data("params"); // parameters to send along with data
data.append("params", params);
// data is ok
console.log(params)
$.ajax({
type: form.attr("method"),
url: "include/" + form.attr("action"),
data: data,
dataType: "json",
contentType: false,
processData: false,
cache: false
}).done(function(data) {
alert(data['msg']);
}).fail(function(data) {
alert("Error: Ajax Failed.");
}).always(function(data) {
// always do the following, no matter if it fails or not
})
});
在我的php文件(submit_img_comment.php)中,我无法获得评论,就像这样
in my php file (submit_img_comment.php) im able to get the comment, like this
$_POST['comment'];
但是,当我尝试获取imageid时,就像这样
But, when i try to get the imageid, like this
$_POST['imageid'];
我收到错误:未定义索引:imageid
注释是表单的一部分,但是imageid作为参数发送并附加在FormData中.
The comment is part of the form, but the imageid is send as a parameter and appended in FormData.
如何在我的php文件中获取imageid?
How do i get the imageid in my php file?
推荐答案
您看错了,添加到表格中的不是imageid
而是params
.另外,您要发送的是一个javascript对象,您需要首先将其转换为字符串.您将需要在JavaScript中执行以下操作:
You are look at this all wrong, what you have appended to your form is not imageid
but params
. Also, what you are sending is a javascript object, you need to convert this to a string first. You will need to do the following in JavaScript:
var data = new FormData(this);
var params = $("input[type=submit]", this).data("params"); // parameters to send along with data
var json_params = JSON.stringify(params); // This converts your javascript object into a string that you can send to the server.
data.append("params", json_params); // We are adding the string we have converted from the javascript object.
现在我们已经向服务器发送了JSON字符串,现在我们需要对其进行解码.要在php中解码JSON,我们使用:
Now that we have sent a JSON string to the server, we now need to decode it. To decode the JSON in php we use:
$params = json_decode($_POST['params']);
变量$params
现在将是一个包含imageid
作为属性的php对象.这意味着您的图片ID现在存储在$params->imageid
变量中,例如echo $params->imageid
将输出您的图像ID.
The variable $params
will now be a php object which contains imageid
as a property. This means that your image id is now stored in $params->imageid
variable e.g. echo $params->imageid
will output your image id.
正如@baboizk正确提到的那样,您应该在PHP中使用isset()
来确保它确实存在,然后再使用它.例如
As @baboizk has rightly mentioned, you should use isset()
in PHP to make sure it actually exists before using it. E.g.
$params = json_decode($_POST['params']);
// Check that imageid exists.
if(isset($params->imageid) == true) {
// Do code that needs $params->imageid;
} else {
// Fail gracefully.
}
这篇关于如何从FormData获取PHP文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!