2011-10-21 36 views
0

我想創建一種請求,其中註冊成員可以查看其他註冊成員的帖子。他們可能會選擇與他們聯繫,然後才能對帖子發表評論。我想要的是使用jQuery將請求發送到PHP,然後將其插入數據庫,狀態爲connection requested。當成員看到請求時,他可以選擇接受或拒絕該請求。如果他接受,狀態將更改爲you are connected with this member通過jQuery發送變量到PHP並返回

請讓我知道有類似的問題,但不完全如我的,有人應該請幫忙。

'cont_email' is email of member sending request 
'email_cont' is email receiving the request 
'status' is the status of the connection 
'counter' is request increment 

這是我的HTML部分:

<input name="connect" type="button" value="Connect with&nbsp;<?php  echo"$mem_com[name]";?>" class="intag2" onclick="connt()"/> 

的PHP部分工作正常。我唯一的問題是通過jQuery將請求發送到PHP並返回到現在顯示基於成員活動的連接狀態的jQuery。

+0

您將需要使用ajax post/get方法將變量/ s發送到外部文件。這[鏈接](http://api.jquery.com/jQuery.post)將幫助你 – punit

回答

0

更新答案:

將這個內容放到文件的用戶將看到(我假設泰德的GET參數將被傳遞到該頁面時,對他們的用戶的土地?):

<input id="connectButton" name="connect" type="button" value="<?php echo $status;?>" class="intag2" /> 

<script type="text/javascript"> 
var email_cont = '<?=$_GET[email_cont]?>'; 
var cont_email = '<?=$_GET[email]?>'; 

$('#connectButton').click(function() { // add a click event to the button (no need for onclick) 
    var counter = 0; 
    $.ajax({ 
     url: 'requests.php?cont_email='+cont_email+'&email_cont='+email_cont+'&counter='+counter, 
     success: function(data) { 
      $('#connectButton').val(data); // set the button value to the new status. 
      $('#connectButton').unbind('click'); //stop the user clicking the button loads of times. 
     } 
    }); 
}); 
</script> 

然後把它放在一個名爲requests.php的文件中:

<?php 

    $email_cont = $_GET['email_cont']; 
    $cont_email = $_GET['cont_email']; 
    $counter = $_GET['counter']; 
    $status = "Connection Sent!"; 

    $insert = mysql_query("INSERT INTO request VALUES ('','$cont_email','$email_cont','$status', '$counter', NOW())"); 
    $result = mysql_query($query); 

    if(!result){ 
     //If it fails to run the SQL return an error. 
     echo "Connection Failed!"; 
    }else{ 
     //If all goes well, return the status 
     echo $status; 
    } 

?> 

未經測試,但應該做你正在尋找的東西。

+1

沒問題,如果這樣排序你的問題,請接受回答:) –

+0

你好richi!抱歉打擾你,但我怎麼稱呼阿賈克斯?不是真的擅長它,或者不是它的傑克,但可以很快跟進,當它被解釋..如果我做這樣的事情: –

+0

保持你的'onclick =「connt()」'原樣,幷包裹在一個名爲connt()的函數中使用ajax請求,上面更新了代碼。 –