SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating Form input via PHP and then redirecting with POST data?

    I would like to use PHP to validate form data, and then redirect to another page while passing the form's data as POST variables.

    I thought that perhaps I could set the form action as "" so that it comes back to the current page, which would then check the data and redirect if everything was OK (although I'm open to alternative approaches if this approach is not the best idea). For example, the PHP code below would redirect to the current page, use some simplified validation checking for the social security number and email data, and if everything was OK it would redirect somehow:
    PHP Code:
    <?php
    $error 
    FALSE;
    if(
    count($_POST)>0){
        
    $error_check TRUE;
    }
    else{
        
    $error_check FALSE;
    }
    ?>
    <form action="" method="POST">
    <input maxlength='3' name='ssn1' size='3' value=''>)-<input maxlength='2' name='ssn2' size='2' value=''>-<input maxlength='4' name='ssn3' size='3' value=''>
    <?php
    if($error_check && (!is_numeric($_POST['ssn1']) || !is_numeric($_POST['ssn2']) || !is_numeric($_POST['ssn3']))){
        echo 
    "<br />Error - invalid SSN<br />";
        
    $error TRUE;
    }
    ?>
    <input name='email' size='30' maxlength='80' value=''>
    <?php
    if($error_check && strpos($_POST['email'], '@')===FALSE){
        echo 
    "<br />Error - invalid email<br />";
        
    $error TRUE;
    }
    ?>
    <input type="submit" name="Submit">
    </form>
    <?php
    if($error_check && !$error){
        echo 
    "Redirecting...";
        
    //redirect somehow?
    }
    ?>
    However I don't know how to redirect while still passing the data as POST data. Is there some way to do this?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You can use $_SESSION to store it, or if that's not possible - you can store it in a database, then generate a token that gets passed to the redirected page. On the redirected page you would query and pull out the $_POST data back. This method does introduce some minor security issues, so the best thing to do would be to expire the data in 30s or so.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, but how would one redirect to another target page (e.g. http://example.com/target.php) while still passing the POST data?

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    form1.php

    PHP Code:
    session_start();

    if (
    $_POST['submitted']) {
        
    # process the $_POST data 
        
    if ($no_error) {
            
    $_SESSION['form_data'] = $_POST;
            
    header ("Location: form2.php"); exit;
        }

    form2.php

    PHP Code:
    session_start();

    if (
    $_SESSION['form_data']) {
        
    # do your thing here


  5. #5
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but would that pass data in POST form...? In any case that would not work for my as the page I need to redirect to is on a different domain. So a session can't be passed.

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kadence View Post
    Thanks, but would that pass data in POST form...? In any case that would not work for my as the page I need to redirect to is on a different domain. So a session can't be passed.
    In this situation, there's two ways.

    1) If the user needs to be redirect as well, you have to create a form as HTML - then use javascript to submit it. There's no other way.

    2) If the user doesn't need to be redirected, you can use cURL to submit the data via POST.

  7. #7
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help


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
  •