switch case not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Annalyzer
    New Member
    • Aug 2007
    • 122

    switch case not working

    My form looks like this:

    Code:
          <form action="handle_event.php" method="POST" enctype="multipart/form-data">
            <table id="event_edit" border="0">
              <tr>
                <td>
                  <label for="title">TITLE: </label>
                </td>
                <td class="input">
                  <input id="title" name="title" type="text" size="80" value="<?php if (!empty($title)) {echo $title;} ?>"  />
                </td>
              </tr>
              <tr>
                <td>
                  <label for="description">DESCRIPTION: </label>
                </td>
                <td class="input">
                  <textarea id="description" name="description" cols="60" rows="10"><?php if ($id) {echo $description;} ?></textarea>
                </td>
              </tr>
              <tr>
                <td><label for="userfile">IMAGE: </label><?php if (!empty($filename)) echo $filename ?></td>
                <td class="input"><input type="file" name="userfile" class="picture" size="67"></td>
              </tr>
              <tr><td>&nbsp;</td></tr>
              <tr>
                <td colspan="2">
                  <div id="admin_buttons">
                    <input type="hidden" name="id" value="<?php if (isset($_GET['id'])) echo $_GET['id']; ?>" />
                    <input type="hidden" name="submitted" value="TRUE" />
                    <input type="image" name="delete" value="delete" src="../images/delete_button.gif" class="admin_button"  onclick="return confirm('Are you sure you want to delete this event?');" />
                    <input type="image" name="update" value="update" src="../images/update_button.gif" class="admin_button" />
                    <input type="image" name="add" value="add" src="../images/add_button.gif" class="admin_button" />
                    <a href="event.php">
                      <input type="image" name="clear" value="clear" src="../images/clear_button.gif" class="admin_button" />
                    </a>
                  </div>
                </td>
              </tr>
            </table>
          </form>
    handle_event.ph p looks like this:

    Code:
    $add_x = $_POST['add_x'];
    $add_y = $_POST['add_y'];
    $update_x = $_POST['update_x'];
    $update_y = $_POST['update_y'];
    $delete_x = $_POST['delete_x'];
    $delete_y = $_POST['delete_y'];
    
    if (!empty($add_x) || !empty($add_y)) {
    	$action = add;
    } else if (!empty($update_x) || !empty($update_y)) {
    	$action = update;
    } else if (!empty($delete_x) || !empty($delete_y)) {
    	$action = delete;
    }
    
    $id = $_POST['id'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    
    if (!empty($_FILES['userfile'])) {
    	$filename = $_FILES['userfile']['name'];
    	$tmpname = $_FILES['userfile']['tmp_name'];
    	$filesize = $_FILES['userfile']['size'];
    	$filetype = $_FILES['userfile']['type'];
    	$error = $_FILES['userfile']['error'];
    
    	$fp      = fopen($tmpname, 'r+');
    	$content = fread($fp, fileSize($tmpname));
    	$content = addslashes($content);
    	fclose($fp);
    	if(!get_magic_quotes_gpc()) {
    		$filename = addslashes($filename);
    	}
    }
    
    switch ($action) {
      case "add":
        $msgHead = "Add an Event";
        /* do a bunch of stuff */
        break;
      case "update":
        $msgHead = "Update an Event";
        /* do a bunch of stuff */
        break;
      case "delete":
        $msgHead = "Delete an Event";
        /* do a bunch of stuff */
        break;
    default:
        echo 'Page accessed in error.';
        break;
    }
    When I click the update image, the switch should execute the statements in the update case. I've echoed the value of $action (it said $action was update) and strlen($action) just to make sure I wasn't getting something extra besides just the string "update" (I wasn't, the length was 6 as expected), but the entire switch case is being ignored and I'm getting "Page accessed in error" every time. Can anyone see what's wrong with my code?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    well, the posted switch code works as expected. but you you should put your strings in quotation marks! (lines 9, 11, 13) unless you have these constants defined.

    to check a variable better use var_dump()

    Comment

    • Annalyzer
      New Member
      • Aug 2007
      • 122

      #3
      Problem solved. It was actually a break missing in the part of the code I didn't give you causing lines to be interpreted that weren't relevant to the case.

      Thanks for your help.

      Comment

      Working...