I am using cakePHP5 and I'm having trouble submitting post values with FormHelper and it's a modelless form. Here is the code .
C:\xampp\htdocs\cake3app\templates\Hello\index.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html;" charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
<h1>sample</h1>
<p><?=$result;?></p>
<?=$this->Form->create(null,
['type'=>'post','url'=>['controller'=>'Hello',
'action'=>'index']]) ?>
<?=$this->Form->text("HelloForm.text1");?>
<?=$this->Form->submit("submit");?>
<?=$this->Form->end();?>
</body>
</html>
C:\xampp\htdocs\cake3app\src\Controller\HelloController.php
<?php
namespace App\Controller;
class HelloController extends AppController{
public function initialize():void{
parent::initialize();
$this->name='Hello';
$this->autoRender = true;
$this->viewBuilder()->setLayout('Hello');
$this->set('msg','Hello\index');
$this->set('footer','Hello\footer2');
header('Content-Type: application/x-www-form-urlencoded');
}
public function index(){
$result="";
$this->modelClass = null;
if($this->request->is('Post')){
$result="submitted data";
foreach($this->request->getData('HelloForm') as $key->$val){
$result.=$key."=>".$val;
}
$result.="";
}else{
$result="write something...";
}
$this->set("result",$result);
}
}
?>
I read the manual https://book.cakephp.org/5/en/core-libraries/form.html and it says to make src/Form/ContactForm.php but there's no Form folder in the installed package.
Also the form I posted above returns error log when I try to submit it.
<`div class="cake-error"> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr670f67f47a8af-trace').style.display = (document.getElementById('cakeErr670f67f47a8af-trace').style.display == 'none' ? '' : 'none')"
<b>Warning</b> (2)
: Undefined variable $val [in C:\xampp\htdocs\cake3app\src\Controller\HelloController.php, line 23] <div id="cakeErr670f67f47a8af-trace" class="cake-stack-trace" style="display: none;"> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr670f67f47a8af-code').style.display = (document.getElementById('cakeErr670f67f47a8af-code').style.display == 'none' ? '' : 'none')"
I spent 1 day and it seems quite complicated for me I would really appreciate help.
If you want to create ContactForm
then you should run with command bin/cake bake form ContactForm