2012-10-07 26 views
0

我正在開發使用HTML和PHP的一個網站Facebook的...implemantation像按鈕

我已經完成的工作爲我的網頁上像按鈕如下(概念是相同的唯一名字是在Facebook上也不同是和我的網站上它是點)

<form method="post"> 
<input type="hidden" value="<?php echo $posts[postid]; ?>" name="postid"> 
<input type="submit" name="pointsup" value="Points Up" /> 
</form> 

上述將創建名稱點的按鈕。

if(isset($_POST['pointsup'])) { in this if block i have written all queries to update database and user interface and all }

我想是不是那個按鈕,應該有一些鏈接女巫會跑我的SQL代碼。

我也嘗試JavaScript,但它並沒有什麼幫助

預先感謝您的回答!

+1

您可以使用AJAX。這裏是文章:http://www.php4every1.com/tutorials/jquery-ajax-tutorial/ – GBD

回答

0

當您使用單個錨標記(超鏈接)時,應該通過GET方法發送參數。如果在PHP中使用/your/address/rate.php?id=和檢查$_GET['id'](驗證/清理/ ...)

也可以使用AJAX將您的請求作爲POST發送。 這是jQuery的一個樣本:

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" } 
}).done(function(msg) { 
    alert("Data Saved: " + msg); 
}); 

看看here

,但如果你想使用純JavaScript,你應該的XMLHttpRequest對象工作。看看here

0

你應該這樣做:

  1. 找到你應該指向你的鏈接,以及需要什麼樣的參數,我將其稱之爲$ URL。它可能涉及諸如創建附加文件,重構代碼等等......
  2. 編寫js方法,它將發送請求到$ url。我會調用這個方法sendPointsUp;
  3. 創建鏈接,它將對onClick事件做出反應,並從那裏調用帶有所需參數的sendPointsUp。
0

你可以做的只是添加一個鏈接與一個ID,所以你可以很容易地與jQuery達成。
創建一個php頁面,您可以在其中執行查詢和其他內容(安全檢查,Cookie,無論您想做什麼),最後在鏈接中添加一個ajax事件以調用您的php頁面。

打印在你的頁面的鏈接:

<a href="" id="voteup">Vote up</a> 

創建PHP

<?php 
    // do the query and other stuff, return the result in json format 
    // if you want to do something with the result (for example display the votes) 

的Javascript

$('#voteup').click(function(e){ 
    $.ajax({ 
    url: "your/url/to/phpfile", 
}).done(function(data) { 
    // rewrite link to undo vote or whatever you want 
    // do something with the returned data 
}); 
});