2015-11-28 92 views
0

我不明白JavaScript,AJAX很好,需要快速执行此代码,所以如果你们可以帮忙,那会很棒! 我有我的网页上的类似按钮,可以让用户喜欢的内容:AJAX执行php无刷新页面

的HTML代码如下:

<form action=\"\" method=\"post\" enctype=\"multipart/form-data\"><button name=\"like$i\">like</button></form> 

而且PHP代码:

for ($i = 100; $i >= 0; $i-=1) { 
$primid = latest1($i); 
$id = $user_data['id']; 
if(isset($_POST["like" . $i]) && already_liked($id, $primid)===false) { 
    add_like($id,$primid); 

} 
if(isset($_POST["like" . $i]) && already_liked($id, $primid)===true){ 
    echo "you have already liked this art"; 
} 
} 

所有的代码工作正常,但是当我点击像按钮;页面刷新极其不理想...

我知道你可以使用AJAX来解决这个问题;我已经寻找答案,但他们似乎都是为内容插入等电子表格... 对不起,如果我看起来很懒,但我必须完成这个快速,没有时间正确地学习阿贾克斯的时刻:S

在此先感谢=)

回答

0

遵守以下代码:

jQuery.ajax({ 
    url: '/some/file.php', //link to your php 
    method: 'POST', // Method, you can use GET too 
    data: $('#the-form').serialize() //get form values 
}).done(function (response) { //works if response is success 
    // Do something with the response 
}).fail(function() { //works if response is fail 
    // Whoops; show an error. 
}); 
0

只是编译您需要的所有的东西:

JavaScript代码:

function submitFormData(inputForm) { 
    jQuery.ajax({ 
     url: '/some/file.php', //link to your php 
     method: 'POST', // Method, you can use GET too 
     data: $(inputForm).serialize() //get form values 
    }).done(function (response) { //works if response is success 
     // Do something with the response 
    }).fail(function() { //works if response is fail 
     // Whoops; show an error. 
    }); 
} 

HTML代码:

<form action="" method="post" enctype="multipart/form-data"> 
    <button type="button" onclick="submitFormData(this.form);" name="like$i">like</button> 
</form> 

祝任务的实现。

+1

只要给'类型= “按钮”'到按钮的标签,否则将提交表单因为默认情况下它会考虑为提交按钮。 –

+0

好点。我已经做到了。 –

0

你可以做这样的事情:

HTML

<form action="" method="post" id="likeForm" onsubmit="return false;"> 
    <input type="submit" name="like<?php echo $id; ?>" value="Like" /> 
</form> 

旁注:onsubmit="return false;"是用来防止在阿贾克斯uncaught exception: out of memory错误。

jQuery的

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script> 
    $(document).ready(function(){ 
     $('#likeForm').submit(function(){ 
      var value = $(this).children().attr('name'); 
      var param = {value: value}; 
      $.ajax({ 
       type: 'POST', 
       url: 'yourpage.php', // change this yourpage.php to point to a page where you want to process your ajax request 
       cache: 'false', 
       data: param, 

       beforeSend: function(){ 
        // before send 
       }, 

       success: function(data){ 
        // success 
       }, 

       error: function(){ 
        // error 
       } 
      }); 
     }); 
    }); 
</script> 

yourpage.php

<?php 

    for($i = 100; $i >= 0; $i-=1) { 
     $primid = latest1($i); 
     $id = $user_data['id']; 
     if(isset($_POST['value']) && $_POST['value'] == "like" . $i && already_liked($id, $primid)===false) { 
      add_like($id,$primid); 

     } 
     if(isset($_POST['value']) && $_POST['value'] == "like" . $i && already_liked($id, $primid)===true){ 
      echo "you have already liked this art"; 
     } 
    } 

?>