Simple PHP RSVP Form

I am trying to write what, in my opinion, will be a very simple PHP RSVP page for my (fast-approaching) wedding. I am learning PHP specifically for this task, and everything was fine until I hit the wall. Below are high-level steps on how I envision this work, as well as the code that I have currently written. I really want to do this this week, so any help would be greatly appreciated.

  • The user puts the name and surname and clicks the "Search" button. Here is an image of how the form looks. Below is the HTML form.

            <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>
    
  • In the "Search" window, click, request a database. The following is the PHP code.

            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);
            }
    
  • . , "" " ". this.
  • "RSVP". . . ...

                //This is the code that will eventually update the SQL database with the user responses.
                if (isset($_POST['rsvp_submit'])) {
                    echo 'What do I do now?';
                }
    
  • "RSVP" , .

, (, -) , 4. , . , , , , , , . , .

+4
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;
         .....
    }
}
0

Source: https://habr.com/ru/post/1523985/


All Articles