Search code examples
codeigniteremailform-submitsubmit-buttongetmethod

CodeIgniter getMethod() Deprecated and Submit Button not functioning


I practice my CodeIgnitor right now and I'm new to them still. I am doing the inquiry form and wanted to make sure that it checks the form is correctly verified. After clicking on the submit button, It will send the form to a corresponding email.

but I'm having issues saying getMethod() is deprecated and my submit button is not responding too.

In fact, I don't grasp what deprecated means are, and are there anybody who can assist me clarify this part and provides any other approach before using getMethod().

Can you guys check what I did wrong with the submit button too? If you have a better approach to validate the form and send the email to the corresponding that somewhat cleaner. It's also wonderful.

This is my code:-

config/Controller/Contact Controller.php

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

class ContactController extends Controller
{
    // CONTACT PAGE
    public function contact()
    {
        //Library
        helper(['form']);

        //Contact Data Initiate
        $contact_data = [];
        $data = [
            'meta_title' => 'Contact | MFD',
        ];
        
        //Check request if it is post
        if($this->request->getMethod() == 'post'){

            // Setting Form Rules
            $settings_rule = [
                'email' => [
                    'name' => 'required|min_length[3]|max_length[20]',
                    'email' => 'required|valid_email',
                    'subject' => 'required',
                ],
                'msg' => 'required',
            ];

            if($this->validate($settings_rule)){
                //Validation true send message successfully sent and return back to the same page
                return view('page_templates/contact', $data);

            }else {
                // Will provide check list of error of the one we create 
                $contact_data['validation'] = $this->validator;
            }
        }

        return view('page_templates/contact', $data);
    }

views/page_templates/contact.php

               <div class="col-lg-8 mt-5 mt-lg-0">

                    <?php if(isset($validation)) : ?>
                        <div class="error-message">
                            <?= $validation->listErrors(); ?>
                        </div>
                    <?php endif ?>

                    <form method="post" role="form" class="php-email-form">
                        <div class="row">
                            <div class="col-md-6 form-group">
                                <input type="text" name="name" class="form-control" id="name" 
                                    value="<?= set_value('name'); ?>" placeholder="Your Name">       
                            </div>
                            <div class="col-md-6 form-group mt-3 mt-md-0">
                                <input type="email" class="form-control" name="email" id="email" 
                                    value="<?= set_value('email'); ?>" placeholder="Your Email">
                            </div>
                        </div>
                        <div class="form-group mt-3">
                            <input type="text" class="form-control" name="subject" id="subject" 
                                value="<?= set_value('subject'); ?>" placeholder="Subject">
                        </div>
                        <div class="form-group mt-3">
                            <textarea class="form-control" name="message" rows="5" placeholder="Message"><?= set_value('msg'); ?></textarea>
                        </div>
                        <!-- <div class="my-3">
                            <div class="loading">Loading</div>
                            <div class="error-message"></div>
                            <div class="sent-message">Your message has been sent. Thank you!</div>
                        </div> -->

                        <div style="height: 10px;"></div>
                        <div class="text-left button">
                            <button type="submit" name="submit">Send Message</button>
                        </div>
                    </form>

                </div>

Really appreciate it if anyone that can help me with this. Thank you


Solution

  • What is the exact deprecation message that you are getting? And which version of CodeIgniter are you using?

    Deprecation just means that the interface will be removed in future revisions.

    But I just looked at the source and documentation and the method doesn't appear to be deprecated. The optional parameter $upper is deprecated though.

    So you would not want to use $this->request->getMethod(TRUE | FALSE) since that wont be supported in the future. But $this->request->getMethod() should be fine.

    As for your button problem... You didn't provide enough information.

    For the client rendered button to respond to a click event you will need to add a listener. I am guessing you have not. It goes something like this.

    <script>
       let button = document.querySelector(".button button");
       button.addEventListener("click", function(event) {
           // Do your work here...
       });
    </script>