I'm attempting to develop a series of pages which display text from a database and have an edit button. When you click on the edit button the text becomes editable (using TinyMCE). The code for editing / updating the text works when used in isolation. The code for the edit button to switching to an editable version of the page also works. The problem is, used together, you can click on edit to get the editable version of the page, but any editing doesn't submit properly to the database - it just keeps what was there originally. Here's what I have above the doctype to set up the edit button <?php // process the script only if the form has been submitted if (array_key_exists('edit', $_POST)) { // start the session session_start(); $_SESSION['authenticated'] = 'Edit'; } // if the session variable has been set, show editable content if (isset($_SESSION['authenticated'])) { $view = 'Editable'; } // if the session variable hasn't been set, leave as preview else { $view = 'Preview'; } ?> Then in the body I first include the edit button <?php if ($view == 'Preview') { include('edit_button.php'); } ?> followed by the database connection and the options for regular or editable versions of the text <?php include('db_connect.php'); ?> <?php if ($view == 'Preview') { include('db_preview.php'); } else { include('db_edit.php'); } ?> The code in db_preview simply selects the text from the database and displays it. The code in db_edit.php is as follows: <?php if (isset($_POST['text'])): // The text have been updated. $text = $_POST['text']; $sql = "UPDATE page_text SET text='$text' WHERE id='$id'"; if (@mysql_query($sql)) { echo $text; mysql_close(); } else { echo '<p>Error updating text: ' . mysql_error() . '</p>'; } else: // Allow the user to edit text $page_text = @mysql_query( "SELECT text FROM page_text WHERE id=\"$id\""); if (!$page_text) { exit('<p>Error fetching text: ' . mysql_error() . '</p>'); } $page_text = mysql_fetch_array($page_text); $text = $page_text['text']; // Convert special characters for safe use // as HTML attributes. $text = htmlspecialchars($text); ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label> <textarea id="elm1" name="text" cols="50" rows="10"><?php echo $text; ?></textarea> </label><br /> <input type="submit" value="SUBMIT" /> </form> <?php endif; ?> If I remove the if / else statement for which version of the text to display and just include db_edit.php, I can update the text and click on submit - which then displays the updated text along with the edit button. I can then click on edit to go back to the editable version and make further changes. I do however suspect that what's happening there is that the edit button is just causing the page to reload rather than doing anything more interesting! My guess is that by having 2 bits of code that rely on different $_POST statements (for two different form buttons) there's a clash and they aren't being differentiated between? But if that is the case I don't know how to get around it. Can you have two form buttons on the same page, designed to trigger different things? Any ideas? many thanks Steve
From a quick look it might be to do with the session ... You are checking whether to show editable text or not on a session variable that is only set if the "edit" button has been clicked ... when you click "submit", you don't start / restart the session so you can't check for the variable ... Try moving session_start(); out of the if/the and putting it as your first line of code ...
From the first view, I can see that your edit field are not in the form tag. Also session_start should not be in if/else, put this on the beginning of the file.
The edit button is within a separate form - all of which is in an include edit_button.php which is referenced in the above code. If it all worked properly you'd never see both buttons together, hence two forms
Put <?php endif; ?> in front of HTML form. Right now form is shown only if $_POST['text'] is not set.
Just a thought here but why not store the entire html update in its own session and call back to it with a $_SESSION['myhtml']; versus posting it back and forth?