session_start(); // So sessions work // Let's start off by creating some blocks if they haven't visited yet if (!isset($_SESSION['session_started'])){ $_SESSION['session_started'] = true; set_up_blocks(); } // Restores blocks to initial data function set_up_blocks() { unset($_SESSION['blocks']); $_SESSION['blocks'][0] = true; $_SESSION['blocks'][1] = "Baby, is this love for real?"; $_SESSION['blocks'][2] = "All I want to do is dance"; $_SESSION['blocks'][3] = "I pray the funk will make me freak"; } // Removes a block function remove_block($id) { global $message; // Unset the array key/value given the id (key) unset($_SESSION['blocks'][$id]); $message = "Block successfully removed!"; } // Remove a block? if ($_GET['delete']) { remove_block($_GET['delete']); } // Reset the example? if ($_GET['reset']) { global $message; $message = "Blocks reset"; set_up_blocks(); } // Here's where we decide what to do based on the request type if ($_GET['ajax']) { // This is an ajax request, spit out message and that's it. echo $message; exit(); // Exiting here prevents further script output. } // If this is not an ajax request, the rest of the page will be rendered as usual ?>
Semantical HTML and DOM Scripting meets remote requests. Read the explination of this example on my journal.
// This bit displays the message in event that a non-ajax request was made ?> if ($message) : ?> endif; ?> // This element is used for ajax requests. Since javascript will the be _only_ thing using this, we put // a style attribute on it of display:none; because we want it to be hidden for degradation ?> if ($_SESSION['blocks']) : ?>Would you like to reset this example? Reset