SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Page not remembering the forms contents ?

    is there any way, that when a form is incorrectly completed and you click send, the next page says that some of the forms questions haven't been answered properly, and then you click the back button to go back to the form, is there any way that the parts that you did complete, would still be in the form ? This is in formmail.cgi Is there any way of doing this, without anything to do with PHP please ?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It all depends on which browser is being used. Some keep the form content and redisplay it when you go back and some don't.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Stephen, is there an easy way to do it, without any php, where it would, in any browser, remember the details please ?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Not if they press the back button to go back since then their browser has full control and if the browser doesn't retain what they typed then there is nothing you can do to put it back into the form.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,172
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    It is quite easy to do with PHP (and you don't need to know much about PHP, either). Is there any reason you don't want to use PHP?

  6. #6
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    It is quite easy to do with PHP (and you don't need to know much about PHP, either). Is there any reason you don't want to use PHP?
    Thanks Ralph and Stephen. I have
    Code:
    <?php
    session_start();
    at the very top of the page for something else, and guess there might be a problem trying to get another piece of php code at the very top of the page for the form completion ?

  7. #7
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,172
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by Dez View Post
    Thanks Ralph and Stephen. I have
    Code:
    <?php
    session_start();
    at the very top of the page for something else, and guess there might be a problem trying to get another piece of php code at the very top of the page for the form completion ?
    No, there shouldn't be any problem at all. You just close off one PHP instruction and start another. E.g.

    <?php
    one set of instructions
    ?>

    <?php
    another set of instructions
    ?>

  8. #8
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by ralph.m View Post
    No, there shouldn't be any problem at all. You just close off one PHP instruction and start another. E.g.

    <?php
    one set of instructions
    ?>

    <?php
    another set of instructions
    ?>
    Aaaah, thanks Ralph, I just thought that the php code for the form completion, had to go at the very top of the page ? ? If not, then, yep, you've convinced me What would be an example of a complete php code, for a form completion, for a first name and email address please ?

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,172
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by Dez View Post
    I just thought that the php code for the form completion, had to go at the very top of the page ? ?
    There can be other PHP code before it. From memory, I don't think you're allowed any whitespace between tags, so no whitespace before the first <?php, and no gaps between the PHP entries. E.g.

    <?php
    one set of instructions
    ?>
    <?php
    another set of instructions
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">


    What would be an example of a complete php code, for a form completion, for a first name and email address please ?
    There are plenty of form-to-email scripts available online, such as FormToEmail.com. I have a few of my own that include form validation to filter out spam. Perhaps PM me though, as there are lots of behaviors available.

    (PS I'm a PHP noob, so you could also head over to the PHP forum. )

  10. #10
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Re the original question
    The only way to do this would be to use AJAX to detect that the user has returned to the previous page, and then re-populate the form fields based on what was submitted. This can not be guaranteed to work every time, though, as not everyone will have Javascript enabled. I'm not an AJAX programmer, so I couldn't tell you how to do it, but it should be possible (albeit complicated).

    A (in my oppinion better) solution, which will work in every browser, would be to place a button which, when clicked, will go to the form and populate the form fields using PHP.

    Re the PHP
    There is no reason to close the PHP block and start another, if you have no litteral HTML in between the two sets of instructions. There is no output difference between these two examples:

    Code php:
    <?php
    $array = array(1, 2, 3, 4, 5);
     
    foreach($array as &$value) {
     $value++;
    }
    ?>
    <?php
    foreach($array as $key => $value) {
     printf("The value of item %s is %s<br>", $key, $value);
    }
    ?>

    Code php:
    <?php
    $array = array(1, 2, 3, 4, 5);
     
    foreach($array as &$value) {
     $value++;
    }
     
    foreach($array as $key => $value) {
     printf("The value of item %s is %s<br>", $key, $value);
    }
    ?>

    You can even do this:

    Code php:
    <?php
    $array = array(1, 2, 3, 4, 5);
     
    foreach($array as $key => $value) {
    ?>
    The value of item <?php $key; ?> is <?php $value++; ?><br>
    <?php
    }
    ?>

    The last example can't be recommended, though, as it will be a true pain to debug later.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  11. #11
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    What type of form fields are we talking about? Normal text boxes are easy.
    PHP Code:
    <input type="text" name="firstName" value="
    <?php if(!isset($_POST['firstName']){
    echo 
    "Enter your first name";
    }else{ echo 
    $_POST['firstName'];}?>"
    > // closes the input tag
    Ryan B | My Blog | Twitter

  12. #12
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dez View Post
    is there any way, that when a form is incorrectly completed and you click send, the next page says that some of the forms questions haven't been answered properly, and then you click the back button to go back to the form, is there any way that the parts that you did complete, would still be in the form ? This is in formmail.cgi Is there any way of doing this, without anything to do with PHP please ?
    Use javascript to detect if the form has been completed correctly BEFORE letting the user leave the page.

    I can't remember how I ended up on this page
    http://www.designplace.org/scripts.php?page=1&c_id=22
    but it has a simple script to validate form fields.

    You could also try googling on form validation javascript. It brings up lots of examples.

  13. #13
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Many thanks all - much appreciated. Problems all solved now.


Bookmarks

Posting Permissions

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