Welcome Guest, Not a member yet? Register   Sign In
form validation, post, get
#1

[eluser]theprodigy[/eluser]
quick question (may lead to others):

Does CI have an issue with accepting both $_GET and $_POST at the same time?

I have a 'contact' controller, who's view contains a contact form. It submits to itself. All works well.

I have a 'news' controller, which uses drill down navigation, and contains a spot where the visitor can comment on an article. The url is:
Code:
domain.tld/news/comments/[article link name]

The form on the news comment page submits to itself (and to a comments($name) ) function. It does not work on that one.

The only difference that I can see is:
1. I load the form_validation library in the function rather then in the constructor
2. This function requires a parameter (essentially a $_GET variable).

I'm a little stumped as to why it doesn't work, unless CI is having issues receiving both $_GET and $_POST at the same time.

Any advice is welcome.

Oh, and I'm using Jamie's MY_Controller and MY_Model libraries (in case this matters)

Contact Controller:
Code:
public function index()
    {
        /*
         * Form Validation Rules in config/form_validation.php
         */
        if ($this->form_validation->run() == TRUE)
        {
            $form['name'] = $this->input->post('name');
            $form['email'] = $this->input->post('email');
            $form['phone'] = $this->input->post('phone');
            $form['subject'] = $this->input->post('subject');
            $form['message'] = $this->input->post('message');
            $form['sender_ip'] = $_SERVER['REMOTE_ADDR'];

            $form['message'] = $form['name'] . ' has filled out the contact form. Here are the details.

Name: ' . $form['name'] . '
Email: ' . $form['email'] . '
Phone: ' . $form['phone'] . '
Subject: ' . $form['subject'] . '
Message:
' . $form['message'] . '
------------END MESSAGE------------';

            if( empty( $form['email'] ) )
            {
                $form['email'] = '*********';
                $form['subject'] = "NO EMAIL - " . $form['subject'];
            }

            $this->load->library('email');

            $this->email->from($form['email'], $form['name']);
            $this->email->to('********');
            //$this->email->cc('[email protected]');
            //$this->email->bcc('[email protected]');

            $this->email->subject("Contact Form Submission: " . $form['subject']);
            $this->email->message($form['message']);

            $this->email->send();

            $this->contact->insert($form);

            $this->view = 'contact/submit';
        }
    }

News Controller:
Code:
public function comments( $name = null )
    {
        if( ! empty( $name ) )
        {
            $this->data['article'] = $this->news->get_by( 'link_name', $name );

            $this->load->library( 'form_validation' );

            if ($this->form_validation->run() == TRUE)
            {
                $insert['name'] = $this->input->post('name');
                $insert['email'] = $this->input->post('email');
                $insert['comment'] = $this->input->post('comment');
                $insert['news_id'] = $this->data['article']->id;

                $this->comments->insert($insert);

                $this->load->library('email');

                if( ! $this->input->post('email') )
                {
                    $this->email->from( '*********', $this->input->post('name') );
                }
                
                $this->email->to('*********');

                $this->email->subject('Comment: ' . $this->data['article']->title);
                $this->email->message( $this->input->post('comment') );

                $this->email->send();

                $this->view = 'news/comment_submitted';                
            }
        }
        else
        {
            redirect( 'news' );
        }
    }
#2

[eluser]Cro_Crx[/eluser]
CodeIgntiers input class destroys all $_GET variables. Info is here http://ellislab.com/codeigniter/user-gui...input.html. You can enable this in the config, although it's better practise and more secure to use the input class to retrieve URI segments with $this->uri->segment(n);
#3

[eluser]theprodigy[/eluser]
well, it may be destroying the global get array, but it's getting to the right controller, and the right function, and passing in the correct value as an argument.

the problem is that it doesn't validate properly, even when all values are filled in and meet the requirements. It always fails validation. But, on the other hand, when I output validation_errors() in my view, it shows nothing. So, it's failing validation, but not reporting any errors.
#4

[eluser]danmontgomery[/eluser]
Where are you setting form validation rules?
#5

[eluser]theprodigy[/eluser]
Form Validation Rules in config/form_validation.php
#6

[eluser]theprodigy[/eluser]
Quote:Where are you setting form validation rules?

I am setting the rules in config/form_validation.php, but that does bring up a quick question. The user docs say

Quote:When a rule group is named identically to a controller class/function it will be used automatically when the run() function is invoked from that class/function

will it still run, if I have the extra uri segment added on? The class/function is named exactly correct, but the url has the url parameter as well.

Does that make a difference?
#7

[eluser]theprodigy[/eluser]
well, I moved the validation rules from config/form_validation.php to the function being called (comments), and it worked as expected. I guess having the extra url segment throws off the array key settings of the form_validation array when you externalize it.

Oh well, at least now it works.




Theme © iAndrew 2016 - Forum software by © MyBB