[ask] online order form

Hi all

I’m making a form for my own online store, I wanted to be before the buyer entered into the data store database is mine, I want to have a preview display that allows the buyer to change his own data if found errors in the form.

I use php session data for this.

I had to fill the form field but after I press the ‘submit’ all existing data in the form erased or disappeared.

here is my code form .


<?
    // start session

    session_start();

    // define variable

    $name   = isset ($_SESSION['name']) ? $_SESSION['name'] : '' ;
    $email  = isset ($_SESSION['email']) ? $_SESSION['email'] : '';
    $phone  = isset ($_SESSION['phone']) ? $_SESSION['phone'] : '';
    $pcode  = isset ($_SESSION['pcode']) ? $_SESSION['pcode'] : '';
    $pcolor = isset ($_SESSION['pcolor']) ? $_SESSION['pcolor'] : '';
    $ptype  = isset ($_SESSION['ptype']) ? $_SESSION['ptype'] : '';
    $psize  = isset ($_SESSION['psize']) ? $_SESSION['psize'] : '';
    $pquan  = isset ($_SESSION['pquan']) ? $_SESSION['pquan'] : '';
    $address = isset ($_SESSION['address']) ? $_SESSION['address'] : '';
    $city   = isset ($_SESSION['city']) ? $_SESSION['city'] : '';
    $zip    = isset ($_SESSION['zip']) ? $_SESSION['zip'] : '';
    $state  = isset ($_SESSION['state']) ? $_SESSION['state'] : '';
    $notes  = isset ($_SESSION['notes']) ? $_SESSION['notes'] : '';

    // define preview, edit and submit

    $preview    = isset ($_POST['preview']) ? TRUE : FALSE ;
    $edit       = isset ($_POST['edit']) ? TRUE : FALSE ;
    $submit     = isset ($_POST['submit']) ? TRUE : FALSE ;

    // define session data to variable

    if($preview)    {
        $_SESSION['name']   = $name;
        $_SESSION['email']  = $email;
        $_SESSION['phone']  = $phone;
        $_SESSION['pcode']  = $pcode;
        $_SESSION['pcolor'] = $pcolor;
        $_SESSION['ptype']  = $ptype;
        $_SESSION['psize']  = $psize;
        $_SESSION['pquan']  = $pquan;
        $_SESSION['address'] = $address;
        $_SESSION['city']   = $city;
        $_SESSION['zip']    = $zip;
        $_SESSION['state']  = $state;
        $_SESSION['notes']  = $notes;
    }

    if($submit) {
        $server     = 'localhost';
        $username   = 'root';
        $password   = '';
        $database   = 'order';

        // connect to database
        
        $con        = mysql_connect($server, $username, $password);
        if (!$con)  {
            die ('Error:' .' '.mysql_error());
        }

        // select database
        
        $sql        = mysql_select_db($database, $con);
        if (!$sql)  {
            die ('Error:' . '' . mysql_error());
        }

        // insert value to table field
        
        mysql_query("INSERT INTO db_buyer (id, name, email, phone, pcode, pcolor, ptype, psize, pquan, address, city, zip, state, notes)
            VALUES('', $name, $email, $phone, $pcode, $pcolor, $ptype, $psize, $pquan, $address, $city, $zip, $state, $notes)");

        // destroy session

        $_SESSION = array();
        session_destroy();
        
    }
?>

<html>
    <head>
        <title>Order form</title>
    </head>
    <body>
        <?php if ($preview): ?>
        Name : <?php echo $name; ?> <br>
        Email : <?php echo $email; ?> <br>
        Phone : <?php echo $phone; ?> <br>
        Product Code : <?php echo $pcode; ?> <br>
        Product Color : <?php echo $pcolor; ?> <br>
        Product Type : <?php echo $ptype; ?> <br>
        Product Size : <?php echo $psize; ?> <br>
        Product Quantity : <?php echo $pquan; ?> <br>
        Address : <?php echo $address; ?> <br>
        City : <?php echo $city; ?> <br>
        ZIP CODE : <?php echo $zip; ?> <br>
        State : <?php echo $state; ?> <br>
        Notes : <?php echo $notes; ?> <br>
        <br>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <input type="submit" name="submit" value="submit" /> <input type="submit" name="edit" value="edit" />
        </form>

        <?php elseif ($submit): ?>
        Submitted

        <?php else: ?>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="col">
                Name: <input type="text" id="name" name="name" value="<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : ''; ?>" >
            </div>
            <div class="col">
                Email: <input type="text" id="email" name="email" value="<?php echo isset($_SESSION['email']) ? $_SESSION['email'] : ''; ?>">
            </div>
            <div class="col">
                Phone: <input type="text" id="phone" name="phone" value="<?php echo isset($_SESSION['phone']) ? $_SESSION['phone'] : ''; ?>">
            </div>
            <div class="col">
                Product Code: <input type="text" id="pcode" name="pcode" value="<?php echo isset($_SESSION['pcode']) ? $_SESSION['pcode'] : ''; ?>">
            </div>
            <div class="col">
                Product Color: <input type="text" id="pcolor" name="pcolor" value="<?php echo isset($_SESSION['pcolor']) ? $_SESSION['pcolor'] : '';?>">
            </div>
            <div class="col">
                Product Type: <input type="text" id="ptype" name="ptype" value="<?php echo isset($_SESSION['ptype']) ? $_SESSION['ptype'] : ''; ?>">
            </div>
            <div class="col">
                Product Size: <input type="text" id="psize" name="psize" value="<?php echo isset($_SESSION['psize']) ? $_SESSION['psize'] : ''; ?>">
            </div>
            <div class="col">
                Order Quantity: <input type="text" id="pquan" name="pquan" value="<?php echo isset($_SESSION['pquan']) ? $_SESSION['pquan'] : '';?>">
            </div>
            <div class="col">
                Address: <input type="text" id="address" name="address" value="<?php echo isset($_SESSION['address']) ? $_SESSION['address'] : ''; ?>">
             </div>
            <div class="col">
                City: <input type="text" id="city" name="city" value="<?php echo isset($_SESSION['city']) ? $_SESSION['city'] : '';?>">
            </div>
            <div class="col">
                ZIP: <input type="text" id="zip" name="zip" value="<?php echo isset($_SESSION['zip']) ? $_SESSION['zip'] : '';?>">
            </div>
            <div class="col">
                State: <input type="text" id="state" name="state" value="<?php echo isset ($_SESSION['state']) ? $_SESSION['state'] : '';?>">
            </div>
            <div class="note">
                Notes: <br>
                <textarea cols="25" rows="10" id="notes" name="notes"></textarea>
            </div>
            <br  />

            <input type="submit" name="preview" value="submit" />
        </form>
        <?php endif; ?>
    </body>
</html>

I ask help from you all. and I also have a question, how do I get when a page ‘preview’ then I nor the buyer get the invoice number ( automatically placed above ‘name’ field ) of the form with the numbers and letters combination randomly.

thanks for your help. I have been very tired with this: D :x :blush: :goof:

owhhh my God :smiley: :smiley: :smiley: finally it’s solved :smiley: .

so it was the location of faults ?

Now all has gone well. I am very grateful for the knowledge you provide.

I once again thank you for helping Mr. Pmw75 :slight_smile:

Is that a tab or spacing that I see before the <?php code?

thanks for your answer Mr.pmw57 :), nice to meet in you in here

I apologize for the late reply to this message, I’ve started to understand so I do not need to check a session variable but I must first put the code in the ‘preview’, right?

OK, I want to ask you one other thing that is:


function getSessionOrPost($key)
{
    return !empty($_SESSION[$key]) ? $_SESSION[$key] : filter_input(INPUT_POST, $key);
}
$name = getSessionOrPost('name');

Where do I put the code?

I’ve tried but failed, I apologize because I am really a beginner.

thanks :slight_smile:

OK, thanks i will try it :slight_smile:

thanks for your attention to me :stuck_out_tongue: .

OK, now i can to make invoice number . thank you :smiley:

but the main problem that I face is not finished, I’ve tried to save the file in UTF-8 without DOMS but no change.

What is wrong in the code that I created? : (

here is my complete code :slight_smile:


        <?php
        
        session_start();

        // define preview, edit, & submit
            $preview    = isset ($_POST['preview']) ? TRUE : FALSE;
            $edit       = isset ($_POST['edit']) ? TRUE : FALSE;
            $submit     = isset ($_POST['submit']) ? TRUE : FALSE;
        
        // make preview
        if ($preview)   {

            $name   = isset ($_SESSION['name']) ? $_SESSION['name'] : '' ;
            $email  = isset ($_SESSION['email']) ? $_SESSION['email'] : '';
            $phone  = isset ($_SESSION['phone']) ? $_SESSION['phone'] : '';
            $pcode  = isset ($_SESSION['pcode']) ? $_SESSION['pcode'] : '';
            $pcolor = isset ($_SESSION['pcolor']) ? $_SESSION['pcolor'] : '';
            $ptype  = isset ($_SESSION['ptype']) ? $_SESSION['ptype'] : '';
            $psize  = isset ($_SESSION['psize']) ? $_SESSION['psize'] : '';
            $pquan  = isset ($_SESSION['pquan']) ? $_SESSION['pquan'] : '';
            $address = isset ($_SESSION['address']) ? $_SESSION['address'] : '';
            $city   = isset ($_SESSION['city']) ? $_SESSION['city'] : '';
            $zip    = isset ($_SESSION['zip']) ? $_SESSION['zip'] : '';
            $state  = isset ($_SESSION['state']) ? $_SESSION['state'] : '';
            $notes  = isset ($_SESSION['notes']) ? $_SESSION['notes'] : '';

        }

        // function to get SESSION or POST
        function getSessionOrPost($key) {
                 return !empty($_SESSION[$key]) ? $_SESSION[$key] : filter_input(INPUT_POST, $key);
        }

        $name   = getSessionOrPost('name');
        $email  = getSessionOrPost('email');
        $phone  = getSessionOrPost('phone');
        $pcode  = getSessionOrPost('pcode');
        $pcolor = getSessionOrPost('pcolor');
        $ptype  = getSessionOrPost('ptype');
        $psize  = getSessionOrPost('psize');
        $pquan  = getSessionOrPost('pquan');
        $address = getSessionOrPost('address');
        $city   = getSessionOrPost('city');
        $zip    = getSessionOrPost('zip');
        $state  = getSessionOrPost('state');
        $notes  = getSessionOrPost('notes');
        
        if ($submit)    {

            // config & connect database
            $server     = 'localhost';
            $username   = 'root';
            $password   = '';
            $database   = 'order';

            // connect to database
            $conn       = mysql_connect($server, $username, $password);
                if (!$conn) {
                    die ('Error:'. ' ' .mysql_error());
                }

            // select to database
            $sql        = mysql_select_db($database, $conn);
                if (!sql)   {
                    die ('Error:'. ' ' .mysql_error());
                }

            // insert value to table
            mysql_query ("INSERT INTO db_order (ID, name, email, phone, pcode, pcolor, ptype, psize, pquan, address, city, zip, state)
                    VALUES ('', $name, $email, $phone, $pcode, $pcolor, $ptype, $psize, $pquan, $address, $city, $zip, $state)");
       
        
            // session destroy
            $_SESSION   = array ();
            session_destroy();
        }

        ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>order form</title>
    </head>
    <body>
            <?php if ($preview): ?>
                Name : <?php echo $name; ?> <br>
                Email : <?php echo $email; ?> <br>
                Phone : <?php echo $phone; ?> <br>
                Product Code : <?php echo $pcode; ?> <br>
                Product Color : <?php echo $pcolor; ?> <br>
                Product Type : <?php echo $ptype; ?> <br>
                Product Size : <?php echo $psize; ?> <br>
                Product Quantity : <?php echo $pquan; ?> <br>
                Address : <?php echo $address; ?> <br>
                City : <?php echo $city; ?> <br>
                ZIP CODE : <?php echo $zip; ?> <br>
                State : <?php echo $state; ?> <br>
                Notes : <?php echo $notes; ?> <br>
                <br>
            <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                <input type="submit" name="submit" value="submit" />
                <input type="submit" name="edit" value="edit" />
            </form>

            <?php elseif ($submit): ?>
        Submitted

        <?php else: ?>
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
            <div class="col">
                Name: <input type="text" id="name" name="name" value="<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : ''; ?>" >
            </div>
            <div class="col">
                Email: <input type="text" id="email" name="email" value="<?php echo isset($_SESSION['email']) ? $_SESSION['email'] : ''; ?>">
            </div>
            <div class="col">
                Phone: <input type="text" id="phone" name="phone" value="<?php echo isset($_SESSION['phone']) ? $_SESSION['phone'] : ''; ?>">
            </div>
            <div class="col">
                Product Code: <input type="text" id="pcode" name="pcode" value="<?php echo isset($_SESSION['pcode']) ? $_SESSION['pcode'] : ''; ?>">
            </div>
            <div class="col">
                Product Color: <input type="text" id="pcolor" name="pcolor" value="<?php echo isset($_SESSION['pcolor']) ? $_SESSION['pcolor'] : '';?>">
            </div>
            <div class="col">
                Product Type: <input type="text" id="ptype" name="ptype" value="<?php echo isset($_SESSION['ptype']) ? $_SESSION['ptype'] : ''; ?>">
            </div>
            <div class="col">
                Product Size: <input type="text" id="psize" name="psize" value="<?php echo isset($_SESSION['psize']) ? $_SESSION['psize'] : ''; ?>">
            </div>
            <div class="col">
                Order Quantity: <input type="text" id="pquan" name="pquan" value="<?php echo isset($_SESSION['pquan']) ? $_SESSION['pquan'] : '';?>">
            </div>
            <div class="col">
                Address: <input type="text" id="address" name="address" value="<?php echo isset($_SESSION['address']) ? $_SESSION['address'] : ''; ?>">
             </div>
            <div class="col">
                City: <input type="text" id="city" name="city" value="<?php echo isset($_SESSION['city']) ? $_SESSION['city'] : '';?>">
            </div>
            <div class="col">
                ZIP: <input type="text" id="zip" name="zip" value="<?php echo isset($_SESSION['zip']) ? $_SESSION['zip'] : '';?>">
            </div>
            <div class="col">
                State: <input type="text" id="state" name="state" value="<?php echo isset ($_SESSION['state']) ? $_SESSION['state'] : '';?>">
            </div>
            <div class="note">
                Notes: <br>
                <textarea cols="25" rows="10" id="notes" name="notes"></textarea>
            </div>
            <br  />

            <input type="submit" name="preview" value="submit" />
        </form>
        <?php endif; ?> 
    </body>
</html>

Sorry if i seem too stupid :slight_smile:

first , i want to say thanks to mr.pmw57 .

everything is done & now i can continue to make over my order form :smiley: , but before that i want to ask about this problem :slight_smile:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at F:\\xampp\\htdocs\\order\\index.php:1) in F:\\xampp\\htdocs\\order\\index.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at F:\\xampp\\htdocs\\order\\index.php:1) in F:\\xampp\\htdocs\\order\\index.php on line 4

why I find these warning messages ? and how to solve this problem . in my first code that i was post in this page i didn’t look something wrong :slight_smile:

and the last question is how to make invoice number that combination between number and text this only consists of five random characters .

thanks in advance , :blush:


notes : if you’re want to see my code form please check in here :

http://pastie.org/1004738

oh no, I’ve found new problem in this form :frowning:

I’ve done everything but when I have to fill all existing data and then I press the ‘submit’ I can see a preview of all the data but when I press the ‘edit’ I lost all my data is already content.

This is very tiring and boring if you must fill out the data from scratch.

How Do I get a way to make ‘value’ of each field is and I only live just changing some of his data?

thanks

You can put the function near the top of the code.

Then, the code that assigns $name and $email and the other variables from $_SESSION can all use that function. The last line of the above code which assigns $name should be copied to the other ones such as $email and the other variables.

Those warnings occur when output occurs before the session has finished being written to.

The cause for it can be text output, or the format of the text file. If the text file is saved as a UTF-8 file with a BOM (byte-order-mark), then the BOM is the cause of the problem. You would need to save the file as UTF-8 with no BOM instead.

You could use the uniqid command, but that’s 13 characters long. You could just use [url=“http://www.php.net/manual/en/function.substr.php”]substr to take the last five of it though.

Place hidden form fields in the preview, with which to post to the next page?

First, you shoudn’t be checking if the session variables are set. You set them in the preview, so even if they are set they could be empty.

You need to instead check if the session variables are not empty.

Second, if the form is submitted for the first time, when the session variable is empty, how does the information submitted viar $_POST get assigned to variables such as $name?

Here’s a possible solution, that you can apply to the other form variables too.


function getSessionOrPost($key)
{
    return !empty($_SESSION[$key]) ? $_SESSION[$key] : filter_input(INPUT_POST, $key);
}
$name = getSessionOrPost('name');