2014-01-28 51 views
4

我正在嘗試編寫我認爲對於我的(快速接近)婚禮來說非常簡單的PHP RSVP頁面。我專門爲這項任務學習PHP,並且一切都很順利,直到我碰壁。以下是我設想這項工作的高級步驟,以及我目前編寫的代碼。我真的想在本週完成這個任務,所以任何幫助將不勝感激。簡單的PHP回覆表格

  1. 用戶輸入名字和姓氏並點擊「搜索」按鈕。 Here是窗體外觀的圖像。下面是表單的HTML。

     <form method = "post" action="rsvp.php"> 
          <span>First Name:</span><input type="text" name="firstName" value="Father"> 
          <span>Last Name </span><input type="text" name="lastName" value="Test"> 
          <input type="submit" name="search_submit" value="Search"> 
         </form> 
    
  2. 在「搜索」按鈕上按下,查詢數據庫。以下是PHP代碼。

     if (isset($_POST['search_submit'])) { 
          //Connect to the appropriate database. 
          include 'dbconnect.php'; 
          //Set variables. 
          $firstName = $_POST["firstName"]; 
          $lastName = $_POST["lastName"]; 
          $query = "SELECT * FROM guests WHERE PartyID IN (SELECT PartyID FROM guests WHERE FirstName = '$firstName' AND LastName = '$lastName')"; 
          $result = mysqli_query($sql, $query); 
          //If query doesn't return any results, give an error. 
          if (mysqli_num_rows($result) == 0) { 
           echo "<p id=" . '"' . "searchError" . '"' . ">Sorry, I couldn't find your name. Try again. If you still have an issue please send me an email."; 
          } 
          //If query does return results, create a new form that allows guests to say if they are attending or not. 
          else { 
           echo '<form method = "post" action="rsvp.php"> 
           <span class="guestNames">Party Members</span> 
           <span class="radioButtons">Attending</span> 
           <span class="radioButtons">Not Attending</span> 
           <br/> 
           <br/> 
           '; 
           while ($row = mysqli_fetch_array($result)) { 
            echo '<span class="guestNames">' . $row['FirstName'] 
            . ' ' 
            . $row['LastName'] . '</span> 
            <input type="radio" class="radioButtons" name="' . $row['ID'] . '"value="yes"> 
            <input type="radio" class="radioButtons" name="' . $row['ID'] . '"value="no"> 
            <br /> 
            '; 
           } 
           echo '<input type="submit" name="rsvp_submit" value="RSVP"> 
           </form>'; 
          } 
    
          mysqli_close($sql); 
         } 
    
  3. 創建另一個表單。數據庫返回用戶聚會的所有成員的姓名,以及「出席」或「不出席」單選按鈕選項。它看起來像this
  4. 用戶爲每個成員選擇適當的單選按鈕並點擊「RSVP」按鈕。這是我卡住的地方。見下面...

      //This is the code that will eventually update the SQL database with the user's responses. 
          if (isset($_POST['rsvp_submit'])) { 
           echo 'What do I do now?'; 
          } 
    
  5. 在「RSVP」按鈕上按下,爲每個來賓運行適當的更新語句。

我真的認爲我可以找出所有的數據驗證(確保每個客人都有一個無線電框選擇),並更新語句,一旦我經過第4步。我真的不知道該怎麼辦這個。關於應該做什麼,我有很多想法,但我並沒有比以前更長時間,我希望能從這裏的專家那裏得到一些想法。再次,任何幫助將不勝感激。

+0

首先,在你即將舉行婚禮的祝賀!我可以提出一個建議,而不是強調你自己,你可能想看看[this](http://thydzik.com/online-rsvp-form-and-database-with-php-javascript-and -mysql /)。 –

+0

感謝您的快速回復。實際上,我在發佈之前就下載了它。我做了一個快速掃描,看到了很多我不需要的東西。在我的工作中,我不得不經歷其他人的HTML/CSS足夠的時間,才能意識到構建一些簡單的東西通常比較容易,而不是剝離更復雜的項目以適應您的需求。我希望在這裏找到一些東西,如果失敗了,我會去Hydzik路線。我也對PHP很感興趣,並打算在這個項目完成後繼續使用它。再次感謝你的回覆。 – Krazyeyes21

+0

相信我,我聽到你的聲音。我在婚禮前至少還有一年時間,我正在爲RSVP製作一個網站,試圖在註冊表中掛鉤整個她的聲音。儘管如此,儘管如此,我完全滿足於投擲一個WordPress網站,並且每天都這樣稱呼它。還有很多其他的東西需要強調! –

回答

0

我想你的形式應該是這樣的:

<input type="radio" class="radioButtons" name="guess[' . $row['ID'] . ']" value="yes"> 
<input type="radio" class="radioButtons" name="guess[' . $row['ID'] . ']" value="no"> 

然後,在你的PHP文件:

if (isset($_POST['rsvp_submit'])) { 
    foreach ($_POST['guess'] as $key => $value) { 
     $attending = 0; 
     $not_attending = 0; 
     if ($value == 'yes') { 
      $attending = 1; 
     } else if ($value == 'no') { 
      $not_attending = 1; 
     } 
     $sql = 'UPDATE guess set attending = $attending, not_attending = $not_attending WHERE id = $key; 
     ..... 
    } 
}