gpt4 book ai didi

Php表单提交,切换到另一个页面

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:08:45 24 4
gpt4 key购买 nike

我有一个 add.php 文件,它是一个收集数据的表单。当用户点击 enter.submit 按钮时,add.php 会将信息添加到数据库中,然后它应该转到另一个页面。它应该重定向到一个页面(view.php)。我用过

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

$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>

library2.php:

<?php
//Function to connect to MYSQL server
function connectMysql(){

$link = mysqli_connect('localhost', 'bti-mysql', 'mysqlrocks!', 'bti320')
//$link = mysqli_connect('localhost', 'root', '', 'bti320')
or die('Could not connect: ' . mysqli_error($link));

return $link;
}
?>
<?php
//Function to run the given query, requires $link from connect and SQL query
function runQuery($link, $sql_query){

$result = mysqli_query($link, $sql_query) or die('query failed'. mysqli_error($link));
return $result;
}
?>
<?php
//Function to display logo/header.
function printHeader()
{
?>
<img id="logo" src="logo.png" alt="openclipart.org CC creative commons" height="275" width="420">

<?php
}
?>

<?php
//Function to display footer.
function printFooter()
{
?>
Copyright © 2015 Shashank Inc.
<?php
}
?>

最佳答案

你必须使用

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>

关于Php表单提交,切换到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com