Featured post
php - Posting not working via JS (Jquery) but is with form -
i have rather confusing problem.
i have php file (http://example.com/delete.php)
<?php session_start(); $user_id = $_session['user_id']; $logged_in_user = $_session['username']; require_once('../classes/config.php'); require_once('../classes/post.php'); $post = new post(null,$_post['short']); #print_r($post); try { if ($post->user_id == $user_id) { $pdo = new pdoconfig(); $sql = "delete posts id=:id"; $q = $pdo->prepare($sql); $q->execute(array(':id'=>$post->id)); $pdo = null; } else {throw new exception('false');} } catch (exception $e) { echo 'false'; } ?>
and i'm trying jquery post data it, , delete data.
$('.post_delete').bind('click', function(event) { var num = $(this).data('short'); var conf = confirm("delete post? (" + num + ")"); if (conf == true) { var invalid = false; $.post("http://example.com/delete.php", {short: num}, function(data){ if (data == 'false') { alert('deleting failed!'); invalid = true; } }); if (invalid == false) { alert("post has been deleted!"); } else { event.preventdefault(); return false; } } else { event.preventdefault(); return false; } });
and when that, returns "post has been deleted!" not delete post.
confused that, made form test php.
<form action="http://example.com/delete.php" method="post"> <input type="hidden" value="8" name="short"/> <input type="submit" name="submit" value="submit"/> </form>
which works beautifully. odd.
i have code identical deleting of comment, , works great in javascript.
any ideas? beats me.
thanks in advance, will
edit: works... doesn't follow href @ end, desired effect. odd.
$('.post_delete').bind('click', function(event) { var num = $(this).data('short'); var conf = confirm("delete post? (http://lala.in/" + num + ")"); if (conf == true) { var invalid = false; $.post("http://example.com/delete/post.php", {short: num}, function(data){ if (data == 'false') { alert('deleting failed!'); invalid = true; } }); if (invalid == false) { alert("post has been deleted!"); ****************************************** event.preventdefault(); return false; ****************************************** } else { event.preventdefault(); return false; } } else { event.preventdefault(); return false; } });
if php script delete post, doesn't return anything.
my bad, it's not answering real question, still mistake ;)
actually, seems php session , ajax doesn't quite work sometimes.
it means if ($post->user_id == $user_id)
never validate, hence non-deleting problem.
2 ways see :
- log
$user_id
, see if it's notnull
- try send
$_session['user_id']
ajax post , check it. not in production, security reason.
1-
your php should return in every case (at least, when you're looking bug actual case).
<?php [...] try { if ($post->user_id == $user_id) { [...] echo 'true'; } else {throw new exception('false');} } catch (exception $e) { echo 'false'; } ?>
2-
jquery nice use ajax many reasons. example, handles many browsers , make checks moreover, can handle success , error in same .ajax()
/ .post()
/ .get()
function \o/
$('.post_delete').bind('click', function(event) { var num = $(this).data('short'); // if that's data is... fair enough. if (confirm("delete post? (http://lala.in/" + num + ")")) { $.post("delete/post.php", {short: num}, // relative nice :d function(data){ if (data == 'false') { alert('deleting failed!'); }else{ alert("post has been deleted!"); // redirection here ? } }); } });
3-
if need send data form script , redirection, i won't recommand ajax use not leave page ! therefore, should what's in comment, form php script apparently delete , redirection.
- Get link
- X
- Other Apps
Comments
Post a Comment