Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/204161-form-flip-flop/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1069709
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070159
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070174
Share on other sites

<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.

Link to comment
https://forums.phpfreaks.com/topic/204161-form-flip-flop/#findComment-1070243
Share on other sites

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.