Unable to post form data to controller function

I have a contact us form and user enter data and submits it. This data needs to accessed in controller function so I can send a mail to admin informing about the user request recently received.

But when I press submit button, nothing happens. Form just reloads and the contactus form (view page) is shown to the users. I don’t know why the data is not getting passed to the controller function.

I’mm new to CakePHP framework and I’m using CakePHP3 for development purposes.

Here is my form:

<form action="contactus" method="post">
  
    <div class="col-md-6">
        <input type="text" id="fname" name="fname" placeholder="Your name.." required>
    </div>

    <div class="col-md-6">
        <input type="text" id="mail" name="mail" placeholder="Your Email id.." required>
    </div>

    <div class="col-md-6">
        <textarea id="subject" name="subject" placeholder="Write something.." ></textarea>
    </div>
  
    <div class="col-md-6">
        <input type="submit" value="Submit">
    </div>

</form>

And my controller function is:

 public function contactus()
 {
		$pages ='';
		if ($this->request->is('post')) 
		{
			$pages =   $this->request->data('Contact');
		}
        $this->set(compact('pages'));
        $this->set('_serialize', ['pages']);
	}

Can anyone tell me the mistakes I made?

First of all why you are not using formHelper and also the action should be something like /users/contactus. In this the users is the controller and contactus is the method.

when i gave action like /users/contactus, it throws me an error.

Object not found!
The requested URL was not found on this server.

But that view exists.

Tell me your controller’s name and the version of CakePHP.

Version: 3.4.*

Controller: Pages

Try with this form

<?php echo $this->Form->create(null, ['url' => ['controller' => 'Pages', 'action' => 'contactus']]); ?>
    <div class="col-md-6">
        <?php echo $this->Form->input('fname', ['placeholder' => 'Your name.' , 'id' => 'fname', 'required' => 'required']); ?>
    </div>
    <div class="col-md-6">
        <?php echo $this->Form->input('mail', ['placeholder' => 'Your email.' , 'id' => 'mail', 'required' => 'required']); ?>
    </div>
    <div class="col-md-6">
        <?php echo $this->Form->input('subject', ['placeholder' => 'Write something.', 'id' => 'subject']); ?>
    </div>

    <div class="col-md-9">
        <?php echo $this->Form->button(__('Submit')); ?>
    </div>         
<?php echo $this->Form->end(); ?>

In your method just try debug($this->request->data);

2 Likes

I tried the form you provided. Also tried to debug.
Nothing is happening. It seems like form gets reloaded when I click submit button.

Show the code of contactus method.

	public function contactus()
    {
		 debug($this->request->data);
		$pages ='';
		if ($this->request->is('post')) 
		{
			echo "Request is post";die;
			$pages =   $this->request->data('Contact');
		}else{
			echo "request is not post";die;
		}
		$this->set(compact('pages'));
		$this->set('_serialize', ['pages']);
	}

Try this.

public function contactus() {
    $pages ='';
    if ($this->request->is('post')) {
        debug($this->request->data);exit;
        $pages =   $this->request->data('Contact');
    }
    $this->set(compact('pages'));
    $this->set('_serialize', ['pages']);
}