Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

The Event Information Form

To administer the calendar system, we need to be able to add a new event. It's common for PHP forms to act as both form and action, so we'll add the actual form to the saveevent.php file that we created earlier, as shown in Listing 9.

Listing 9—The Event Information Form (saveevent.php)

<head><title>Event Information Form</title></head>
  <h2>Event Information Form</h2>

import_request_variables('p', '');

if (isset($month)) {

  // Connect to database.
  $connection = mysql_connect ("localhost", "myusername", "mypassword")
     || die ( "Error connecting to database!" );

  $insert_statement =
        "insert into events ".
        "(eventMonth, eventDay, eventYear, eventTitle, eventDesc)".
        " values ".
        "($month, $day, $year, '$title', '$description')";

  if (mysql_db_query ( "mysql", $insert_statement)) {
    echo("<p>Event '$title' added.</p>");
  } else {
    die ("Cannot add event.");
  <form action="saveevent.php" method="post">
    Month: <input type="text" name="month" /><br />
    Day: <input type="text" name="day" /><br />
    Year: <input type="text" name="year" /><br />
    Title: <input type="text" name="title" /><br />
    <textarea name="description" cols="60" rows="4"></textarea><br />
    <input type="submit" value="Save Event" />

When the page is loaded, it can be in one of two states:

  • Loaded for the first time, to add a new event

  • Loaded in response to submitting the form, to save an event

If the form has been submitted, each of the form variables (such as $month) will have been set by import_request_variables(), so the simplest way to check the state of the form is to use isset() to check for the presence of one of these variables.

If the form has been submitted, the page takes the information and enters it into the database, displaying a message showing that it has been added successfully, as shown in Figure 8. If not, the dynamic part of the page does nothing. In either case, the form is then displayed.

Figure 8Figure 8 The page displays a message about the added event (if any) and then the form itself.

  • + Share This
  • 🔖 Save To Your Account