Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Posting submitted data

    I'm still learning PHP so I'm not sure the best way to do this. Right now I have a contact form that redirects to a separate page once the form is submitted. What I would like to do is display the information that the user submitted right there on the page, so they can double check to see that they didn't make any mistakes. Is this possible to do with a flat file or would I need to create a database?

  • #2
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    Well it depends on your form. If you have a form that starts something like this:

    Code:
     <form action="check.php" method="get">
       <input type="text" name="nom" /><input type="submit" />
     </form>
    You can call the submitted data [in check.php] by using the following PHP code:

    PHP Code:
     <?php echo $_GET['nom']; ?>
    This method posts all the data in the URL - allowing the user to see it all in the address bar.


    The next method is the POST method. It goes something like this:
    Code:
     <form action="check.php" method="post">
       <input type="text" name="nom" /><input type="submit" />
     </form>
    You can call the data from the [check.php] page by using this PHP code:
    PHP Code:
    <?php echo $_POST['nom']; ?>
    See, get -> $_GET while post -> $_POST.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #3
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Doesn't seem to be working for me. I don't know if you got this from my first post but I want to display the data on the page that that the user gets redirected to, not on the page that they submitted the form from.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Zach ...

    So, after the email is sent, you then display a confirmation page ...
    and it's that page where the information appears?

  • #5
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, the form is filled out, the user clicks submit, and they are redirected to a new page called success.php. I want to display the data they submitted on success.php.

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Should be something like this

    success.php
    PHP Code:
    //pseudo code

    //all variables are posted to success.php from within a form on your mail page.
    $to $_POST['to']; // gets its value from a form element named "to".
    $subject $_POST['subject'];  // gets its value from a form element named "subject".//
    $message $_POST['message']; // gets its value from a form element named "message".
    $headers 'From: webmaster@example.com' "\r\n";
    $headers .= 'Reply-To: webmaster@example.com' "\r\n";
    $headers .= 'X-Mailer: PHP/' phpversion();


    if ( 
    mail($to,$subject,$message,$headers) ) {  //if mail was successfully sent
           
    echo "<h1>Success</h1>\n";
           echo 
    "<br />\n"
           
    echo "Your email to " $to " concerning " $subject " was successfully sent.";
    }
    else { 
    //if mail could not be sent
           
    echo "Your email to " $to .  " concerning " $subject " failed."

    Last edited by StupidRalph; 07-17-2007 at 08:44 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Can't seem to make it work. Here is what I have (simplified):

    contact.php:

    PHP Code:
    <?php
    if (@$_POST['submitted']) {
      
    $firstName = @$_POST['firstname'];
      
    $lastName = @$_POST['lastname'];
    //continutes with more variables, form validation

      
    $destinationEmail "my email address";
      
    $emailSubject "Contact Information";
      
    $emailBody "$firstName $lastName"//shortened

      
    if ($error=="0") {
        
    mail ($destinationEmail$emailSubject$emailBody);
        
    header ('Location: success.php');
        exit ();
      }
    }
    ?>
    <html>
    <form action="contact.php" method="post">
    <input name="firstname" type="text" />
    <input name="lastname" type="text" />
    //rest of inputs
    <input name="submitted" type="submit" value="Submit" />
    </form>
    </html>
    PHP Code:
    success.php:

    <?php
    $firstName 
    = @$_POST['firstname'];
    $lastName = @$_POST['lastname'];
    //continues
    ?>
    <html>
    <p>The following information has been submitted:</p>
    <p>First Name: <?php echo $firstName ?></p>
    <p>Last Name: <?php echo $lastName ?></p>
    </html>
    Can anyone see what I'm doing wrong?

  • #8
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You are posting the input back to contact.php, therefore, the $_POST variables are not set when you get to success.php.

  • #9
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Is there a way to post the variables for both contact.php and success.php?

  • #10
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    There's really no need. You can move the php code from contact.php to success.php and change the form action to success.php

  • #11
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm not sure what you mean. Move what part of the php code?

  • #12
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Wait never mind. That worked - kind of. Before, if the page only redirected if certain fields were filled and no errors were found. Now it is redirected anytime you submit, regardless of which fields are filled in.

  • #13
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    contact.php:

    Code:
    <html>
    <form action="success.php" method="post">
    <input name="firstname" type="text" />
    <input name="lastname" type="text" />
    //rest of inputs
    <input name="submitted" type="submit" value="Submit" />
    </form>
    </html>
    success.php
    PHP Code:
    <?php
    if (@$_POST['submitted']) {
      
    $firstName = @$_POST['firstname'];
      
    $lastName = @$_POST['lastname'];
    //continutes with more variables, form validation

      
    $destinationEmail "my email address";
      
    $emailSubject "Contact Information";
      
    $emailBody "$firstName $lastName"//shortened

      
    if ($error=="0") {
        
    mail ($destinationEmail$emailSubject$emailBody);
        
    header ('Location: success.php');
        exit ();
      }
    }
    ?>
    <html>
    <p>The following information has been submitted:</p>
    <p>First Name: <?php echo $firstName ?></p>
    <p>Last Name: <?php echo $lastName ?></p>
    </html>
    Alternatively, you could do this:

    contact.php:

    PHP Code:
    <?php
    if (@$_POST['submitted']) {
      
    $firstName = @$_POST['firstname'];
      
    $lastName = @$_POST['lastname'];
    //continutes with more variables, form validation

      
    $destinationEmail "my email address";
      
    $emailSubject "Contact Information";
      
    $emailBody "$firstName $lastName"//shortened

      
    if ($error=="0") {
        
    mail ($destinationEmail$emailSubject$emailBody);
        
    header ('Location: success.php?firstname=' $firstname '&lastname=' $lastname);
        exit ();
      }
    }
    ?>
    <html>
    <form action="contact.php" method="post">
    <input name="firstname" type="text" />
    <input name="lastname" type="text" />
    //rest of inputs
    <input name="submitted" type="submit" value="Submit" />
    </form>
    </html>
    success.php:
    PHP Code:
    <?php
    $firstName 
    = @$_GET['firstname'];
    $lastName = @$_GET['lastname'];
    //continues
    ?>
    <html>
    <p>The following information has been submitted:</p>
    <p>First Name: <?php echo $firstName ?></p>
    <p>Last Name: <?php echo $lastName ?></p>
    </html>
    Although this would get messy if you are passing a lot of variables to success.php.

  • #14
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Zach, be sure you're reading these posts carefully. I've stated this information in my post earlier.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #15
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am reading them carefully, I just didn't understand what you meant. I'm still learning PHP so I have to study the code for a while before I understand it.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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