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 :goof: