2012-02-11 52 views
0

我想讓用戶通過提交按鈕接受/拒絕事件的請求。信息被循環顯示(用戶名,位置,接受,拒絕)。接受/拒絕提交按鈕循環與PHP

現在2個用戶正在顯示;用戶1和用戶2(當前用於測試)。無論如何,我試圖讓正確的用戶名使用正確的用戶名。目前,無論我接受或拒絕哪個用戶,都會顯示用戶2。我試圖根據隱藏的輸入設置用戶標識的值,但它不能正常工作。

這是我的代碼。

for($i=0;$i<count($userExplode)-1;$i++){ 
      $user = mysql_query("select userid,username from users where userid = ".$userExplode[$i]." "); 
      $user = mysql_fetch_array($user); 
      $userLoc = mysql_query("select userLocation from userinfo where userid = ".$userExplode[$i]." "); 
      $location = mysql_fetch_array($userLoc); 
      $locationExplode = explode("~",$location['userLocation']); 

//the displayed output is working correctly so I know it's setting $user['userid'] properly 
      echo '<form method="post"><table><tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td> 
       <td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td> 
       <td><input type="hidden" name="userReq" value='.$user['userid'].'></td> 
       <td><input type="submit" name="accept" value="Accept Request" ></td> 
       <td><input type="submit" name="decline" value="Decline Request" ></td></tr>'; 
      } 
       echo '</table></form>'; 
     } 
      if(isset($_POST['accept'])){ 
       echo $_POST['userReq']; //displays user 2 even if I click user 1 
        } 
      if(isset($_POST['decline'])){ 
       echo $_POST['userReq']; //also displays user 2 even if i click user 1 
      } 
    } 

回答

2

有很多你的代碼錯誤:

  • 你不會在環路
  • 關閉for的存在表單
  • 你在無動作開始一個新的表中的每個循環

更換錨通過提交按鈕的,並在那裏添加參數。然後,您可以將錨點的樣式設置爲按鈕。

<input type="submit" name="decline" value="Decline Request" > 

成爲

echo "<a href="yourfile.php?userid=".$user['userid'].">Decline request</a>"; 

刪除所有<form>秒和1臺裝載的一切。

結果:

<?php 
if(isset($_GET['action']) AND isset($_GET['userid'])){ 

    switch($_GET['action']){ 
     case "accept": 
      // do whatever 
     break; 
     case "decline": 
      // do whatever 
     break; 
     default: 
      die('something wrong'); 
     break; 

    } 

} 

echo '<table width="100%">'; 

for($i=0; $i <= count($userExplode); $i++){ 
    $q = " 
     SELECT u.userid,u.username, userLocation 
      FROM users u 
    INNER JOIN userLocation ul ON u.userid = ul.userid 
     WHERE u.userid = ".$userExplode[$i]." 
    "; 
    $rs = mysql_query($q) or die(mysql_error());  
    $user = mysql_fetch_array($rs); 

    $locationExplode = explode("~",$user['userLocation']); 

    //the displayed output is working correctly so I know it's setting $user['userid'] properly 
    echo '<tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td>'. 
     '<td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>'. 
     '<td><a href="yourfile.php?userid=' . $user['userid'] . '&action=accept">Accept Request</a></td>'. 
     '<td><a href="yourfile.php?userid=' . $user['userid'] . '&action=decline">Decline Request</a></td></tr>'; 

} 

echo '</table>'; 

我認爲這僅僅是冰山的一角..你怎麼$userExplode例如?這是非常奇怪和不合邏輯的。我假設你首先運行一個查詢來獲取所有用戶,然後循環使用它?

+0

你應該使用'$ _GET'而不是'$ _POST' – Gaurav 2012-02-11 19:59:03

+0

感謝您的提示!使用$ _GET來安全嗎?我想我只需再次檢查其他頁面,只有在事件的創建者是訪問該頁面的頁面時才允許。 – user1104854 2012-02-11 20:02:42

+0

如果您安全使用它是安全的。只有管​​理員纔可以提出這些要求。 – Richard 2012-02-11 20:04:21

1

在你的例子中,它看起來像你有一些代碼問題。這是清理了一點:

for($i=0;$i<count($userExplode)-1;$i++) { 
     $user = mysql_query("select userid,username from users where userid = ".$userExplode[$i]." LIMIT 1"); 
     $user = mysql_fetch_array($user); 
     $userLoc = mysql_query("select userLocation from userinfo where userid = ".$userExplode[$i]." LIMIT 1"); 
     $location = mysql_fetch_array($userLoc); 
     $locationExplode = explode("~",$location['userLocation']); 
     echo '<form method="post" action=""><table><tr><td><a href="profile.php?userid=' . $user['userid'] . '">' . $user['username'] . '</a></td> 
      <td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td> 
      <td><input type="hidden" name="userReq" value='.$user['userid'].'></td> 
      <td><input type="submit" name="accept" value="Accept Request" ></td> 
      <td><input type="submit" name="decline" value="Decline Request" ></td></tr>'; 
     echo '</table></form>'; 

    } 

    if(isset($_POST['accept'])){ 
      echo $_POST['userReq']; //displays user 2 even if I click user 1 
    } 

    if(isset($_POST['decline'])){ 
      echo $_POST['userReq']; //also displays user 2 even if i click user 1   
    } 

基於您的代碼,這是正在傳遞的數組應該是:

 $users = "1|2"; 
    $userExplode=explode("|",$users) // or whatever your delimiter is 

要進行測試,做一個開始你的循環var_dump($userExplode);之前,確保你已經爆炸了條目。

原始頁面的源轉儲也會有幫助。如果你可以發佈,那麼我可以看到你的代碼是如何呈現html表單的。