doutuo1939 2016-05-23 21:42
浏览 96
已采纳

使用PHP和HTML在表单上执行多个操作

I am creating a website, and part of the website should be a Bookings section. I have created a form, and the data entered into this form is to be stored in a database. Up to this point everything works fine. However, I also need to validate the data input by the user. Such as checking that characters only are entered in the Name text field, etc..I have also managed to do this correctly.

The problem is, that the form is not allowing me to do both functions, as there can only be one "action" in the form tag. The form is allowing me to store data in the database whenever I write action="Database.php" and allowing me to validate the user input whenever I write action= . I need a function which can combine both as I need to do both for my website. Do you have any suggestions please?

This is the code:

<p><span class="error"></span></p>
<form class="MyForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

    * required field
    <br>
    <br>
   Name and Surname:  <br><input type="text" name="name">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>

   E-mail: <br><input type="text" name="email">
   <span class="error"> <?php echo $emailErr;?></span>
   <br><br>
   Contact Number:  <br><input type="text" name="contactnumber">
   <span class="error">* <?php echo $contactnumberErr;?></span>

   <br><br>
   Number of people:  <br><input type="text" name="numberofpeople">
   <span class="error">* <?php echo $numberofpeopleErr;?></span>
   <br><br>
   Date:  <br><input type="date" name="date">
   <span class="error">* <?php echo $dateErr;?></span>
   <br><br>
   Time of Booking:  <br><input type="time" name="timeofbooking">
   <span class="error">* <?php echo $timeofbookingErr;?></span>
   <br><br>
   Comments: <br><textarea name="comment" rows="5" cols="40">Comments...</textarea>
   <br><br>
   <button class="button button1">Submit</button>
   <br>
</form>

And this is the code for the validation in PHP:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $contactnumberErr = $numberofpeopleErr = $dateErr = $timeofbookingErr = "";
$name = $email = $contactnumber = $numberofpeople = $date = $timeofbooking = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name and Surname are required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed";
    }
  }
  if (!empty($_POST["email"])) {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }
  if (empty($_POST["contactnumber"])) {
    $contactnumberErr = "Contact number is required";
  } else {
    $contactnumber = test_input($_POST["contactnumber"]);

    if (!preg_match("/^[0-9 ]*$/",$contactnumber)) {
      $contactnumberErr = "Only numbers are allowed";
    }
  }
  if (empty($_POST["numberofpeople"])) {
    $numberofpeopleErr = "Number of people is required";
  } else {
    $numberofpeople = test_input($_POST["numberofpeople"]);

    if (!preg_match("/^[0-9 ]*$/",$name)) {
      $numberofpeopleErr = "Only 2 digit numbers are allowed";
    }
  }
  if (empty($_POST["date"])) {
    $dateErr = "Date is required";
  } else {
    $date = test_input($_POST["date"]);
    }
    if (empty($_POST["time"])) {
    $timeErr = "Time is required";
  } else {
    $time = test_input($_POST["time"]);
    }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
  • 写回答

2条回答 默认 最新

  • doumei1955 2016-05-23 21:54
    关注

    Explaining a few important fundamentals:

    The action="database.php" or action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" just refers to where you want the form information to be sent, and does not limit you to how much you can do as you are suggesting with your question. I'll explain this in a simple manner:

    If you write the name of a file in your action attribute like so:

    <form action="database.php" method="POST">
    //... more code
    

    That just means that you are going to send the values that were inside the form to the page database.php, and at the same time, you will be redirected to that page.

    If you use the following however:

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    //... more code
    

    that just means that you want to send the form information to the same page and you want to redirect the user to the very same page.

    You might ask yourself "but why does code exist that redirects you to the same page?". Well that has to do with the passing of information. If you load a page that has a form in it, after the page has loaded, you no longer have a connection with the website (there are exceptions like when using AJAX code). Since you no longer have a connection, your browser will have to create another connection when you hit the "submit" button on the form. When that connection is made, the form tells the browser: "send this information to this page and redirect me there to". That way, your message is sent to the server, the server can see the input from the form, and process it.

    So by redirecting the user to the same page, you are giving the browser a chance to reconnect with the server of the website so that It can pass the information you placed in the form.

    ANSWER TO YOUR PROBLEM:

    So finally... back to the meat of the question. All you have to do is include code that processes both the database information and that does the validation that you want on the same page (regardless if it is in database.php or to the very same page.

    You are not being limited to just doing 1 process because your action attribute is different. You just have to make sure that all the processes you want are coded into the page where the form is sending the data.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c