Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member
    Join Date
    Dec 2003
    Posts
    1,274
    Member #
    4362
    I have a form to mail script, and it checks the input received via POST, and if a error occurs, it uses "header" to redirect back to the form page with a GET string to show to the user where he forgot to put something in or whatever, but I also want to restore all the other data in each input box.
    The thing is, I want to send the POST array using header:
    PHP Code:
    header ("location: http://xtdesign.net/contact?error=FullName"); 
    Any clue?
    Thanks

  2.  

  3. #2
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Seeing as the Location header is merely an instruction for the client to go somewhere else for the page, the only way of sending the data back to them is via the query string.

    Now what you could do is use a session to temporarily store the information entered and send along with the location header the session ID. You can then have your contact page look for the session variable and re-retireve the information pertaining to that user, if it exists.

    Your contact form will look something like this.

    contact.php
    PHP Code:
    <?php 
    session_start
    ();

    if (isset (
    $_SESSION['posted'])) { /* read session variables if they exist */
        
    $name $_SESSION['name'];
        
    $email $_SESSION['email'];
        
    $message $_SESSION['message'];
    } else {
        
    $name $email $message '';
    }
    ?>
    <html>
        <head>
            <title>Contact Me</title>
        <body>
        <form action="process.php<?php echo (SID?('?' SID):''?>" method="post">
            <p>
               Your Name: <input type="text" name="name" value="<?php echo (htmlspecialchars($name)) ?>" /><br />
               Your E-Mail: <input type="text" name="email" value="<?php echo (htmlspecialchars($email)) ?>" /><br /> 
               Your Message<br />
               <textarea name="message"><?php echo (htmlspecialchars($message)) ?></textarea><br />
               <input type="submit" />
            </p>
        </form>
        </body>
    </html>
    And your pocessing page will look something like this:

    process.php
    PHP Code:
    <?php
        session_start
    ();

        
    $error false;
        
        
    $_SESSION['posted'] = true/* set the session flag */

        /* check validity of session variables here */
        
        
    if (isset ($_POST['name']) && $_POST['name'] != '') {
            
    $_SESSION['name'] = $_POST['name'];
        } else {
            
    $_SESSION['name'] = '';
            
    $error true;
        }

        if (isset (
    $_POST['email']) && $_POST['email'] != '') {
            
    $_SESSION['email'] = $_POST['email'];
        } else {
            
    $_SESSION['email'] = '';
            
    $error true;
        }

        if (isset (
    $_POST['message']) && $_POST['message'] != '') {
            
    $_SESSION['message'] = $_POST['message'];
        } else {
            
    $_SESSION['message'] = '';
            
    $error true;
        } 

        if (
    $error) { /* redirect the form */
            
    header ('Location: contact.php' . (SID?'?' SID:''));
        }
    ?>
    It really is as easy as that - and is definatley the easiest way.

  4. #3
    Senior Member
    Join Date
    Dec 2003
    Posts
    1,274
    Member #
    4362
    That's a really good explanation, thanks
    But I don't know why now the SID is empty always, look at the code in contact.php:
    PHP Code:
    <?php 
                session_start
    (); 
                
                if (isset (
    $_SESSION['posted'])) { /* read session variables if they exist */ 
                    
    $FullName $_SESSION['FullName']; 
                    
    $Email $_SESSION['Email']; 
                    
    $Subject $_SESSION['Subject'];
                    
    $Question $_SESSION['Question'];  
                } else { 
                    
    $FullName $Email $Subject $Question ''
                } 
                
    ?>
      <form name="form1" id="form1" method="post" action="/scripts/f2m.php?<?php echo SID?>">
    See, I echo the SID on the action of the form, but if you look at the source of the page nothing is echoed. Here is the page:
    http://xtdesign.net/contact/

    Thanks

  5. #4
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Yes - this is to do with the way session management works in PHP. By default it uses a cookie stored on the client machine. If the cookie is saved successfully by the clients browser then the SID constant is an empty string. If the client doesn't save the cookie - the SID variable conains something like:

    PHPSESSID=eb6d6566d0716eb72d80d58c547fdbd4

    This means you can append the SID constant to the end of all your links just incase the client doesn't support cookies. If you don't want to use the cookies then you can use this before calling session start:
    PHP Code:
    ini_set('session.use_cookies''0'); 
    Your script should be working as it is. Are you using windows? If so, then you need to set the session.savepath directive in the php.ini to a valid directory, otherwise your sessions won't work.

    It's worth having a quick read of the documentation on the PHP site regarding sessions, if you want to know how it all works:

    http://uk2.php.net/manual/en/ref.session.php

  6. #5
    Senior Member
    Join Date
    Dec 2003
    Posts
    1,274
    Member #
    4362
    Oh, I see, the PHPSESSID is only appended to the url in the case that cookies are disabled, right?

  7. #6
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Yes. In my opinion, this is the PHP session modules' best feature. It means you can pretty much guaruntee the functionality you need without having to worry about whether your visitors have cookies enabled (PHP does it all in the background, transparently).

    I'll be posting a link later to a class I have just written which allows you to save sessions in a MySql database - you may find it useful.

  8. #7
    Senior Member
    Join Date
    Dec 2003
    Posts
    1,274
    Member #
    4362
    Thanks visualAd! The contact form is up and working like a charm after a few modifications.
    Thanks


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com