2

I'm implementing a photo tagging system.

in my php file I have:

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:

$('#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. The problem must be somewhere in either the $.ajax part or php. Can someone help me?

1

4 Answers 4

1

Well, you're checking post variable $_POST['type'] == "insert_tag" while it's actual value is: &type=insert. Others look fine. Btw never user .live(), it's obsolete, do it with .on()

Also, if all your elements are on a form you may use $('your_form_selector').serialize() - that'l be your post data

Sign up to request clarification or add additional context in comments.

1 Comment

sry just edited it. that sadly wasn't the mistake i just fundled with the code too much and forgot to edit it before i posted it here
0

Your php code is looking for 'type' to be equal to 'insert_tag'.

Your JavaScript is POSTing with type=insert - so your php code ignores it.

1 Comment

thx that was just a copy/paste mistake. I actually had that right. Still doesn't work sadly..
0

Why are you redirecting in your PHP code? 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);

Comments

0

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")

As the value you are passing of $_POST['type'] is insert not insert_tag.

EDITED:

one more thing as I have found that is you have not used the var before initializing the variable. use the var name = ...

2 Comments

thx I edited the code adding "," after the mouseY. Still doesn't trigger the alert proving it has successed nor does anything get transmitted to the php :S
@Cloudzane use the firebug to debug your code and then you will surely find the proper error in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.