I have a php order form named (order.php) and when the user clicks the (submit button \"Next Step\") it takes him to another page called (confirm-order.php)
The (con
EDIT: a simple example
do , validation and inserting in one file, say form.php:
// check if FORM has been posted
$posted = isset($_POST['submit']);
if ($posted) { // form has been posted...
// validate input
if (!isset($_POST['item']) || strlen(trim($_POST['item'])) == 0)
$error['item'] = "please insert an item-name!";
if (!isset($_POST['price']) || !is_numeric($_POST['price']))
$error['price'] = "please enter a valid price!";
// ready for input?
if (!isset($error)) { // no $error --> go insert!
// I'll do the db-operation with PDO and a prepared statement.
// this is cool, easy and safe. LEARN IT!
$sql = "INSERT INTO table (item,price) VALUES (:item,:price)";
$insert = $db->prepare($sql);
$insert->execute(array(
':item' => $_POST['item'],
':price' => $_POST['price']
));
} // $error
} // submit
?>
Now, in the of the same page...
// check whether to display confirmation or form...
if ($posted && !isset($error)) {
// form was sent AND no error --> confirm
?>
Confirmed!
Your data has been sent, thank you very much!
go to somepage
} else {
// form not sent or errors --> display form
?>
Please enter data
// display error-message, if there's one:
if (isset($error)) {
$output = "";
foreach ($error as $field => $msg)
$output .= (strlen($output) > 0?', ':'') . "[$field]: $msg";
echo "There were errors: $output
";
} // $error
?>
} // submit & $error
?>
See the use of a ternary-operator for setting the value-attribute of the -elements:
(?:)