你的问题出在你的 if 语句中
替换这个:
if (empty($_POST['customer_name']) $err[] = "Username field is required";
if (empty($_POST['tel_num']) $err[] = "Comments field is required";
和:
if (empty($_POST['customer_name'])) {
$err[] = "Username field is required";
}
if (empty($_POST['tel_num'])) {
$err[] = "Comments field is required";
}
问题是你在那个站立)
之后错过了一个。)
所以你有这个:
empty($_POST['customer_name']) //notice 1 (
但它必须是这样的:
empty($_POST['customer_name'])) //notice 2 (
您也可以使用短标签 ( <?
) 打开 php,但最<?php
好像在其他脚本中一样使用
编辑
在 cart.php 中,您应该使用以下代码:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$err = array();
//performing all validations and raising corresponding errors
if (empty($_POST['customer_name'])) {
$err[] = "Username field is required";
}
if (empty($_POST['tel_num'])) {
$err[] = "Comments field is required";
}
if (empty($err)) {
//if no errors - saving data and redirect
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} else {
// all field values should be escaped according to HTML standard
foreach ($_POST as $key => $val) {
$form[$key] = htmlspecialchars($val);
}
}
} else {
$form['customer_name'] = 'm;
$form['tel_num'] = '';
}
include 'form.tpl.php';
?>
EDIT2
我再次检查了您问题中的代码。我找不到任何问题。我确实做了一些小改动
我认为当您使用此脚本时,它应该可以工作:
<?php
session_start();
/* Created by Adam Khoury @ www.developphp.com */
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysqli.php";
// Determine which page ID to use in our query below ---------------------------------------------------------------------------------------
if (!empty($_GET['pid'])) {
$pageid = 1;
} else {
$pageid = preg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die(mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body = $row["pagebody"];
}
mysqli_free_result($query);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// Query the module data for display ---------------------------------------------------------------------------------------------------------------
$sqlCommand = "SELECT modulebody FROM modules WHERE showing='1' AND name='footer' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die(mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$footer = $row["modulebody"];
}
mysqli_free_result($query);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// Query the module data for display ---------------------------------------------------------------------------------------------------------------
$sqlCommand = "SELECT modulebody FROM modules WHERE showing='1' AND name='custom1' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die(mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$custom1 = $row["modulebody"];
}
mysqli_free_result($query);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// Build Main Navigation menu and gather page data here -----------------------------------------------------------------------------
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id DESC";
$query = mysqli_query($myConnection, $sqlCommand) or die(mysqli_error());
$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$menuDisplay .= '<a href="index.php?pid=' . $pid . '">' .
$linklabel . '</a><br />';
}
mysqli_free_result($query);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
//mysqli_close($myConnection);
// This file is www.developphp.com curriculum material
// Written by Adam Khoury January 01, 2011
// http://www.youtube.com/view_play_list?p=442E340A42191003
// Script Error Reporting
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 (if user chooses to empty their shopping cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] === 'emptycart') {
unset($_SESSION["cart_array"]);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 (if user chooses to adjust item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >= 100) {
$quantity = 99;
}
if ($quantity < 1) {
$quantity = 1;
}
if (empty($quantity)) {
$quantity = 1;
}
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $item_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !== '') {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"][$key_to_remove]);
sort($_SESSION["cart_array"]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h3 align='center'>Your shopping cart is empty</h3>";
} else {
// Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="http://chenlikpharmacy.freeserver.me/order_list.php" method="post">
<input type="hidden" name="cartOutput" value = "$cartOutput">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sqlCommand = "SELECT * FROM products WHERE id='$item_id' LIMIT 1";
$sql = mysqli_query($myConnection, $sqlCommand);
while ($row = mysqli_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_US");
$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$pp_checkout_btn .= '<input type="hidden" name="item_name[]" value="' . $product_name . '">
<input type="hidden" name="amount[]" value="' . $price . '">
<input type="hidden" name="quantity[]" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-" . $each_item['quantity'] . ",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>RM' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
setlocale(LC_MONETARY, "ms_MY");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : " . $cartTotal . " MYR</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
Name: <input type="text" name="customer_name">
<br/>
Tel: <input type="text" name="tel_num">
<input type="submit" value="Submit">
</form>';
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$err = array();
//performing all validations and raising corresponding errors
if (empty($_POST['customer_name'])) {
$err[] = "Username field is required";
}
if (empty($_POST['tel_num'])) {
$err[] = "Comments field is required";
}
if (empty($err)) {
//if no errors - saving data and redirect
header("Location: " . $_SERVER['PHP_SELF']);
exit;
} else {
// all field values should be escaped according to HTML standard
foreach ($_POST as $key => $val) {
$form[$key] = htmlspecialchars($val);
}
}
} else {
$form['customer_name'] = '';
$form['tel_num'] = '';
}
include 'form.tpl.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title>CHENLIK PHARMACY ONLINE CATALOGUE</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="css/images/favicon.ico" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery.jcarousel.min.js"></script>
<!--[if IE 6]>
<script type="text/javascript" src="js/png-fix.js"></script>
<![endif]-->
<script type="text/javascript" src="js/functions.js"></script>
</head>
<body>
<!-- Header -->
<div id="header" class="shell">
<div id="logo">
<h1><a href="http://chenlikpharmacy.freeserver.me/index.php">Chenlik Pharmacy Sdn. Bhd.</a></h1><span><a href="http://chenlikpharmacy.freeserver.me">Serve with Care & Passion</a></span></div>
<!-- Navigation -->
<div id="navigation">
<ul>
<li><a href="http://chenlikpharmacy.freeserver.me/index.php" >Home</a></li>
<li><a href="http://chenlikpharmacy.freeserver.me/product_list.php">Products</a></li>
<li><a href="http://chenlikpharmacy.freeserver.me/promotions.php">Promotions</a></li>
<li><a href="http://chenlikpharmacy.freeserver.me/profile.php">Profile</a></li>
<li><a href="http://chenlikpharmacy.freeserver.me/about_us.php" class="active">About Us</a></li>
<li><a href="http://chenlikpharmacy.freeserver.me/contacts.php" >Contacts</a></li>
</ul>
</div>
<!-- End Navigation -->
<div class="cl"> </div>
<!-- Login-details -->
<div id="login-details">
<p>Welcome, <a href="#" id="user">Guest</a> .</p>
<p><a href="http://chenlikpharmacy.freeserver.me/cart.php" class="cart" ><img src="css/images/cart-icon.png" alt="" /></a>Shopping Cart <a href="http://chenlikpharmacy.freeserver.me/cart.php" class="sum"> Cart Total</a></p>
</div>
<!-- End Login-details -->
</div>
<!-- End Header -->
<!-- Main -->
<div id="main" class="shell">
<!-- Products -->
<div id="main" class="products">
<table width="100%" border="1" cellspacing="0" cellpadding="6">
<tr>
<td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
<td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
<td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
<td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
<td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
<td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
</tr>
<?php echo $cartOutput; ?><br/>
<!-- <tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr> -->
</table>
<?php echo $cartTotal; ?>
<br />
<br />
<?php echo $pp_checkout_btn; ?>
<br />
<br />
<a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
<!-- End Products -->
<div class="cl"> </div>
</div>
<div class="cl"> </div>
</div>
<!-- End Main -->
<!-- Footer -->
<div id="footer" class="shell">
<div class="top">
<div class="cnt">
<div class="col about">
<h4>About Chenlik Pharmacy Sdn. Bhd.</h4>
<?php echo $custom1; ?>
</div>
<div class="col store">
<h4>Store</h4>
<?php echo $footer; ?>
</div>
<div class="col" id="newsletter">
<h4>Newsletter</h4>
<p>This function is not activate yet. </p>
<form action="" method="post">
<input type="text" class="field" value="Your Name" title="Your Name" />
<input type="text" class="field" value="Email" title="Email" />
<div class="form-buttons"><input type="submit" value="Submit" class="submit-btn" />
</div>
</form>
</div>
<div class="cl"> </div>
<div class="copy">
<p>©2013 <a href="http://chenlikpharmacy.freeserver.me">Chenlik Pharmacy Sdn. Bhd.</a> Design by <a href="http://css-free-templates.com/">CSS-FREE-TEMPLATES.COM</a>  .Source code credit to: <a href="http://www.developphp.com">Adam Khoury</a>. Modified & Complied by: Philip Tiong</p>
</div>
</div>
</div>
</div>
<!-- End Footer -->
</body>
</html>
要显示错误,您需要将其添加到要显示错误的 html 中:
<?php
if (!empty($err)) :
foreach ($err as $error) :
echo $error;
endforeach;
endif;