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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)