Jquery $ .post()在HTML

时间:2015-05-23 11:26:07

标签: javascript php jquery html post

我写了一个页面,这样我就可以在用户之间建立友谊。但我的问题是jquery不会发布参数。

我需要一些帮助来解决这个问题。

我的代码:

//currentURL -> e.g: firstname.lastname

$('#friendRequestButton').on("click",function(e){
     if(currentURL != "" && typeof(currentURL) != "undefined"){

          $.post("sendFriendRequest.php",{user1:$.cookie("user-id"),user2:currentURL})
              .done(function(data) {
                   console.log(data); // Here I get the same page(in HTML-format).. as result in the console.
              });
              //$('#friendRequestButton').attr("disabled","true");

     }

});

如果您需要更多详情,请与我们联系。 我的PHP代码:

<?php
    if(isset($_POST['user1']) && isset($_POST['user2'])){

        $user1 = $_POST['user1'];
        $user2 = $_POST['user2']
        die("OK!");
    }else {
        die("NOO!");
    }
?>

编辑:我改变了您提到的e.preventDefault();

的方式
<?php
            $friends = false;
            if(!$friends){
                ?>

                <center>
                    <button class="submit" id="friendRequestButton">Ask for friendship</button>
                    <br />
                    <br />
                    <button>Send Message!</button>
                </center>
                <script>
                    $(document).ready(function(e){
                        $('#friendRequestButton').on("click",function(e){
                        console.log(e.target);
                        e.preventDefault();
                            if(currentURL != "" && typeof(currentURL) != "undefined" && $.cookie("user-id") != "" && typeof($.cookie("user-id")) != "undefined"){

                                $.post("sendFriendRequest.php",{user1:$.cookie("user-id"), user2:currentURL},function(data){
                                        console.log(data);
                                }
                                );
                                //$('#friendRequestButton').attr("disabled","true");

                            }else {
                                console.log("NOOOOO");   
                            }

                        });
                    });



                </script>
            <?php
            }
    ?>

3 个答案:

答案 0 :(得分:3)

没有看到html这是一个猜测,但从你的元素的名称判断你正在使用一个按钮。也许该按钮会提交表格&#34; normal&#34;方式,导致你的页面重新加载,你没有看到你的ajax调用的结果。

如果是这种情况,您需要阻止按下按钮的默认事件(在这种情况下提交表单):

$('#friendRequestButton').on("click",function(e){
  e.preventDefault();
  // the rest of your code

答案 1 :(得分:0)

使用像你这样的ajax请求 -

$交(&#34; sendFriendRequest.php&#34; {USER1:$饼干(&#34;用户ID&#34),用户2:CURRENTURL}),功能(数据){               的console.log(数据);            });

答案 2 :(得分:0)

我找到了问题:

我是SOOOO愚蠢:

我写了这样的文件名:sendFriendRequest..php(两个点) 它应该是:sendFriendRequest.php 所以感谢大家的帮助!! :d