version 2
version 2

Reputation: 1059

cakephp3- 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:

<?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(); ?>

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?

Upvotes: 1

Views: 1288

Answers (1)

bikash.bilz
bikash.bilz

Reputation: 821

I think your form is submitting but not through the post method. So I would like to say you that, please make the bellow changes before submitting the form.

$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']);

Now check, which is printing in the display. Then I can help you further.

Remember: - fill the form, then after change the controller method, then press submit method.

Upvotes: 1

Related Questions