1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried, nothing gets printed. I fill the form, then I click submit button but I don't see the echo as per the solution. It seems like the form reloads and nothing else. I just see the contact us form again. @bikash.bilz
Maybe there is wrong method calling while form submit. Use cake form helper, I think this will help you. You can take follow up from this link. @Anna Belle book.cakephp.org/3.0/en/views/helpers/form.html
Or at least in the start of the form use <?= $this->Form->create(null,array('url' => ['controller' => 'Logins', 'action' => 'login'],'id'=>'contact-form','class'=>'form-signin','inputDefaults'=>array( 'label'=>false ))); ?> And in the form closing use <?= $this->Form->end() ?> Don't write html form tags, as it will create automatically by cake. Hope this will help surely. @Anna Belle
I used form helper to build form now but its still doing nothing. @bikash.bilz
Last thing I can guess is use "<?= $this->Form->submit('Click me')?>;" instead of button type submit. If this not works then, Need to debug, if you feel comfortable share your TV. @Anna Belle
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.