dimik2 Posted June 8, 2010 Share Posted June 8, 2010 Hi guys I have an input form related problem, I simplyfy it by just showing an example with 1 field. So there is a panel where the user can enter certain informations eg name, birth, address etc and he can preview all the entered information (as many times he likes) just like a comment editor. Every time he pushes the preview button a preview will be generated on the bottom of the page. In order to not to reset out all the fields what he entered I store the data in session variables. If he modifies the field again the session variable will be updated. Here is a snippet about how did I implement this: echo '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">'; echo '<br><input name="data1" size="50" value="'.$_SESSION['data1'].'"><br>'; echo '<br><input type="submit" name="prv" value="Preview">'; echo '</form>'; if (isset($_POST['prv'])) { if (strcmp($_SESSION['data1'], $_POST['data1']) != 0 ) $_SESSION['data1'] = $_POST['data1']; $data1 = $_SESSION['data1']; ... validate input data ... } The problem is that this doesn't behaves as it expected. Once the user hits the preview button it shows the output right but for next time all the forms are empty, after that it's right again. It acts like it would store down everything twice. If I fill out the empty forms their content is saved too. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/ Share on other sites More sharing options...
JakeJ Posted June 8, 2010 Share Posted June 8, 2010 I avoid that problem by splitting the processing of page from the display. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069294 Share on other sites More sharing options...
dimik2 Posted June 8, 2010 Author Share Posted June 8, 2010 I still waiting for a meaningful answer for this. There must be something what I do wrong here. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069661 Share on other sites More sharing options...
kenrbnsn Posted June 8, 2010 Share Posted June 8, 2010 We need to see more of your code. Please put the tags around your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069686 Share on other sites More sharing options...
GoneNowBye Posted June 8, 2010 Share Posted June 8, 2010 also store the session in <input type="hidden" value=[here] name="this" /> at least. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069689 Share on other sites More sharing options...
dimik2 Posted June 8, 2010 Author Share Posted June 8, 2010 also store the session in <input type="hidden" value=[here] name="this" /> at least. I'm sorry but why would I do this? That's the whole point to refresh the data in the input forms all the time so the user can modify it. Just like in this forum where you would change the subject field from "Re: Form FLIP-FLOP" "Re: Form FLIP-FLOP 2" when you reply. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069709 Share on other sites More sharing options...
dimik2 Posted June 10, 2010 Author Share Posted June 10, 2010 Well you asked for the full code but that's pretty much enough code to demonstrate whats going on. Let me make it full for ya: <?php session_start(); echo '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">'; echo '<br><input name="data1" size="50" value="'.$_SESSION['data1'].'"><br>'; echo '<br><input type="submit" name="prv" value="Preview">'; echo '</form>'; if (isset($_POST['prv'])) { if (strcmp($_SESSION['data1'], $_POST['data1']) != 0 ) $_SESSION['data1'] = $_POST['data1']; $data1 = $_SESSION['data1']; } ?> This will perfectly demonstrates whats going on. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070159 Share on other sites More sharing options...
kenrbnsn Posted June 10, 2010 Share Posted June 10, 2010 This works right: <?php session_start(); if (isset($_POST['prv'])) { if (strcmp($_SESSION['data1'], $_POST['data1']) != 0 ) $_SESSION['data1'] = $_POST['data1']; $data1 = $_SESSION['data1']; } echo '<form method="post" action="">'; echo '<br><input name="data1" size="50" value="'.$_SESSION['data1'].'"><br>'; echo '<br><input type="submit" name="prv" value="Preview">'; echo '</form>'; ?> I moved the processing before the form display. Also, you don't need enctype="multipart/form-data". Ken Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070174 Share on other sites More sharing options...
ignace Posted June 10, 2010 Share Posted June 10, 2010 <form method="POST" action="#"> <input name="data1" size="50" value="<?php print isset($_POST['data1']) ? $_POST['data1'] : '' ?>"> <input type="submit" name="prv" value="Preview"> </form> <?php if (isset($_POST['Preview'])) { echo '<div style="width: 500px; height: 500px; overflow: scroll">', applyTemplate($_POST['data1']), '</div>'; } if (isset($_POST['submit'])) { //submit } You don't need session's to create a preview just make sure to auto-populate the form fields. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070243 Share on other sites More sharing options...
dimik2 Posted June 10, 2010 Author Share Posted June 10, 2010 Thanks its solved. It's really about that the php code has to come first even tho I not fully understand why. Quote Link to comment https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070354 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.