SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Page resubmission, simple problem

    I am a beginner and am having problem with a form, when i refresh page with the form and the data, all my error messages are still there...

    is there a way to stop the form repeating the same action when a page is refreshed, e.g. just going back to being empty...?

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey thanks.. is there any sample code you think could work for the PRG pattern?

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    form.php:
    PHP Code:
    <input id="email" name="email" value="<?php echo isset($_SESSION['email']) ? htmlspecialchars($_SESSION['formdata']['email']) : "default value" />

    <?
    php if (isset($_SESSION['formerror']['email'])): ?>
    <strong>Please enter your email</strong>
    <?php endif ?>

    <?php unset($_SESSION['formerror'], $_SESSION['formdata']) ?>
    process.php:
    PHP Code:
    if (!isset($_POST['email']) or !validateEmail($_POST['email'])) {
        
    $_SESSION['formerror']['email'] = TRUE;
        
    $_SESSION['formdata']['email'] = isset($_POST['email']) ? $_POST['email'] : "";

        
    header("Location: /form.php");
        exit;
    }

    // do stuff

    header("Location: /success.php"); 
    That's about it. This is a very simplified example to get you going. There's a lot of manual work going on in this example which can be automated.

    Bear in mind I've ommited a lot of stuff (session starting, declaring variables, etc.) for clarity.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But what I do normally is, the form is submitted in the same page (generally in case of single entry system) checking whether the request method was posted or not. Validate entries and if there are any errors remain in the same page. If successfully done the processing form then redirect to the same page or another page as required:
    form.php
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        
    $errors = array();
        
    $email mysql_real_escape_string($_POST['email']);
        if(empty(
    $email) || !validateEmail($email)){
            
    $errors['email'] = 'Invalid E-mail';
        }
        if(empty(
    $errors)){
            
    // do some actions
            // and redirect to the requried page or the same page form.php
            
    header("Location: form.php");
            exit();
        }
    }
    // if there are validations errors then the $error variable will have your messsages 
    // which you can print out in your form
    // put your rest of the form and/or other HTML tags here under. 
    Edit:
    The form will look something like this:
    Code html4strict:
    <form name="frm1" id="frm1" method="post" action="">
        E-mail: <input type="text" name="email" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" />
        <?php if(isset($errors['email'])): ?><span style="color: red;"><?php echo $errors['email']?></span> <?php endif; ?>
    </form>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •