www.webdeveloper.com
Results 1 to 7 of 7

Thread: Validate - Go Back Button

  1. #1
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64

    Arrow Validate - Go Back Button

    I have a small tell a friend form made up with input fields My Name, My E-mail, Friends Name and Friends E-mail fields and a Send button.

    Each of the fields are scripted like follows:
    <?php
    if (strlen($m_name) <2 )
    {

    $write .="Please enter your Name<BR />";
    $status = "unchecked";
    }
    ?>
    It all works until I introduce a Go Back link facility .....

    To validate I am seeking to install a GoBack link as follows to enable visitor go back to form and do corrections:

    echo '<a href="javascript:history.go(-1)">Go Back to Form</a>';

    When using this arrangement to 'Go Back to Form' all the details already filled in by the visitor have been emptied from the fields and they would have to start filling in the form all over again!

    Is there a simple workround to this ? What am I missing?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    If you want to show the errors on one page and then go to the separate form page, your most likely choice would be to use PHP sessions for the submitted data, and on the form page checking to see if values are populated in the relevant session variables:
    PHP Code:
    <label>Foo: <input type='text' name='foo' value='<?php
    if(!empty($_SESSION['foo'])) { echo htmlspecialchars($_SESSION['foo']); }
    ?>'></label>
    Generally, I like to use the technique where the form and the form-processor is the same file, redisplaying the form if there were any validation errors. In that case, the submitted data will be in the $_POST (or $_GET) array, and then you can use the same technique as above with that array instead of $_SESSION if there were any errors.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Thanks NogDog,
    The sparks around here do not glow very bright, hense I am struggling to get the message in this line of code you provided:
    PHP Code:
    <label>Foo: <input type='text' name='foo' value='<?php
    if(!empty($_SESSION['foo'])) { echo htmlspecialchars($_SESSION['foo']); }
    ?>'></label>
    Does the above get integrated into the following form code at my <label>position?

    <?php
    // we must never forget to start the session
    session_start();
    ?>
    <html><head><title>form.php</title></head><body>
    <form method="post" action="processor.php">
    <label for="user">My Name:</label><input type="text" name="m_name" value="" /><br />
    <input type="submit" name="submitbutton" id="submitbutton" value="Send the Website Link to my Friend" />
    </form></body></html>

    I am using the following processor:
    <?php
    session_start();
    //Form Processor Starts
    $status = "checked";
    $write = "";
    $m_name = $_POST['m_name'];
    //Validate starts here
    if (strlen($m_name) <5 )
    {
    $write .="Please enter your Name<BR />";
    $status= "unchecked";
    }
    if($status=="checked")
    {
    mail();//mail parameters removed
    echo "<font size='3' color=green>Thank You, Your message was sent to your friend";
    }else{//error message
    echo "<font size='2' color=red>$write";
    }
    ?>
    <br /><!--Go Back Link--><a href="javascript:history.go(-1)">Go Back to Form</a>

    Currently not getting fields to maintain information as required even though I have tried different things with the above Foo code!
    -------------------------------------------------------------
    The 2nd part of your response sounds interesting but I would need another day or two to work out.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    You may not be able to do it via PHP if you are going to use the JavaScript history method, as that will most likely just reload the page from the browser cache. You would instead need to make it an actual link to the form page URL.

    Then the idea would be to save the form data to session data in the form handler, so you could simply do something like:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['form_data'] = $_POST;
    // form handler processing
    // ...
    ?>
    <a href="url_to_your_form">Go back to the form</a>
    Then in the form page:
    PHP Code:
    <?php
    session_start
    ();
    ?>
    <form.....>
    <input type='text' name='foo' value='<?php
    if(isset($_SESSION['form_data']['foo'])) { echo htmlspecialchars($_SESSION['form_data']['foo']); }
    ?>' />
    If you want to get fancier, you could add error messages to the $_SESSION array linked to form field names, so that you could highlight the fields with errors. You might want to then clear any such session data after displaying the form. If so, in this example you could just do:
    PHP Code:
    </form> <!-- end of form HTML -->
    <?php
    $_SESSION
    ['form_data'] = null;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    I am not getting the second part of your direction to do with the form. I have put the form up at http://www.shopdemo.webitry.net/
    just to make sure we are on the same track. The form will not work there but I have installed a link to the processor at the bottom of the page source.
    I am not as yet finding the correct arrangement for the foo code in the form page. Will your patients last to provide another pointer!

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Note that "foo" is just a sample form element name. You'll have to replace it with the applicable form element name in each case. Also, if you have a textarea element, you set the value between the tags, not as a "value" attribute within the tag.

    Here's an abbreviated example using just one text element:

    form.php:
    PHP Code:
    <?php
    session_start
    ();
    ?><html><head><title>Test</title></head><body>
    <form action='test.php' method='post'>
       <p><label>Enter something: <input type='text' name='foo' value='<?php
    if(isset($_SESSION['form_data']['foo'])) {
       echo 
    htmlspecialchars($_SESSION['form_data']['foo']);
    }
    ?>' /></label></p>
       <p><input type='submit' value='Enter' /></p>
    </form>
    </body></html>
    test.php (the form-handler):
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['form_data'] = $_POST;
    ?><html><head><title>Form Handler</title></head><body>
    <p>You entered: <br />
    <?php
    if(isset($_POST['foo'])) {
       echo 
    htmlspecialchars($_POST['foo']);
    }
    else {
       echo 
    "(nothing)";
    }
    ?></p>
    <p><a href='form.php'>Back to the form</a></p>
    </body></html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Thanksfor your asistance, got the form working.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles