@Dimon3x

Как проверить отправленные значения формы, с использованием ООП?

Я создал класс формы

error_reporting(E_ALL);
	
	class Form {
		protected $type ;
		protected $value;
		protected $placeholder;
		protected $action = '';
		protected $method;
		protected $name;
		protected $className;
		
		public function checkValue($arr) {
			if (array_key_exists('value', $arr)) {
				$this->value = $arr['value'];
			}
		}
		
		public function checkName($arr) {
			if (array_key_exists('name', $arr)) {
				$this->name = 'name="'.$arr['name'].'"';
			}
		}
		
		public function checkClass($arr) {
			if (array_key_exists('class', $arr)) {
				$this->className = 'class="' . $arr['class'] . "\" ";
			}
		}
		
		public function input($arr) {
			$this->type = $arr['type'];
			$this->checkName($arr);
			$this->checkValue($arr);
			$this->checkClass($arr);
			return '<input ' . $this->className . $this->name . ' type="' .$this->type . '" value="' .$this->value . '">';
		}
		
		public function password($arr) {
			$this->type = 'text';
			$this->checkName($arr);
			$this->checkValue($arr);
			$this->checkClass($arr);
			
			return '<input '. $this->className . $this->name .' type="' .$this->type . '" value="' .$this->value . '">';
		}
		
		public function submit($arr) {
			$this->type = 'submit';
			$this->checkName($arr);
			$this->checkValue($arr);
			$this->checkClass($arr);
			return '<input ' . $this->className . $this->name .' type="' .$this->type . '" value="' .$this->value . '">';
		}
		
		public function textarea($arr) {
			$this->placeholder = $arr['placeholder'];
			$this->name = $arr['name'];
			$this->checkValue($arr);
			$this->checkClass($arr);
			return '<textarea ' . $this->className . $this->name . ' placeholder="' . $this->placeholder. '">' . $this->value . '</textarea>';
		}
		
		public function open($arr) {
			$this->action = $arr['action'];
			$this->method = $arr['method'];
			$this->checkClass($arr);
			return '<form '.$this->className.' action="'.$this->action.'" method="'.$this->method.'">';
		}
		
		public function close() {
			return '</form>';
		}
	}


Потом создал класс, который всё наследует от класса Form

class SmartForm extends Form {
		protected $value;
		
		 public function checkValue($arr) {
			if(!empty($_POST['name'])) {
				$this->value = $_POST['name'];
			}
			
			 parent::checkValue($arr);
		}
	}
	
	
	$form2 = new SmartForm;
	
	echo $form2->open(['action'=>'./2.php', 'method'=>'POST']) . "\n";
	echo "\t ". $form2->input(['type'=>'text', 'placeholder'=>'Ваше имя', 'name'=>'name', 'class'=>'name']) . "\n";
	echo "\t ". $form2->password(['placeholder'=>'Ваш пароль', 'name'=>'pass', 'class'=>'user-password']) . "\n";
	echo "\t ". $form2->submit(['value'=>'Отправить']) . "\n";
	echo $form2->close();


Почему после отправки формы, в поле password, подставляется значение из name?

Как подставить в каждое поле своё значение?

И почему, если $_POST['name'] изменить на $_POST['pass'], то в поля подставится Отправить? Этого я вообще не понимаю
  • Вопрос задан
  • 237 просмотров
Решения вопроса 1
@MadridianFox
Web-программист, многостаночник
public function checkValue($arr) {
      if(!empty($_POST[$arr['name']])) {
        $this->value = $_POST[$arr['name']];
      }
      
       parent::checkValue($arr);
    }
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
lxfr
@lxfr
1. во первых, ваш код читать никто не будет
2. задайте вопрос по определенному вопросу - ваш вопрос слишком размыт
3. не стоит сюда выкладывать куски кода больше, чем 10-15строк.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Ведисофт Екатеринбург
от 25 000 ₽
YCLIENTS Москва
от 200 000 до 350 000 ₽
от 300 000 до 500 000 ₽
06 мая 2024, в 12:36
30000 руб./за проект
06 мая 2024, в 12:22
5000 руб./за проект
06 мая 2024, в 12:19
1500 руб./в час