Implemented the following design:

Form with action = "/?page=pages&controller=add" where page is the name of the class, controller is the name of the method to which you need to transfer data from the form.

 public function add() { if (!empty($_POST)) { echo "<pre>"; print_r($_POST); echo "</pre>"; } else { echo "ERROR!!!"; } } 

However, when sending a request returns ERROR!!! .
If you look in Firebug, then after submitting the form, it is executed first.

POST constructor?page=pages&controller=add

(POST passed variables are displayed) with status 301 Moved Permanently, and then

GET ?page=pages&controller=add

which returns the answer ERROR!!! .

Why can't I transfer data?

The form:

 <form action="<?php echo BASEPATH;?>?page=pages&controller=add" method = "post"><br/> <input name ="title" placeholder ="Наименование"/><br/> <textarea name="description" placeholder="Описание"></textarea><br/> <textarea name="text" placeholder ="Текст"></textarea><br/> <textarea name="metakey" placeholder="META-Keywords"></textarea><br/> <textarea name="metadescr" placeholder ="META-Description"></textarea><br/> <input type = "submit" name = "add" value = "Сохранить"/> </form> 

And here is the processing of the address bar parameters:

  public function doCommand($uri, $type){ $controller = ''; $flag = FALSE; $dir = CMPDIR; $f = scandir($dir); foreach ($f as $file){ if(preg_match('/\.(php)/', $file)){ if($uri === substr($file, 0, -4)){ $flag = TRUE; $controller = CMPDIR.$file; break; } } } if($flag === FALSE){ $error['error'] = 'Страница <b>'.$_SERVER['HTTP_HOST'].'/'.implode('/',$uri).'</b> не существует'; $this->load('error404', $error); } else { include strtolower($controller); $cl = ucfirst($uri); $class = new $cl; $md = FALSE; $methreg = ''; $method = get_class_methods ( $class ); for ($i = 0; $i < count($method); $i ++){ if (strtolower($method[$i]) === strtolower($type)){ $md = TRUE; $methreg = $method[$i]; break; } } if ($md === TRUE){ return $class->$methreg(); } else { $error['error'] = 'Method не существует'; $this->load('error404', $error); } } } 
  • We need more details about sending data, whether some JS handler was hung on the submit-button, it would be good to bring in the question and the form itself. How does the processing of form data occur? Is there a redirect at the end of this processing that results in a GET request for the form? - cheops

2 answers 2

The fact is that the $_POST array is filled only if you transmit data using the POST method and the page=pages&controller=add placed in the body of the HTTP request. If you transfer data using the GET method with GET parameters in the address (ie, in HTTP headers), then the parameters fall into $_GET , and the $_POST array remains empty.

  • How can this be fixed? in the admins of various CMS (for example, Joomla) POST data is sent to the page with parasetras - M.Sol
  • @ M.Sol We need details to figure out why you get a second GET request, it’s almost impossible to tell from the available data even where it is initiated in JS or in PHP code. - cheops

The problem was solved by adding a slash before the parameters in the action form:

 <form action="<?php echo BASEPATH;?>/?page=pages&controller=add" method = "post"><br/>