Jump to content

Session variables to remember form data


synking

Recommended Posts

What i want to do is have a form that remembers the user input if an error happens.

 

I tried to build it my self but kept having isuess.  I got help from lestat on the irc channel but did not work.

Then i found this tutorial. http://net.tutsplus.com/tutorials/html-css-techniques/build-a-neat-html5-powered-contact-form/

 

so now on to my issue

I have index.php that is the main page for the site.  And i want the contactus form to load under index.php so when a user clicks contact us it does not take them to a new page.  So i followed the tutorial and have the cantactus.php built.

 

<div id="contact-form" class="clearfix">
                    <h1>Get In Touch!</h1>
                        <h2>Fill out our super swanky HTML5 contact form below to get in touch with us Please provide as much information as possible. <img class="wp-smiley" alt="" src="http://net.tutsplus.com/wp-includes/images/smilies/icon_smile.gif"> </h2>

                        <?php
                        //init variables
                        $cf = array();
                        $sr = false;

                        if(isset($_SESSION['cf_returndata'])) {
                            $cf = $_SESSION['cf_returndata'];
                            $sr = true;
                        }
                        ?>

                        <ul id="errors" class="<?php echo ($sr &&!$cf['form_ok']) ? 'visible' : ''; ?>">
                            <li id="info">There were some problems with your form submission:</li>
                            <?php
                            if(isset($cf['errors']) && count($cf['errors']) >0) :
                                 foreach($cf['errors'] as $error):
                            ?>
                            <li><?php echo $error ?></li>
                            <?php
                                endforeach;
                            endif;
                            ?>
                        </ul>
                        <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for the message! we will get back asap!</p>
                        <form method="post" action="process.php">
                            <label for="name">Name: <span class="required">*</span></label>
                                <input type="text" id="name" name="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="John Doe" required="required" autofocus="autofocus" />

                                <label for="email">Email Address: <span class"required">*</span></label>
                                <input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="johndoe@example.com" reqired="required" />

                                <label for="telephone">Telephone:</label>
                                <input type="tel" id="telephone" name="telephone" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['telephone'] : '' ?>" />

                                <label for="inquiry">inquiry:</label>
                                <select id="inquiry" name="inquiry">
                                    <option value="general" <?php echo ($sr && !$cf['form_ok'] &&  $cf['posted_form_data']['inquiry'] == 'general') ? "selected='selected'" : '' ?>>General</option>
                                        <option value="sales" <?php echo ($sr && !$cf['form_ok'] &&  $cf['posted_form_data']['inquiry'] == 'sales') ? "selected='selected'" : '' ?>>Sales</option>
                                        <optoin value="support" <?php echo ($sr && !$cf['form_ok'] &&  $cf['posted_form_data']['inquiry'] == 'support') ? "selected='selected'" : '' ?>>Support</option>
                                </select>

                                <label for="message">Message: <span class="required">*</span></label>
                                <textarea id="message" name="message" placeholder="Your message must be greater the 20 charcters" required="required" data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>

                                <span id="loading"></span>
                                <input type="submit" value="Send" id="submit-button" />
                                <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
                        </form>
</div>

 

now when people fill out the form it sends the info to process.php and that part works it will send the email to the proper account with the info the user put in as long as it was filled out properly.  The part that is not working if they do no fill out the form correctly it should save the info and tell them what was wrong.

 

But it is not doing that.  it just shows a blank from once it is done.

 

Is there anyway that i can keep the information without the need to have the contact page on a seperate page.

 

Not sure if anymore info is needed but thanks in advance.

Link to comment
Share on other sites

Ok so i have been trying to work on this myself and found that the error array is not updating properly.

 

I put

print_r ($_SESSION);

after the session start and found the error array empty.

 

This may need to get moved to the thrid party scripts as i think this is an issue with the process.php function wich i did not create. but here it is

 

if(isset($_POST)) {
    //form validation vars
    $formok = true;
    $errors = array();

    //submission data
    $ipaddress = $_SEVER['REMOTE_ADDR'];
    $date = date('m/d/y');
    $time = date('H:i:s');

    //form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $telephone = $_POST['telephone'];
    $inquiry = $_POST['inquiry'];
    $message = $_POST['message'];

    //form validation
    //name is not empty
    if(empty($name)) {
         $formok = false;
         $errors[] = "You have not entered a name";
    }

    //validate email
    if(empty($email)) {
         $formok = false;
         $errors[] = "you have not entered an email";
    //validate email is valid
    } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $formok = false;
         $errors[] = "You have Not entered a valid email address";
    }

    //validate message
    if(empty($message)) {
        $formok = false;
        $errors[] = "Do you just want us to know who you are.";
    } elseif(strlen($message) < 20) {
        $formok = false;
        $error[] = "That small of a message will not get a reply";
    }

    //send email if all is ok
    if($formok) {
        $headers = "From: admin@anarchspeeks.com" . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso=8859-1' . "\r\n";

        $emailbody = "<p>You have recieved a new message from the inquiers on anarchyspeeks.com</p>
                      <p><strong>Name:</strong> {$name} </p>
                      <p><strong>Email Address:</strong> {$email}</p>
                      <p><strong>Telephone: </strong> {$telepone}</p>
                      <p><strong>Inquiry: </strong> {$inquiry}</p>
                      <p><strong>Message: </strong> {$message}</p>
                      <p>This Message was sent from the IP address: {$ipaddress} on {$date} at {$time} </p>";
         mail("king.a.joe@gmail.com","New Question",$emailbody,$headers);
     }    

//return back to form
     $returndata = array (
         'posted_form_data' => array(
             'name' => $name,
             'email' => $email,
             'telephone' => $telephone,
             'inquiry' => $inquiry,
             'message' => $message
          ),
          'form_ok' => $formok,
          'error' => $errors
     );

     if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){

         //set session
         session_start();
         $_SESSION['cr_returndata'] = $returndata;

         //redirect to form
         header('location: '. $_SERVER['HTTP_REFERER']);

      }

}

 

So once the form gets submitted it goes to process.php but the error array is not updating.

 

anyone got any ideas.

Link to comment
Share on other sites

Ok so i was working on this and found more information to why it is not working.

 

First i had the error array named two seperate things fixed that so now the error array i getting populated properly.

 

But now the issue is in this chunk of code

if(isset($_SESSION['cf_returndata'])) {
                            $cf = $_SESSION['cf_returndata'];
                            $sr = true;
                        }

 

it seems that $cf never actaully gets populated with the $_SESSION['cf_returndata'] info.  anyone have any thoughts on this.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.