简单的PHP RSVP表单

时间:2014-01-28 01:28:46

标签: php mysql

我正在尝试为我的(快速接近的)婚礼编写我认为非常简单的PHP RSVP页面。我正在专门为这项任务学习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”按钮,按每个来宾运行相应的更新声明。
  6. 我真的认为我可以弄清楚所有数据验证(确保每个客户都选择了一个无线电盒)并在我越过第4步后更新语句。我真的不知道如何处理这个问题。我对应该做什么有很多想法,但不是比我现有的更长时间,我希望我能从这里的专家那里得到一些想法。再次,非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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;
         .....
    }
}