corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Lesson 7: Editing the Wish List - Editing and Deleting Wishes The document applies to NetBeans 6.1 only

Contributed by Barbara Mityashina
June 27, 2008

Contents

Application Source Code from the Previous Lesson

Click here to download the source code that reflects the project state after the previous lesson is completed.

Lesson Scope

In this lesson you will expand the application's functionality with two features:

The implementation of these two features will affect the editWishList.php and editWish.php files. A new file deleteWish.php will be created.

Editing a Wish

The functionality supports the following use case:
  • On the editWishList.php page, the user presses the Edit button to the right of a wish. The editWish.php page with the data of the selected wish opens.
  • The user changes the description and/or the due date of the wish and presses the Save Changes button.
  • If the description is not filled in, an error message is displayed and the user returns to the editWish.php page.
  • Else, if the description is filled in, the application returns to the editWishList.php page where the wish is updated.

The implementation consists of the following steps:

Implementing the Edit Button

A table with the wishes of a wisher is implemented by means of a loop that displays rows with wishes while the wishes are selected from the database. The suggested approach is to display an Edit button as the extreme right cell in a row.

  1. To transfer the ID of a wish through the HTML input form, store it in a variable. Enter the following code line inside the loop:
    $wishID = $row["ID"];
    ?>
  2. To implement the Edit button, enter the following code inside the "while" loop::
        <td>
          <form name="editWish" action="editWish.php" method="GET">
           <input type="hidden" name="wishID" value="<?php echo $wishID; ?>"/>
           <input type="submit" name="editWish" value="Edit"/>
          </form>
        </td>
    <?php
       echo "</tr>\n";
    The form contains a submit field, which implements the Edit button, and a hidden field wishID for transferring the ID of the wish displayed to left of the Edit button. The ID of the wish is stored in the $wishID variable.

Expanding the Array $wish

Upon pressing the Edit button on the editWishList.php page, the ID of the selected wish is transferred to the editWish.php page through the Server Request method GET. To store the id of the wish, you need to add a new element to the array $wish.

Like in the adding a new wish use case, the input form can be accessed both from the editWishList.php page and from the editWish.php page after an unsuccessful attempt to save. The cases are distinguished by the Server Request method through which the data is transferred. GET indicates that the form is displayed when the user first gets to the page by pressing Edit Wish. POST indicates that the user is redirected to the form after attempting to save a wish without a description.

Update the PHP block above the HTML input form as follows:

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST")
$wish = array("id" => $_POST["wishID"],
"description" => $_POST["wish"],
"due_date" => $_POST["dueDate"]);
else
if (array_key_exists("wishID", $_GET))
$wish = mysql_fetch_array($db->get_wish_by_wish_id($_GET["wishID"]));
else
$wish = array("id" => "",
"description" => "",
"due_date" => "");
?>

The code initializes an array $wish with three elements: id, description, and due_date. The values of these elements depend on the Server Request method. If the Server Request method is POST, the values are received from the input form. Otherwise, if the Server Request method is GET and the $_GET array contains an element with the key "wishID", the values are retrieved from the database by the function get_wish_by_wish_id. Finally, if the Server Request method is neither POST nor GET, which means the Add New Wish use case takes place, the elements are empty.

The preceding code covers the cases for creation and editing wishes. Now you need to update the input form so that it can be also used for both cases.

Updating the HTML Input Form - Unifying the Implementation of Creation and Editing

Currently the input form works for creation of a new wish when the wish has no id. To unify that tasks of creating and editing wishes, you need to add a hidden field for transferring the ID of a wish. The value of the hidden fields must be retrieved from the array $wish. The value must be an empty string during the creation of a new wish and filled in with the ID of the wish in case of editing. Add the following line to the HTML input form:
<input type="hidden" name="wishID" value="<?php echo $wish["ID"];?>" />

Optimizing the Code

Now you need to update the code because the current verifications do not distinguish the creating a new wish case and the updating an existing wish case. In the current implementation, a new record is always added to the database because the code does not verify the value of the wish id transferred from the input form.

You need to add the following verifications:

  • If the transferred element "wishID" is an empty string, a new wish must be created.
  • Otherwise, if the element "wishID" is not an empty string, the wish must be updated.
To implement the preceding verifications, add a condition to the last else statement in the main PHP block:
<?php

   else if ($_POST["wishID"]=="") {
InsertWish($wisherId, $_POST["wish"], $_POST["dueDate"]);
header('Location: editWishList.php' );
exit;
}
?>

Updating the Wish in the Database

To implement the Update Wish functionality, enter the following code below the block that implements the Create new wish functionality:

else if ($_POST["wishID"]!="") {
$db->update_wish($_POST["wishID"], $_POST["wish"], $_POST["dueDate"]);
header('Location: editWishList.php' );
exit;
}

The code checks that the element "wishID" in the $_POST array is not an empty string, which means that the user was redirected from the editWishList.php page by pressing the Edit button and that the user has filled in the description of the wish. If the check is successful, the code calls the function update_wish with the input parameters wishID, description, and dueDate received from the HTML input form through the method POST. After that the application is redirected to the editWishList.php page and the PHP processing is canceled.

Testing the Edit Wish Functionality

  1. Run the application. On the index.php page, fill in the fields: in the Username field, enter "Tom", in the Password field, enter "tomcat".
    The user logs on to edit their wish list
  2. Press the Edit My Wish List button. The editWishList.php page opens.
    The Edit button is added to the editWishList.php page
  3. Click Edit next to Icecream. The editWish.php page opens.
    The editWish.php page with a form for editing a wish. The fields are filled in with the wish data.
  4. Edit the fields and press Back to the List. The editWishList.php page opens but the changes are not saved.
  5. Press Edit next to Icecream. Clear the Describe your wish field and press Save Changes. An error message is displayed.
    The  form for editing wish shows anerror message: the description is not filled in
  6. Enter Chocolate icecream in the Describe your wish field and press Save Changes. The editWishList.php page opens with the updated list.
    editWishList.php page: the updatedwish is on the list

Deleting a Wish

Implementing the functionality consists of two steps:

  • Implementing deletion from the database in a new deleteWish.php file
  • Adding a Delete button in the list of wishes on the editWishList.php page
  1. Create a new PHP file deleteWish.php and enter the following code into the <? php ?> block:
    require_once("Includes/db.php");
    $db = new WishDB;
    $db->delete_wish ($_POST["wishID"]);
    header('Location: editWishList.php' );
    The code enables the use of the db.php file and creates a new $db object of the class WishDB. Then the function delete_wish is called with the wishID as the input parameter. Finally, the application is redirected to the editWishList.php page.
  2. To implement the Delete button, enter the following code block inside the while loop in the editWishList file:
    <td>
          <form name="deleteWish" action="deleteWish.php" method="POST">
           <input type="hidden" name="wishID" value="<?php echo $wishID; ?>"/>
           <input type="submit" name="deleteWish" value="Delete"/>
          </form>
    </td>
    The HTML input form contains a hidden field for the wishID and a submit field Delete.

Testing the Delete Wish Functionality

To check that the functionality is implemented correctly, press Delete next to any item on the editWishList.php page. The item is no longer on the list. editWishList.php page: the wish is deleted

Application Source Code after the Current Lesson Is Completed

Click here to download the source code that reflects the project state after the lesson is completed


<< Previous lesson

Next lesson >>

Back to the Tutorial main page

Back to the PHP Learning Trail

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems