1

我无法确保我的所有表单输入都填写在 OOP 中。通常我可以做到这一点。但我对 OOP 编程有点陌生。我想确保当我的按钮被点击时。

它将控制我的所有表格是否为空。如果为空,则必须回显一条消息(该消息是荷兰语,但基本上意味着您仍有未填写的输入)。但是,它不起作用。该消息始终显示,即使我确实填写了我的输入并单击提交。我不知道我做错了什么。

我的代码:

<?php

  class UserForm{

//Making properties to later use
private $Vvoornaam;
private $Vachternaam;
private $Vemail;
private $Vbericht;
private $Bsubmit;
public function __contruct() {

//Giving the properties a value

$this->Vvoornaam = $_POST["voornaam"];
$this->Vachternaam = $_POST["achternaam"];
$this->Vemail = $_POST["email"];
$this->$Vbericht = $_POST["bericht"];
$this->$Bsubmit = $_POST["submit"];
}

public function Index() {

//Checking if a message has been posted yes or no. If yes, then execute the code from line 21 to 41

if(isset($_POST[$this->Bsubmit]) && empty($this->Vvoornaam) || empty($this->Vachternaam) || empty($this->Vemail) || empty($this->Vbericht)) {
    echo "U moet nog al uw gegevens invullen";
  }
}
}


if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

   $userForm = new UserForm(); $userForm->Index(); 

}

?>

如您所见,问题是当您提交并且输入为空时应该会出现此消息。然而,这条信息始终存在。

我希望你们能帮助我。那太好了!

放入我的构造后的最新通知

在此处输入图像描述

4

3 回答 3

1

您缺少sin __contruct(),您可以将其更新为:

public function __construct()

另外,我认为这将是空的empty($this->Vbericht)

尝试从$this->$

改变

$this->$Vbericht = $_POST["bericht"];
$this->$Bsubmit = $_POST["submit"];

$this->Vbericht = $_POST["bericht"];
$this->Bsubmit = $_POST["submit"];

你的函数看起来像:

public function Index() {
    if (isset($this->Bsubmit) && empty($this->Vvoornaam) || empty($this->Vachternaam) || empty($this->Vemail) || empty($this->Vbericht)) {
        echo "U moet nog al uw gegevens invullen";
    }
}
于 2018-11-03T10:24:47.263 回答
-1

您是否使用if 语句尝试过类似的操作

if($this->Vvoornaam =='' || $this->Vachternaam=='' || $this->Vemail=='' || $this->$Vbericht==''){
echo "All field must be filled.";
exit();
}else{
echo "everything is filled";
}
于 2018-11-03T10:26:57.237 回答
-1

还要确保在检查$_POST数组中的值为空或未设置时设置属性值。

我会做一个这样的功能

public function ValidateForm($data) {

    if(!isset($data["voornaam"]) && strlen($data["voornaam"]) < 1) {
      echo "Please fill in the 'voornaam' field";
      return false;
    }
    else {
      $this->SetData($data);
      return true;
    }

所以现在我们确保一旦设置了所有输入,我们就可以调用我们的函数:

public function SetData($data) {
  $this->voornaam = $data["voornaam"];
}

您的输入名称将是这样的

name="data[voornaam]"

在您的 html/php 文件中,一旦设置了提交,就会触发该函数。

$obj = new UserForm();
if(isset($_POST["submit"]) { $obj->ValidateForm($_POST["data"]); }
于 2018-11-03T10:35:40.763 回答