douji3426 2014-01-10 05:49
浏览 53
已采纳

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

I am working on a silent auction for my school and have run into a problem. I have a form where they submit their information with a button to actually submit it. When the button is pressed at the moment their information is transferred to a database where I can look over it later and see who is the winner. The user is then directed to a success page where they can go to the bids page which contains the names and items of the people who have bid. The problem is that people can submit the form without actually entering anything. I wrote some code that would make the fields mandatory but since there is a redirection on submit the code is not working properly. I am not sure how to go about fixing this.

I can post the codes here if necessary just let me know. Also if any other clarification is needed just ask.

Here is the form page code:

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

And here is 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> 
");
    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条回答 默认 最新

  • doude2635 2014-01-10 06:31
    关注

    in the database file add the code below

    Server Side Validation

    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 */
     }
    

    also in the front-end

    add required for example in text field
    
     <html>
    

    Silent Auction

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

    if you want interactive client side validation there a many site providing it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组