Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress-将javascript变量传递给php_Php_Javascript_Wordpress - Fatal编程技术网

Wordpress-将javascript变量传递给php

Wordpress-将javascript变量传递给php,php,javascript,wordpress,Php,Javascript,Wordpress,我正在实施一个照片标签系统 在我的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"

我正在实施一个照片标签系统

在我的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");
}
捕捉数据。在我的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!");
        }
    });

});
$('#tagit#btnsave').live('click',function()){
名称=$('#标记名').val();
link=$('#taglink').val();
计数器++;
$(“#标记列表ol”).append(“
  • “+counter+”(Entfernen)
  • ”); $('#imgtag')。附加(''+计数器+''); $('#view'+counter).css({top:mouseY,left:mouseX}); $('#tagit').fadeOut(); $.ajax({ 类型:“POST”, url:“content.php”, 数据:“tag_name=“+name+”&tag_link=“+link+”&pic_x=“+mouseX+”&pic_y=“+mouseY+”&type=insert”, 是的, 成功:功能(数据){ 警惕(“成功!”); } }); });
    不知何故,这些变量没有传递到php,导致我无法将数据正确地保存到数据库中。
    问题一定在$.ajax部分或php中的某个地方。有人能帮我吗?

    请尝试以下代码:

    $.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!");
            }
        });
    
    并将
    if($\u POST['type']==“insert\u tag”)
    行替换为以下内容:

    if($_POST['type'] == "insert")
    
    因为您传递的
    $\u POST['type']
    的值是insert not insert\u标记

    编辑:


    我还发现,在初始化变量之前,您没有使用var。使用变量名=…

    好吧,您正在检查post变量
    $\u post['type']==“insert\u tag”
    ,而它的实际值是:
    &type=insert
    。其他的看起来不错。顺便说一句,永远不要使用
    .live()
    ,它已经过时了,请使用
    .on()


    另外,如果您的所有元素都在一个表单上,您可以使用
    $('your_form_selector')。serialize()
    -这就是您的post数据您的php代码正在寻找'type'等于'insert_tag'


    您的JavaScript使用type=insert发布-因此您的php代码会忽略它。

    为什么要在php代码中重定向?删除此行:

    wp_redirect("http://www.test.com");
    
    你可以用某种简单的反馈系统来代替它。毕竟,您希望服务器端传回状态,可能还有一些验证消息

    $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);
    

    sry刚刚编辑了它。可悲的是,这并不是我的错误,我只是在代码上花了太多的钱,在我把它贴到这里之前忘了编辑它。我编辑了代码,在鼠标后加上“,”。仍然不会触发证明已成功的警报,也不会将任何内容传输到php:S@Cloudzane使用firebug调试您的代码,然后您肯定会在code.thx中找到正确的错误,这只是一个复制/粘贴错误。事实上我有这个权利。令人遗憾的是,仍然无法工作。请尝试使用wordpress ajax传递方法,这是一种更好的方法