Reputation: 23
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?
Upvotes: 2
Views: 3208
Reputation: 15603
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 = ...
Upvotes: 0
Reputation: 5693
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);
Upvotes: 0
Reputation: 10117
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.
Upvotes: 0
Reputation: 6948
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
Upvotes: 1