草庐IT

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

coder 2024-04-12 原文

我有一个 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/

有关Php表单提交,切换到另一个页面的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - Ruby on Rails with Haml - 如何从 erb 切换 - 2

    我正在从erb文件切换到HAML。我将hamlgem添加到我的系统中。我创建了app/views/layouts/application.html.haml文件。我应该只删除application.html.erb文件吗?此外,仍然有/public/index.html文件被呈现为默认页面。我想创建自己的默认index.html.haml页面。我应该把它放在哪里以及如何使系统呈现该文件而不是默认索引文件?谢谢! 最佳答案 是的,您可以删除任何已转换为HAML的View的ERB版本。至于你的另一个问题,删除public/index/h

  6. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  7. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  8. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  9. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  10. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

随机推荐