Jump to content

jquery post() not working with php


Go to solution Solved by requinix,

Recommended Posts

hi, guys im learning to create a friend and block system in php with jquery, but it gives the following error in firebug console in google chrome,

"POST function (){ "parser/friend_sys.php", ({"user":user_f}), function(data){ if(data==1){ $(".block_btn").show(); $(".unfriend_btn").show(); }else{ } } }
404 Not Found "

i checked the file permissions(im using ubuntu 14.04), everything seems to be fine. but since im learning php and ajax i'm stumped can anyone help me out, my php and ajax code is as follows.

<?php
include 'includes/header.php';
error_reporting(0);
$sess_uname=  htmlentities($_SESSION['uname']);
$session_uname=  stripslashes($sess_uname);
$f_uname0=  htmlentities($_GET['name']);
$f_uname= stripslashes($f_uname0);
try {
    




if ($f_uname!=$session_uname) {
    $freind_check="select friend_id from friends where friend_one=':session' and friend_two=':f_name' and accepted='1' limit 1";
    $stmt=$conn->prepare($freind_check);
    $stmt->bindparam(":session",$session_uname);
    $stmt->bindparam(":f_name",$f_uname);
    $stmt->execute();
  
   if ($stmt->fetchColumn()>0) {
     
       $is_friend=TRUE;
   }
   $blockcheck0="select blocked_id from blockedusers where blocker=':f_name' and blockee=':session' limit 1";
   $stmt1=$conn->prepare($blockcheck0);
    $stmt1->bindparam(":session",$session_uname);
    $stmt1->bindparam(":f_name",$f_uname);
    $stmt1->execute();
  
   if($stmt1->fetchColumn()>0)
   {
     $ownerblockviewer=TRUE;
   }
   $blockcheck2="select blocked_id from blockedusers whereblocker=':session' and blockee=':f_uname' limit 1";
    $stmt2=$conn->prepare($blockcheck0);
    $stmt2->bindparam(":session",$session_uname);
    $stmt2->bindparam(":f_name",$f_uname);
    $stmt2->execute();
  
   if($stmt2->fetchColumn()>0)
   {
     $viewerblockowner=TRUE;
   }
}


 $friend_button='<button class="btn btn-warning" disabled>Add as a friend</button>';
 $block_button='<button class="btn btn-warning" disabled>Block user</button>';
 #logic for friend button
 if ($is_friend==TRUE) {
    $friend_button='<button name="unfreind_btn" class="unfreind_btn btn btn-warning">unfriend</button>';
}
elseif ($_SESSION['app']==TRUE&&$f_uname!=$session_uname&&$ownerblockviewer==FALSE) {
$friend_button='<button name="friend_btn" class="friend_btn btn btn-warning">Request As friend</button>'; 
}
#logic for block button
if($viewerblockowner==TRUE)
    {
    $block_button='<button name="unblock_btn" class="unblock_btn btn btn-warning">Unblock</button>';
    
    }
 elseif ($user_ok==TRUE&&$f_uname !=$session_uname) {
    $block_button='<button name="block_btn" class="block_btn btn btn-warning">Block</button>';
}
if (isset($_SESSION['app'])&&$session_uname!="") {


                   } else {
                      header("location: index.php");
                   }
} catch (Exception $exc) {
    echo $exc->getTraceAsString();
}
?>
<div class="container-fluid">
<br><div class="row"> </div><br>
<div class="row">
    <div class="col-sm-2">


</div>    
<div class="col-lg-8">
<div class="tab-content">
<ul class="nav nav-tabs">
<li><a href="#tabs-1" data-toggle="tab" class="profile">profile</a></li>
<li class="active"><a href="#tabs-2" data-toggle="tab"class="articles">articles</a></li>
<li ><a href="#tabs-4"data-toggle="tab" class="gallery_photos">Gallery</a></li>
<li><a href="#tabs-5" data-toggle="tab" class="videos">Videos</a></li>


</ul>
<div id="tabs-1" class="tab-pane fade profile1 ">
  <?php 
    echo '<table border="0"><tr width="20%"><td>Profile Photo</td><td>'.$friend_button.'</td></tr>
        <tr><td></td><td>'.$block_button.'</td></tr>
        <tr width="100%"><td width="20%"></td><td width="55%"><p>Full Name:'.$fname_s.' '.$lname_s.'<br>
            
</td><td width="20%"></td></tr>
    </table>'; 
    ?>
</div>
<div id="tabs-2" class="tab-pane fade articles"><legend></legend>Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.
</div>
<div id="tabs-4"class="tab-pane  fade gal_photos">
<?php 
include 'others/gallery.php';
?>    
</div>
<div id="tabs-5" class="tab-pane fade vids">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.
</div>
</div>


</div>
<div class="col-sm-2">


</div>  
</div>
</div>
<script type="text/javascript">
    var spinner='<img src="img/spinner.gif">';
$(".fancybox-effects").fancybox();
$("button.friend_btn").click(function(){
    $("button .friend-btn").show(spinner);
    alert("friend button clicked");
    var user_f=$(this).attr('user');
$.post(function(){
    "parser/friend_sys.php",
    ({"user":user_f}),
    function(data){
        if(data==1){
        $(".block_btn").show();
        $(".unfriend_btn").show();
        }else{
            
        }
    }
});
    $(".friend-btn").html(spinner).fadeOut(250);
});
</script>
</body>
</html>

the friend_sys.php code is as follows:

 

<?php
include '../includes/dbconfig.inc.php';


$session_f_uname=  htmlentities($_SESSION['uname']);
 $user=  htmlentities($_POST['user']);
 $sql="select count(user_id) from user where uname=':uname' and activated='1' limit 1";
 $stmt=$conn->prepare($sql);
 $stmt->bindparam(":uname", $session_f_uname);
$stmt->execute();
if($stmt->rowCount()<1)
{
    echo 'user doesnt exist';
    exit();
}
if (isset($_POST['user'])) {
    $sql="select count(blocked_id) from blockedusers where blocker=':f_uname' and blockee=':session' limit 1";
    $stmt=$conn->prepare($sql);
    $stmt->bindparam(":f_uname",$user);
    $stmt->bindparam(":session",$session_f_uname);
    $stmt->execute();
    $block_count0=$stmt->rowCount();
    $sql1="select count(blocked_id) from blockedusers where blocker=':session' and blockee=':f_uname' limit 1";
    $stmt1=$conn->prepare($sql1);
    $stmt1->bindparam(":f_uname",$user);
    $stmt1->bindparam(":session",$session_f_uname);
    $stmt1->execute();
    $block_count1=$stmt1->rowCount();   
    $sql2="select count(friend_id) from friends where friend_one=':session' and friend_two=':f_uname' and accepted='1' limit 1";
    $stmt2=$conn->prepare($sql2);
    $stmt2->bindparam(":f_uname",$user);
    $stmt2->bindparam(":session",$session_f_uname);
    $stmt2->execute();
    $row_count0=$stmt2->rowCount();
    $sql3="select count(friend_id) from friends where friend_one=':f_uname' and friend_two=':session' and accepted='1' limit 1";
    $stmt3=$conn->prepare($sql3);
    $stmt3->bindparam(":f_uname",$user);
    $stmt3->bindparam(":session",$session_f_uname);
    $stmt3->execute();
    $row_count1=$stmt3->rowCount();
     $sql4="select count(friend_id) from friends where friend_one=':session' and friend_two=':f_uname' and accepted='0' limit 1";
    $stmt4=$conn->prepare($sql4);
    $stmt4->bindparam(":f_uname",$user);
    $stmt4->bindparam(":session",$session_f_uname);
    $stmt4->execute();
    $row_count2=$stmt4->rowCount();
    $sql5="select count(friend_id) from friends where friend_one=':f_uname' and friend_two=':session' and accepted='0' limit 1";
    $stmt5=$conn->prepare($sql5);
    $stmt5->bindparam(":f_uname",$user);
    $stmt5->bindparam(":session",$session_f_uname);
    $stmt5->execute();
    $row_count3=$stmt5->rowCount();


    if ($block_count0[0]>0) {
        echo '<script>alert("This user has blocked you, We cannot proceed");</script>';
        exit();
    }
    elseif ($block_count1[0]>0) {
    echo '<script>alert("You must unblock this '.$user.'in order to proceed");</script>';
        exit();
}
elseif ($row_count0[0]>0 ||$row_count1[0]>0) {
echo '<script>alert("You are already friends with this '.$user.'");</script>';
        exit();
}
elseif ($row_count2[0]>0) {
echo '<script>alert("You already have a pending friend request already sent to '.$user.'");</script>';
        exit();
}
elseif ($row_count3[0]>0) {
echo '<script>alert("'.$user.' has requested to friend with you first. Check your friend request");</script>';
        exit();
}
 else {
    $sql="insert into friends (friend_one, friend_two,date_made) values(':session',':f_uname',now())";
$stmt=$conn->prepare($sql);
$stmt->bindparam(":session",$session_f_uname);
$stmt->bindparam(":f_uname",$user);    
 $stmt->execute();
 echo '<script>alert("Friend request sent");</script>';
        exit();
 }




}








if (isset($_SESSION['app'])&&$session_f_uname!="") {
    header("location: ../home.php?u=".$session_f_uname);
} else {
    header("location: ../index.php");
}


Edited by shan
Link to comment
https://forums.phpfreaks.com/topic/298333-jquery-post-not-working-with-php/
Share on other sites

thanks for the reply@ch0cu3r  :happy-04:

now im facing a new problem its just that the file is now inserting ":session" and ":f_uname" instead of values in the database. since this is the first time im facing this problem im perplexed where to start debugging, can you help me on this??.

i'll attach a screen shot of phpmyadmin  for clarifications.12046758_1135617329800809_25274618424023

 

see the 7th id its storing like this.

  • Solution

Don't put the placeholders (which is what they are) inside of quotes. They aren't strings but, well, placeholders. Placeholders for real values which will come after you've called bindparam() and execute().

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.