如何在表单提交中进行多项操作? PHP

时间:2014-01-10 05:49:39

标签: php forms validation onsubmit

我正在为我的学校进行无声拍卖,并遇到了一个问题。我有一个表单,他们用一个按钮提交他们的信息,以实际提交它。当按下按钮时,他们的信息被传输到数据库,我可以稍后查看它,看看谁是赢家。然后,用户被引导到成功页面,在那里他们可以进入出价页面,该页面包含出价人员的姓名和项目。问题是人们可以在没有实际输入任何内容的情况下提交表单。我编写了一些代码,这些代码会使字段成为必填字段,但由于提交时存在重定向,因此代码无法正常工作。我不知道如何解决这个问题。

我可以在这里发布代码,如果有必要,请告诉我。如果需要任何其他说明,请询问。

这是表单页面代码:

<?php

$nameERR = $numberERR = $emailERR = $bidERR = "";
$name = $number = $email = $bid = "";

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    if (empty($_POST["name"]))
    {
        $nameERR = "Name is required";
    }
    if (empty($_POST["number"]))
    {
        $numberERR = "Student number is required";
    }
    if (empty($_POST["email"]))
    {
        $emailERR = "E-mail is required";
    }
    if (empty($_POST["price"]))
    {
        $bidERR = "A bid is required";
    }
}

    ?>
    <html>

    <head>
<title>Silent Auction</title>   
    </head>

    <body>

<form method="POST" action="database.php">
    <b>Please Enter the Following Information Accurately</p>
    <br>
    Name:
    <br>
    <input type="text" name="name" style="width: 200px;" />
    <span class="error"><?php echo $nameErr ?></span>
    <br>
    Student Number:
    <br>
    <input type="text" name="number" style="width: 200px;" />
    <span class="error"><?php echo $numberErr ?></span>
    <br>
    E-mail:
    <br>
    <input type="text" name="email" style="width: 200px;" />
    <span class="error"><?php echo $emailErr ?></span>
    <br>
    Item:
    <br>
    <select name="item" style="width: 200px;">
        <option name="thing" value="thing">Thing</option>
        <option name="other" value="other">Other Thing</option>
    </select>
    <br>
    Bid Amount:
    <br>
    <input type="text" name="price" />
    <span class="error"><?php echo $bidErr ?></span>
    <br>
    <input type="submit" value="Place Bid" />
    <br>
    <br>
    <a href="bids.html">Open Bids Page</a>
</form>

    </body>

    </html>

这是database.php:

<?php
//Create connection
$con = mysqli_connect("localhost","user","pass","database");
//Test connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySql: " . mysqli_connect_error();
}

if($_POST)
{
    $name = $_POST['name'];
    $bid = $_POST['price'];
    $site = fopen("bids.html","a");
    fwrite($site, $name . " - " . $item . "<br> \n");
    fclose($site);
}

$sql = "INSERT INTO Persons (name, number, email, item, price) VALUES 
(
    '$_POST[name]',
    '$_POST[number]',
    '$_POST[email]',
    '$_POST[item]',
    '$_POST[price]'
)";

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "1 record added. Click " . "<a href='bids.html'>Here</a>" . " to view other bids.";

mysqli_close($con);
?>

5 个答案:

答案 0 :(得分:2)

服务器端验证没有任何问题......实际上,您应该同时执行这两项操作。

这应该可行,只需在同一个脚本中执行整个操作。

<?php

$nameERR = $numberERR = $emailERR = $bidERR = "";
$name = $number = $email = $bid = "";
$errors = false;

if(isset($_POST['submit']))
{
    if (empty($_POST["name"]))
    {
        $nameERR = "Name is required";
        $errors = true;
    }
    if (empty($_POST["number"]))
    {
        $numberERR = "Student number is required";
        $errors = true;
    }
    if (empty($_POST["email"]))
    {
        $emailERR = "E-mail is required";
        $errors = true;
        }
    if (empty($_POST["price"]))
    {
        $bidERR = "A bid is required";
        $errors = true;
    }


        if(!$errors) {

            //Create connection
            $con = mysqli_connect("localhost","user","pass","database");
            //Test connection
            if (mysqli_connect_errno())
            {
                    echo "Failed to connect to MySql: " . mysqli_connect_error();
            }

            if($_POST)
            {
                    $name = $_POST['name'];
                    $bid = $_POST['price'];
                    $site = fopen("bids.html","a");
                    fwrite($site, $name . " - " . $item . "<br> \n");
                    fclose($site);
            }

            $sql = "INSERT INTO Persons (name, number, email, item, price) VALUES 
            (
                    '$_POST[name]',
                    '$_POST[number]',
                    '$_POST[email]',
                    '$_POST[item]',
                    '$_POST[price]'
            )";

            if (!mysqli_query($con,$sql))
            {
                    die('Error: ' . mysqli_error($con));
            }
            echo "1 record added. Click " . "<a href='bids.html'>Here</a>" . " to view other bids.";

            mysqli_close($con);

        }

}

?>
<html>

<head>
    <title>Silent Auction</title>   
</head>

  <body>

    <form method="POST" action="">
        <b>Please Enter the Following Information Accurately</p>
        <br>
        Name:
        <br>
        <input type="text" name="name" style="width: 200px;" />
        <span class="error"><?php echo $nameErr ?></span>
        <br>
        Student Number:
        <br>
        <input type="text" name="number" style="width: 200px;" />
        <span class="error"><?php echo $numberErr ?></span>
        <br>
        E-mail:
        <br>
        <input type="text" name="email" style="width: 200px;" />
        <span class="error"><?php echo $emailErr ?></span>
        <br>
        Item:
        <br>
        <select name="item" style="width: 200px;">
            <option name="thing" value="thing">Thing</option>
            <option name="other" value="other">Other Thing</option>
        </select>
        <br>
        Bid Amount:
        <br>
        <input type="text" name="price" />
        <span class="error"><?php echo $bidErr ?></span>
        <br>
        <input type="submit" name="submit" value="Place Bid" />
        <br>
        <br>
        <a href="bids.html">Open Bids Page</a>
    </form>

  </body>

</html>

答案 1 :(得分:2)

您应该使用javascript进行客户端验证。对于服务器端,您应该使用该php验证。我把sql查询放在if($ _ POST)块中。因此,只有当您拥有有效数据时,才会更新数据库。     

if(!empty($_POST['name'))
{
   $name = $_POST['name'];
   $bid = $_POST['price'];
   $site = fopen("bids.html","a");
   fwrite($site, $name . " - " . $item . "<br> \n");
   fclose($site);


$sql = "INSERT INTO Persons (name, number, email, item, price) VALUES 
(
   '$_POST[name]',
   '$_POST[number]',
   '$_POST[email]',
   '$_POST[item]',
   '$_POST[price]'
)";


if (!mysqli_query($con,$sql))
{
 die('Error: ' . mysqli_error($con));
}
echo "1 record added. Click " . "<a href='bids.html'>Here</a>" . " to view other bids.";

mysqli_close($con);
}
?>

答案 2 :(得分:2)

在数据库文件中添加以下代码

服务器端验证

if($_SERVER["REQUEST_METHOD"] == "POST")
     {
if (empty($_POST["name"]))
{
     $nameERR = "Name is required"; 
     die($nameERR);
}
if (empty($_POST["number"]))
{
    $numberERR = "Student number is required";
    die($numberERR);

}
if (empty($_POST["email"]))
{
    $emailERR = "E-mail is required";
    die($emailERR);
}
if (empty($_POST["price"]))
{
    $bidERR = "A bid is required";
    die( $bidERR);
}



  /* paste reset of code here */
 }

也在前端

add required for example in text field

 <html>

    无声拍卖

<form method="POST" action="">
    <b>Please Enter the Following Information Accurately</p>
    <br>
    Name:
    <br>
    <input type="text" name="name" style="width: 200px;" required />
    <span class="error"><?php echo $nameErr ?></span>
    <br>
    Student Number:
    <br>
    <input type="text" name="number" style="width: 200px;" required />
    <span class="error"><?php echo $numberErr ?></span>
    <br>
    E-mail:
    <br>
    <input type="text" name="email" style="width: 200px;" required />
    <span class="error"><?php echo $emailErr ?></span>
    <br>
    Item:
    <br>
    <select name="item" style="width: 200px;" required>
        <option name="thing" value="thing">Thing</option>
        <option name="other" value="other">Other Thing</option>
    </select>
    <br>
    Bid Amount:
    <br>
    <input type="text" name="price" requried />
    <span class="error"><?php echo $bidErr ?></span>
    <br>
    <input type="submit" name="submit" value="Place Bid" required />
    <br>
    <br>
    <a href="bids.html">Open Bids Page</a>
</form>

如果您想要交互式客户端验证,那么有很多站点提供它

答案 3 :(得分:0)

目前,您正在进行服务器端验证。

使用将在客户端执行的Javascript / jquery验证。

答案 4 :(得分:0)

好奇,这是为了什么目的:

$nameERR = $numberERR = $emailERR = $bidERR = "";
$name = $number = $email = $bid = "";