Php表单提交,更改到其他页面
我有一个add.php文件,它是一个收集数据的表单。当用户点击enter.submit按钮时,add.php会将信息添加到数据库中,然后它会转到另一个页面。它应该重定向到一个页面(view.php)。我用过 标题(“Location:view.php”) 我的代码:Php表单提交,更改到其他页面,php,Php,我有一个add.php文件,它是一个收集数据的表单。当用户点击enter.submit按钮时,add.php会将信息添加到数据库中,然后它会转到另一个页面。它应该重定向到一个页面(view.php)。我用过 标题(“Location:view.php”) 我的代码: <html> <head> <meta charset="UTF-8"> <?php include "library2.php"; prin
<html>
<head>
<meta charset="UTF-8">
<?php
include "library2.php";
printHeader();
?>
<title>Punch Electronics Inc.</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles.css">
<nav>
<ul>
<li>
<a href="add.php"> Add</a>
</li>
<li>
<a href="view.php">View</a>
</li>
</ul>
</nav>
</head>
<body>
<?php
$itemNameErr ="";
$descriptionErr ="";
$suppCodeErr="";
$costErr="";
$sellingPriceErr="";
$numberOnHandErr="";
$reorderPointErr="";
$dataValid = true;
// If submit with POST
if ($_POST) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
//Check if empty, if not then check if it's valid.
if ($itemName== "") {
$itemNameErr = "Error! Please enter an item name.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z :;\-,'0-9]{3,40}$/",$itemName))
{
$itemNameErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($description == "") {
$descriptionErr = "Error! Please enter a description.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z 0-9.,'\-\r\n]{4,2000}$/",$description))
{
$descriptionErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($suppCode == "") {
$suppCodeErr = "Error! Please enter a supplier code.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z \-0-9]{3,40}$/",$suppCode))
{
$suppCodeErr = "Error! Please enter a valid supplier code.";
$dataValid = false;
}
if ($cost == "") {
$costErr = "Error! Please enter a cost";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$cost))
{
$costErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($sellingPrice == "") {
$sellingPriceErr = "Error! Please enter a selling price";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$sellingPrice))
{
$sellingPriceErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($numberOnHand == "") {
$numberOnHandErr = "Error! Please enter number on hand.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$numberOnHand))
{
$numberOnHandErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($reorderPoint == "") {
$reorderPointErr = "Error! Please enter reorder point.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$reorderPoint))
{
$reorderPointErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
}
//If submit with POST and Valid data.
if ($_POST && $dataValid) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
if($_POST['backOrder']==="yes")
{
$backOrder = "y";
}
else $backOrder = "n";
$link = connectMysql();
$sql_query = "INSERT INTO inventory VALUES ('','$itemName', '$description', '$suppCode', '$cost','$sellingPrice', '$numberOnHand','$reorderPoint', '$backOrder', 'n');";
$result = runQuery($link, $sql_query);
//Change to view.php after successfully posted and inserted into database.
header("Location: view.php");
?>
<?php
// If no submit or data is invalid, print form, repopulating fields and print error msg
} else {
?>
</br></br></br>
<div id="requirement">
All fields in <span>*</span> are mandatory.
</div>
<div id="form">
<form action="" id= "add" method="post">
<table>
<tr>
<td>Item Name: </td>
<td><input id="itemInput" name="itemName" type="text" value="<?php if (isset($_POST['itemName'])) echo $_POST['itemName']; ?>"><span >* <?php echo $itemNameErr;?></span></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="10" cols="40" name="description" type="text"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea><span>* <?php echo $descriptionErr;?></></td>
</tr>
<tr>
<td>Supplier Code:</td>
<td><input id="suppInput" name="suppCode" type="text" value="<?php if (isset($_POST['suppCode'])) echo $_POST['suppCode']; ?>"><span >* <?php echo $suppCodeErr;?></span></td>
</tr>
<tr>
<td>Cost:</td>
<td><input name="cost" type="text" value="<?php if (isset($_POST['cost'])) echo $_POST['cost']; ?>"><span >* <?php echo $costErr;?></span></td>
</tr>
<tr>
<td>Selling Price:</td>
<td><input name="sellingPrice" type="text" value="<?php if (isset($_POST['sellingPrice'])) echo $_POST['sellingPrice']; ?>"><span >* <?php echo $sellingPriceErr;?></span></td>
</tr>
<tr>
<td>Number On Hand:</td>
<td><input name="numberOnHand" type="text" value="<?php if (isset($_POST['numberOnHand'])) echo $_POST['numberOnHand']; ?>"><span >* <?php echo $numberOnHandErr;?></span></td>
</tr>
<tr>
<td>Reorder Point:</td>
<td><input name="reorderPoint" type="text" value="<?php if (isset($_POST['reorderPoint'])) echo $_POST['reorderPoint']; ?>"><span >* <?php echo $reorderPointErr;?></span></td>
</tr>
<tr>
<td>On Back Order:</td>
<td>
<input name="backOrder" type="checkbox" value="yes" <?php if (isset($_POST['backOrder'])) echo 'checked'; ?> />
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</table>
</tr>
</form>
</div>
<?php
}
?>
</body>
<footer>
<?php
printFooter();
?>
</footer>
潘克电子公司。
-
-
你必须使用
header("Location: view.php");
在任何其他输出到页面之前
在这里发布add.php的内容
这是add.php的新内容:
<?php
include "library2.php";
$itemNameErr ="";
$descriptionErr ="";
$suppCodeErr="";
$costErr="";
$sellingPriceErr="";
$numberOnHandErr="";
$reorderPointErr="";
$dataValid = true;
// If submit with POST
if ($_POST) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
//Check if empty, if not then check if it's valid.
if ($itemName== "") {
$itemNameErr = "Error! Please enter an item name.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z :;\-,'0-9]{3,40}$/",$itemName))
{
$itemNameErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($description == "") {
$descriptionErr = "Error! Please enter a description.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z 0-9.,'\-\r\n]{4,2000}$/",$description))
{
$descriptionErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($suppCode == "") {
$suppCodeErr = "Error! Please enter a supplier code.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z \-0-9]{3,40}$/",$suppCode))
{
$suppCodeErr = "Error! Please enter a valid supplier code.";
$dataValid = false;
}
if ($cost == "") {
$costErr = "Error! Please enter a cost";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$cost))
{
$costErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($sellingPrice == "") {
$sellingPriceErr = "Error! Please enter a selling price";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$sellingPrice))
{
$sellingPriceErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($numberOnHand == "") {
$numberOnHandErr = "Error! Please enter number on hand.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$numberOnHand))
{
$numberOnHandErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($reorderPoint == "") {
$reorderPointErr = "Error! Please enter reorder point.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$reorderPoint))
{
$reorderPointErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
}
if ($_POST && $dataValid) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
if($_POST['backOrder']==="yes")
{
$backOrder = "y";
}
else $backOrder = "n";
$link = connectMysql();
$sql_query = "INSERT INTO inventory VALUES ('','$itemName', '$description', '$suppCode', '$cost','$sellingPrice', '$numberOnHand','$reorderPoint', '$backOrder', 'n');";
$result = runQuery($link, $sql_query);
//Change to view.php after successfully posted and inserted into database.
header("Location: view.php");
}
?>
<html>
<head>
<meta charset="UTF-8">
<?php
//include "library2.php";
printHeader();
?>
<title>Punch Electronics Inc.</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles.css">
<nav>
<ul>
<li>
<a href="add.php"> Add</a>
</li>
<li>
<a href="view.php">View</a>
</li>
</ul>
</nav>
</head>
<body>
<?php
if ($_POST && $dataValid) {
// If no submit or data is invalid, print form, repopulating fields and print error msg
} else {
?>
</br></br></br>
<div id="requirement">
All fields in <span>*</span> are mandatory.
</div>
<div id="form">
<form action="" id= "add" method="post">
<table>
<tr>
<td>Item Name: </td>
<td><input id="itemInput" name="itemName" type="text" value="<?php if (isset($_POST['itemName'])) echo $_POST['itemName']; ?>"><span >* <?php echo $itemNameErr;?></span></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="10" cols="40" name="description" type="text"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea><span>* <?php echo $descriptionErr;?></></td>
</tr>
<tr>
<td>Supplier Code:</td>
<td><input id="suppInput" name="suppCode" type="text" value="<?php if (isset($_POST['suppCode'])) echo $_POST['suppCode']; ?>"><span >* <?php echo $suppCodeErr;?></span></td>
</tr>
<tr>
<td>Cost:</td>
<td><input name="cost" type="text" value="<?php if (isset($_POST['cost'])) echo $_POST['cost']; ?>"><span >* <?php echo $costErr;?></span></td>
</tr>
<tr>
<td>Selling Price:</td>
<td><input name="sellingPrice" type="text" value="<?php if (isset($_POST['sellingPrice'])) echo $_POST['sellingPrice']; ?>"><span >* <?php echo $sellingPriceErr;?></span></td>
</tr>
<tr>
<td>Number On Hand:</td>
<td><input name="numberOnHand" type="text" value="<?php if (isset($_POST['numberOnHand'])) echo $_POST['numberOnHand']; ?>"><span >* <?php echo $numberOnHandErr;?></span></td>
</tr>
<tr>
<td>Reorder Point:</td>
<td><input name="reorderPoint" type="text" value="<?php if (isset($_POST['reorderPoint'])) echo $_POST['reorderPoint']; ?>"><span >* <?php echo $reorderPointErr;?></span></td>
</tr>
<tr>
<td>On Back Order:</td>
<td>
<input name="backOrder" type="checkbox" value="yes" <?php if (isset($_POST['backOrder'])) echo 'checked'; ?> />
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</table>
</tr>
</form>
</div>
<?php
}
?>
</body>
<footer>
<?php
printFooter();
?>
</footer>
在标题(“Location:view.PHP”)之前输出html。。。这意味着已经发送了标题。我测试了您的代码,标题的位置(“Location:view.php”);没关系
检查您的数据库命令?{maybe header(“Location:view.php”)}永远不会运行,并且您的数据库命令会引发错误!!!
良好的锁定发布您的代码。您在哪里添加了标题(“Location:view.php”)代码>你看到了什么错误?嗯。。。使用HTML?你到底希望我们做什么?告诉你在页面上使用什么颜色?在上面放什么数据?这不是我们来这里的目的。。。你可爱的漏洞只是乞求你的网站和服务器被摧毁。。。至少你已经做到了这一点。这里面有什么问题吗?随便猜猜,你应该在任何代码输出到浏览器之前(在任何回显、空白或任何内容之前)添加头()。如果不是这样,它将不起作用解释问题没有理解编辑只是变成空白,它甚至没有添加到数据库它应该起作用,请检查,如果您正确复制了add.php源代码。如果需要,我还可以模拟数据库。但是代码没问题,请再次检查,我在这里发布的内容。示例-这两种方法都应该适用。它会在CentOs上引发任何错误吗?我也在CentOs(我发给你的链接)上测试了它。若要查看任何错误(不仅仅是空白页),请在代码开头添加以下内容:当然,在CentOs上,您使用的是不同的数据库连接字符串,因此请检查它,如果它正确的话……显示时没有错误,但现在它显示了页面。但是它没有进入view.php。表单已不存在,但在地址栏中它仍在add.phpDid中。您是否更改了某些内容?现在没事了,以前不是吗?我没有更改我的代码。是的,我知道,有没有其他方法可以更改页面?我的代码是正确的,但如果有任何其他错误,请再次检查。当然,在CentOs上,您正在使用不同的数据库连接字符串,所以请检查它。。。
<?php
include "library2.php";
$itemNameErr ="";
$descriptionErr ="";
$suppCodeErr="";
$costErr="";
$sellingPriceErr="";
$numberOnHandErr="";
$reorderPointErr="";
$dataValid = true;
// If submit with POST
if ($_POST) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
//Check if empty, if not then check if it's valid.
if ($itemName== "") {
$itemNameErr = "Error! Please enter an item name.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z :;\-,'0-9]{3,40}$/",$itemName))
{
$itemNameErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($description == "") {
$descriptionErr = "Error! Please enter a description.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z 0-9.,'\-\r\n]{4,2000}$/",$description))
{
$descriptionErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($suppCode == "") {
$suppCodeErr = "Error! Please enter a supplier code.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z \-0-9]{3,40}$/",$suppCode))
{
$suppCodeErr = "Error! Please enter a valid supplier code.";
$dataValid = false;
}
if ($cost == "") {
$costErr = "Error! Please enter a cost";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$cost))
{
$costErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($sellingPrice == "") {
$sellingPriceErr = "Error! Please enter a selling price";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$sellingPrice))
{
$sellingPriceErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($numberOnHand == "") {
$numberOnHandErr = "Error! Please enter number on hand.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$numberOnHand))
{
$numberOnHandErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($reorderPoint == "") {
$reorderPointErr = "Error! Please enter reorder point.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$reorderPoint))
{
$reorderPointErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
}
if ($_POST && $dataValid) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
if($_POST['backOrder']==="yes")
{
$backOrder = "y";
}
else $backOrder = "n";
$link = connectMysql();
$sql_query = "INSERT INTO inventory VALUES ('','$itemName', '$description', '$suppCode', '$cost','$sellingPrice', '$numberOnHand','$reorderPoint', '$backOrder', 'n');";
$result = runQuery($link, $sql_query);
//Change to view.php after successfully posted and inserted into database.
header("Location: view.php");
}
?>
<html>
<head>
<meta charset="UTF-8">
<?php
//include "library2.php";
printHeader();
?>
<title>Punch Electronics Inc.</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles.css">
<nav>
<ul>
<li>
<a href="add.php"> Add</a>
</li>
<li>
<a href="view.php">View</a>
</li>
</ul>
</nav>
</head>
<body>
<?php
if ($_POST && $dataValid) {
// If no submit or data is invalid, print form, repopulating fields and print error msg
} else {
?>
</br></br></br>
<div id="requirement">
All fields in <span>*</span> are mandatory.
</div>
<div id="form">
<form action="" id= "add" method="post">
<table>
<tr>
<td>Item Name: </td>
<td><input id="itemInput" name="itemName" type="text" value="<?php if (isset($_POST['itemName'])) echo $_POST['itemName']; ?>"><span >* <?php echo $itemNameErr;?></span></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="10" cols="40" name="description" type="text"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea><span>* <?php echo $descriptionErr;?></></td>
</tr>
<tr>
<td>Supplier Code:</td>
<td><input id="suppInput" name="suppCode" type="text" value="<?php if (isset($_POST['suppCode'])) echo $_POST['suppCode']; ?>"><span >* <?php echo $suppCodeErr;?></span></td>
</tr>
<tr>
<td>Cost:</td>
<td><input name="cost" type="text" value="<?php if (isset($_POST['cost'])) echo $_POST['cost']; ?>"><span >* <?php echo $costErr;?></span></td>
</tr>
<tr>
<td>Selling Price:</td>
<td><input name="sellingPrice" type="text" value="<?php if (isset($_POST['sellingPrice'])) echo $_POST['sellingPrice']; ?>"><span >* <?php echo $sellingPriceErr;?></span></td>
</tr>
<tr>
<td>Number On Hand:</td>
<td><input name="numberOnHand" type="text" value="<?php if (isset($_POST['numberOnHand'])) echo $_POST['numberOnHand']; ?>"><span >* <?php echo $numberOnHandErr;?></span></td>
</tr>
<tr>
<td>Reorder Point:</td>
<td><input name="reorderPoint" type="text" value="<?php if (isset($_POST['reorderPoint'])) echo $_POST['reorderPoint']; ?>"><span >* <?php echo $reorderPointErr;?></span></td>
</tr>
<tr>
<td>On Back Order:</td>
<td>
<input name="backOrder" type="checkbox" value="yes" <?php if (isset($_POST['backOrder'])) echo 'checked'; ?> />
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</table>
</tr>
</form>
</div>
<?php
}
?>
</body>
<footer>
<?php
printFooter();
?>
</footer>