Jump to content

Recommended Posts

i've got a form that posts data to an insert query on submit, and writes the information to the database. right now, the form action is

[code]
<?php echo $_SERVER['PHP_SELF']; ?>
[/code]

with an if statement at the top of the script that checks if the submit button is set, then it will post to the database, else it will show the form itself. i want to add a "preview" button the same way forums do, like below the message text area, that will take the posted data and open a pop-up with the posted data formatted the way it will look in the site template without losing the data already entered into the form. i have a page, preview.php, which is getting the post data when i test it using

[code]
form action="preview.php"
[/code]

- all i need to know is how to get both submit buttons to do their separate jobs. if i need to break the insert query out of the page (the way it exists right now), i'm willing to do that.
Can't you just run both jobs in the same file ?

Simplified example:
[code]

<?php

$hide_form = false;

if(isset($_POST['submit_preview'])){

  echo htmlspecialchars(nl2br($_POST['message']), ENT_QUOTES);
 
}
elseif(isset($_POST['submit_save'])){

// save posted message

$hide_form = true;
}

if($hide_form == false){

echo <<<_FORM

<form method="post" action="test.php">
<textarea name="message" rows="14" cols="70">$message</textarea>
<input type="submit" name="submit_save" value="Save" />
<input type="submit" name="submit_preview" value="Preview" />
</form>

_FORM;
}

?>

[/code]

...or did i misunderstand you ?
okay, yes... it would have to be an if/elseif/else page if i were to do everything on one page, since it would be if "preview" is pressed, show the preview page, else if "submit" is pressed, write it to the database and show the "success" message, or else show the blank form. if this can work without blanking out the form fields on "preview" then that's it. however, i would like to get the page "preview.php" to be a pop-up this way, and am confused as to how to do that.
so i didn't go with the pop-up, just didn't know how do make that work - but it's fully working as an if/elseif/else page. here's the basic gist of it:

[code]
<?php
  if (isset($_POST['previewbutton'])):                    // if preview button is pressed
      $stuff = $_POST['stuff'];                                // turn post data into variables
      include 'preview.php';                                  // include the "preview" page
      include 'inc/inc.postform.php';                        // and include the form that uses the post data

  elseif (isset($_POST['submit'])):                        // else, if publish button is pressed
      $stuff = $_POST['stuff];                                // turn post data into variables
      $sql = "INSERT INTO table SET stuff='$stuff'";  // insert variables into table
      if (mysql_query($sql)) {                                // if everything's cool
        echo 'New Article added to database.';          // show the success message
      }
      else {                                                        // or else
        echo '<p>An error occurred adding new article: '.mysql_error() . '</p>';  //OOPS!
      }
  else:                                                              // or else, show the blank form
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?> method="post">
<form stuff></formstuff>
<input type="submit" value="Preview" name="previewbutton"/> <input type="submit" value="Publish" name="submit"/>
</form>
[/code]

just for future reference...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.