[英]Wordpress - Passing javascript variable to php
I'm implementing a photo tagging system. 我正在实施照片标记系统。
in my php file I have: 在我的php文件中,我有:
if($_POST['type'] == "insert") {
$pid = $post->ID;
$tag_name = $_POST['tag_name'];
$tag_link = $_POST['tag_link'];
$pic_x = $_POST['pic_x'];
$pic_y = $_POST['pic_y'];
$arr = array("tag_name" => $tag_name, "tag_link" => $tag_link, "pic_x" => $pic_x, "pic_y" => $pic_y);
add_photo_tag($pid, $arr);
wp_redirect("http://www.test.com");
}
to catch the data. 捕捉数据。 in my js file i have:
在我的js文件中,我有:
$('#tagit #btnsave').live('click',function(){
name = $('#tagname').val();
link = $('#taglink').val();
counter++;
$('#taglist ol').append('<li rel="'+counter+'">'+counter+'. <a href="'+link+'#test_id" target="new">'+name+'</a> (<a class="remove">Entfernen</a>)</li>');
$('#imgtag').append('<div class="tagview" id="view_'+counter+'">'+counter+'</div>');
$('#view_' + counter).css({top:mouseY,left:mouseX});
$('#tagit').fadeOut();
$.ajax({
type: "POST",
url: "content.php",
data: "tag_name=" + name + "&tag_link=" + link + "&pic_x=" + mouseX + "&pic_y=" + mouseY + "&type=insert",
cache: true,
success: function(data) {
alert("success!");
}
});
});
Somehow the variables don't get passed to the php resulting in me not able to save the data properly to database. 不知何故,变量没有传递到php,导致我无法将数据正确保存到数据库。 The problem must be somewhere in either the $.ajax part or php.
问题必须在$ .ajax部分或php中。 Can someone help me?
有人能帮我吗?
Well, you're checking post variable $_POST['type'] == "insert_tag"
while it's actual value is: &type=insert
. 好吧,您正在检查发布变量
$_POST['type'] == "insert_tag"
而实际值是: &type=insert
。 Others look fine. 其他人看起来很好。 Btw never user
.live()
, it's obsolete, do it with .on()
顺便说一句永远不要使用
.live()
,它已经过时了,请使用.on()
Also, if all your elements are on a form you may use $('your_form_selector').serialize()
- that'l be your post data 另外,如果所有元素都在表单上,则可以使用
$('your_form_selector').serialize()
-这就是您的帖子数据
Try with the following code: 尝试以下代码:
$.ajax({
type: "POST",
url: "content.php",
data: {"tag_name": name,"tag_link":link,"pic_x": mouseX,"pic_y":mouseY, "type":"insert"},
//change here like I have done
cache: true,
success: function(data) {
alert("success!");
}
});
And Replace the if($_POST['type'] == "insert_tag")
line with following: 然后将
if($_POST['type'] == "insert_tag")
行替换为以下内容:
if($_POST['type'] == "insert")
As the value you are passing of $_POST['type']
is insert not insert_tag. 由于您传递的
$_POST['type']
是插入而不是insert_tag。
EDITED: 编辑:
one more thing as I have found that is you have not used the var before initializing the variable. 我发现的另一件事是您在初始化变量之前没有使用过var。 use the var name = ...
使用var name = ...
Your php code is looking for 'type' to be equal to 'insert_tag'. 您的php代码正在寻找“类型”等于“ insert_tag”。
Your JavaScript is POSTing with type=insert - so your php code ignores it. 您的JavaScript正在使用type = insert进行发布-因此您的php代码将忽略它。
Why are you redirecting in your PHP code? 为什么要重定向PHP代码? Remove this line:
删除此行:
wp_redirect("http://www.test.com");
You can replace it with some sort of a simple feedback system. 您可以将其替换为某种简单的反馈系统。 After all you want the server-side to pass back the status and maybe some validation messages.
毕竟,您希望服务器端传回状态以及一些验证消息。
$return = array('status' => 0, 'msg' => 'all is well');
if(!add_photo_tag($pid, $arr)) {
$return['msg'] = __('Could not add photo tag.');
$return['status'] = -1;
}
echo json_encode($return);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.