Recover data from submit correctly

Discussion in 'PHP' started by afonseca, Sep 3, 2007.

  1. #1
    What can I do when I forgot one field (or bad fill) in a form that is necessary and lost all data that I Input when I submit. If I come back the data is all gone
    How can I recover that data for submit correctly? Any ideas or examples?

    António
     
    afonseca, Sep 3, 2007 IP
  2. alexts

    alexts Well-Known Member

    Messages:
    1,126
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    Digital Goods:
    1
    #2
    I am not sure but i think that you need to use GET method instead of POST in order to achieve this.
     
    alexts, Sep 3, 2007 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    If you are posting the data to the same page as the form, then you can set the "value" of the text box (or other form element) to whatever data has been posted. it is also a good idea to sanitise the data before redisplaying it.

    If you are posting the data to a different page from the form, then set values in the SESSION array, redirect the user to the page that the form is on, and fill the values of the form elements.

    If you have any code, then post it here and I will alter it to show you how to do it

    Brew
     
    Brewster, Sep 3, 2007 IP
  4. afonseca

    afonseca Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi!
    So my form need to have fill the "name", "address" and "etc".
    So when the user dont fill one of 2 selected fields ("address" and "etc") it validates wrong and send it back to the form page.
    At this moment the validation pages works, because I can see the error printed on the page. But when I do a back I dont see any data on the fields (even the ones that had data).
    Ok this is what I have:

    Form page:

    <?php // firstpage.php

    session_start();

    if (isset($_SESSION['form']))
    $vars=$_SESSION['form'];
    else $vars=array();

    // your setup and stuff
    ?>
    ...
    <form action="send2.php" method="post">
    Name: <input type="text" name="name" value="<? if (isset($vars['name']))
    echo $vars['name']; ?>">
    Address: <input type="text" name="foobar" value="<? if (isset($vars['foobar']))
    echo $vars['foobar']; ?>">
    Etc: <input type="text" name="etc" value="<? if (isset($vars['etc']))
    echo $vars['etc']; ?>">
    ...
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>


    Validatin page

    <?php // nextpage.php

    session_start();

    // if the user has submitted the form {
    $_SESSION['form']=$_POST;
    // if the data doesn't validate or whatever {
    // here you could unset() whatever isn't valid
    // say, if their address doesn't look correct, you would {
    //unset($_SESSION["somethingtoidentifythatform"]["address"]);
    // that way the address doesn't get auto-filled in again
    // }
    if(empty($_SESSION['form']['name']) || empty($_SESSION['form']['etc'])){
    echo "<div align='center'><font color='#006633' size=9 face='Verdana, Arial, Helvetica, sans-serif'><h5>Verifique se preencheu correctamente o código de cliente e os respectivos campos obrigatórios (nome e telefone).</h5></font></div>";
    echo $_SESSION['form'] ['name'];
    echo "<div align='center'><font color='#FF0000' size=9 face='Verdana, Arial, Helvetica, sans-serif'><h5><a href='send.php'>Clique aqui para corrigir</a></h5></font></div>";
    //echo " <meta http-equiv=\"refresh\" content=\"5;URL=encomendar.php\"> ";
    // exit;
    }

    //echo "<a href=\"send.php\">Blah blah blah whine whine whine Go fill it in again.</a>\n";
    // }
    // }

    // all good, continue...

    ?>

    Any ideia?

    António
     
    afonseca, Sep 3, 2007 IP
  5. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Here is how I would go about this.

    Setup a file called firstpage.php and copy this code into it:

    <?php
    
    session_start();
    
    // Declare our variables
    $error = false;
    $_SESSION['name'] = '';
    $_SESSION['foobar'] = '';
    $_SESSION['etc'] = '';
    
    if ( isset($_POST['name']) )
    {
    	// Sanitise the data - if you are saving the data to a mysql 
    	// DB, then you should use mysql_real_escape_string() as well
    	$_SESSION['name'] = htmlentities( $_POST['name'] );
    	$_SESSION['foobar'] = htmlentities( $_POST['foobar'] );
    	$_SESSION['etc'] = htmlentities( $_POST['etc'] );
    	
    	// Check the string lengths for all of the posted fields
    	if ( strlen($_POST['name']) < 1 )
    	{
    		echo "Please supply a name<br />";
    		$error = true;
    	}
    
    	if ( strlen($_POST['foobar']) < 1 )
    	{
    		echo "Please supply a foobar<br />";
    		$error = true;
    	}
    
    	if ( strlen($_POST['etc']) < 1 )
    	{
    		echo "Please supply a etc<br />";
    		$error = true;
    	}
    	
    	if ( $error == false )
    	{
    		// If we are here, then there are no errors.. Lets redirect the user to a thank you page
    		header( "Location: thankyou.php" );
    	}
    
    }
    
    ?>
    <html>
    <body>
    <form action="firstpage.php" method="post">
    Name: <input type="text" name="name" value="<? echo $_SESSION['name']; ?>">
    Address: <input type="text" name="foobar" value="<? echo $_SESSION['foobar']; ?>">
    Etc: <input type="text" name="etc" value="<? echo $_SESSION['etc']; ?>">
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>
    PHP:
    Now create a file called thankyou.php and copy this code into it:

    <?php
    
    	session_start();
    	
    	echo "Thanks for the data!  You entered " . $_SESSION['name'] . ", " . 
    		$_SESSION['foobar'] . " and " . $_SESSION['etc'];
    
    ?>
    PHP:
    Now, the form will keep on redisplaying with any submitted data and display errors if the text boxes are not filled in. When they do get filled in, the user is redirected to the thank you page.

    Hope this helps.

    Brew
     
    Brewster, Sep 3, 2007 IP
  6. aRo`

    aRo` Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    another option would be to check the formfields with some javascript validaters
     
    aRo`, Sep 4, 2007 IP
  7. afonseca

    afonseca Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Brew
    Thanks for the code.
    The situation is a little more complicated and I still dont have this completly rigth...

    This form that I have is for validation data from a ecommerce site.
    The client after the purchase has 2 ways of fill the form:
    1- If he is client and fill the client number field ($_SESSION['nr']) the system only requires that he fills the field nome ($_SESSION['nome']) e telefone ($_SESSION['tel']).
    2- If he is client don't fill the client number field ($_SESSION['nr']) the system requires that he fills a list of obligatory fields.

    My problem at this moment whem the client arrives in the page the system presents the "Alert", because the (2º conditions is fill- I mean the client dont fill the data)
    I want that the system makes the validation only when the client fills the fields and submitt form?

    Any ideia?
    António Fonseca

    The code
    -----------------------------------------------------------------------------------------


    <?
    $error = false;

    $_SESSION['nr'] = '';

    $_SESSION['nome'] = '';

    $_SESSION['end'] = '';

    $_SESSION['loc'] = '';

    $_SESSION['cod'] = '';

    $_SESSION['pais'] = '';

    $_SESSION['tel'] = '';

    $_SESSION['fax'] = '';

    $_SESSION['prof'] = '';

    $_SESSION['act'] = '';

    $_SESSION['mail'] = '';

    $_SESSION['nrcont'] = '';

    $_SESSION['msg'] = '';


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

    $valid_cod = ereg("([a-zA-Z0-9]{8})",$_POST['nr']);

    // Sanitise the data - if you are saving the data to a mysql

    // DB, then you should use mysql_real_escape_string() as well

    $_SESSION['nr'] = htmlentities( $_POST['nr'] );

    $_SESSION['nome'] = htmlentities( $_POST['nome'] );

    $_SESSION['end'] = htmlentities( $_POST['end'] );

    $_SESSION['loc'] = htmlentities( $_POST['loc'] );

    $_SESSION['cod'] = htmlentities( $_POST['cod'] );

    $_SESSION['pais'] = htmlentities( $_POST['pais'] );

    $_SESSION['tel'] = htmlentities( $_POST['tel'] );

    $_SESSION['fax'] = htmlentities( $_POST['fax'] );

    $_SESSION['prof'] = htmlentities( $_POST['prof'] );

    $_SESSION['act'] = htmlentities( $_POST['act'] );

    $_SESSION['mail'] = htmlentities( $_POST['mail'] );

    $_SESSION['nrcont'] = htmlentities( $_POST['nrcont'] );

    $_SESSION['msg'] = htmlentities( $_POST['msg'] );



    // Check the string lengths for all of the posted fields

    if ( strlen($_POST['nome']) < 1 )

    {
    echo"<script language='Javascript'>";
    echo"alert ('Por favor preencha o campo nome!')";
    echo"</script>";
    $error = true;
    }

    if ( strlen($_POST['tel']) < 1 )
    {
    echo"<script language='Javascript'>";
    echo"alert ('Por favor preencha o campo telefone!')";
    echo"</script>";
    $error = true;
    }

    else if (!$valid_cod){
    echo"<script language='Javascript'>";
    echo"alert ('Insira um código de cliente valido!')";
    echo"</script>";
    $error = true;
    }

    else {
    header( "Location: formulario.php" );

    }
    }


    else if (empty($_POST['nome']) || empty($_POST['end']) || empty($_POST['loc']) || empty($_POST['cod']) || empty($_POST['pais']) || empty($_POST['tel']) || empty($_POST['nrcont'])) {
    echo"<script language='Javascript'>";
    echo"alert ('Insira todos os campos obrigatorios!')";
    echo"</script>";

    }

    else{
    header( "Location: formulario.php" );
    }

    ?>
     
    afonseca, Sep 4, 2007 IP