vynsane Posted January 30, 2007 Share Posted January 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36339-two-submit-buttons-do-different-things-with-post-data/ Share on other sites More sharing options...
alpine Posted January 30, 2007 Share Posted January 30, 2007 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/36339-two-submit-buttons-do-different-things-with-post-data/#findComment-172829 Share on other sites More sharing options...
cmgmyr Posted January 30, 2007 Share Posted January 30, 2007 Or is this what you want:html:[code]<input type="submit" name="button" value="Preview"> <input type="submit" name="button" value="Submit">[/code]php:[code]if($_POST['button'] == "Preview"){//do this}else{//do this}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36339-two-submit-buttons-do-different-things-with-post-data/#findComment-172832 Share on other sites More sharing options...
vynsane Posted January 30, 2007 Author Share Posted January 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36339-two-submit-buttons-do-different-things-with-post-data/#findComment-172844 Share on other sites More sharing options...
vynsane Posted January 31, 2007 Author Share Posted January 31, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/36339-two-submit-buttons-do-different-things-with-post-data/#findComment-173823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.