【问题标题】:Simple PHP RSVP Form简单的 PHP RSVP 表单
【发布时间】:2014-02-19 03:18:02
【问题描述】:

我正在尝试为我(即将到来的)婚礼编写一个我认为非常简单的 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”按钮后,为每位客人运行相应的更新语句。

我真的认为我可以弄清楚所有数据验证(确保每个客人都选择了一个单选框)并在我完成第 4 步后更新语句。我真的不知道如何处理这个问题。我对应该做什么有很多想法,但我希望能从这里的专家那里得到一些想法,而不是比我已经有的更啰嗦。同样,我们将不胜感激任何帮助。

【问题讨论】:

  • 首先恭喜你们即将举行婚礼!不过,我可以提个建议吗,而不是为此给自己压力,你可能想看看this
  • 感谢您的快速回复。实际上,我只是在发布此内容之前下载了该内容。我快速浏览了一下,发现里面有很多我并不真正需要的东西。在我的工作中,我不得不多次阅读其他人的 HTML/CSS,以意识到构建简单的东西通常比剥离更复杂的项目来满足您的需求更容易。我希望在这里找到一些东西,如果失败了,我会走 Hydzik 路线。我也对 PHP 产生了浓厚的兴趣,并打算在这个项目完成后继续使用它。再次感谢您的回复。
  • 相信我,我听到了。我在婚礼前至少还有一年的时间,我要为 RSVP 做一个网站,试图加入一个注册表,整个 she-bang。不过,在那之前,我完全满足于建立一个 WordPress 网站并称其为一天。还有很多其他的事情需要强调!

标签: php mysql


【解决方案1】:

我认为你的表格应该是这样的:

<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;
         .....
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2016-08-21
    相关资源
    最近更新 更多