Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter FormValidator Example doesn't work out of the box
#1

[eluser]futo[/eluser]
Yes, me 2 Smile ..

I'm two weeks new to CI - but not new to a computer. Normally I'm the guy helping other people.

Have tested some code in nginx and now migrated to apache, where I'm starting to implement some forms and there validation. Some form validation code didn't work, and I jumped to the school example from CI to see if that worked: It didn't. I've used two days researching on the en-us part of the internet, and still no solution.

form_validation->run() always fails ...
Trying the form example from the official CI pages where form_validation->run() always fails.
- My base URL in config.php is set with an trailing slash.

The code is the same as on the pages of CI: http://ellislab.com/codeigniter/user-gui...ation.html . I've just implemented the first part inclusive 'Setting Validation Rules'. But with no success (I can't reach formsucces.php).

I've added this snippet to the code of CI - and it does always return '$_POST is NOT set!'.

Code:
if(isset($_POST['submit'])){
   echo '$_POST is set! <br>';
  } else {
   echo '$_POST is NOT set! <br>';
  }


The code from CI with the above implemented:

Code:
&lt;?php
class Form extends CI_Controller {

function index()
{
  $this->load->helper(array('form', 'url'));

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

  if(isset($_POST['submit'])){
   echo '$_POST is set! <br>';
  } else {
   echo '$_POST is NOT set! <br>';
  }
  
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required');

  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('myform');
  }
  else
  {
   $this->load->view('formsuccess');
  }
}
}
?&gt;

Any ideas?...
#2

[eluser]CroNiX[/eluser]
This would have to do with your view and we'd need to see how you're constructing your submit button. My guess is you are not naming it "submit". Try doing a dump of the whole $_POST array and see what does come through in POST, instead of just checking to see if "submit" was in POST.
#3

[eluser]CroNiX[/eluser]
Ah, if you copied the code from the guide:
Code:
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

Then your check would be wrong because the submit button does not have a name="submit" attribute.

The tutorial should work as posted. It's your extra check for $_POST['submit'] that is extra and causing it to not work. If you name the submit button to "submit" your added check should then work.
#4

[eluser]futo[/eluser]
Thank you very much for your quick response CroNIX! :-D Lovely to hear a helpful voice! :-D

[quote author="CroNiX" date="1363976437"]Ah, if you copied the code from the guide:
Code:
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

Then your check would be wrong because the submit button does not have a name="submit" attribute.

The tutorial should work as posted. It's your extra check for $_POST['submit'] that is extra and causing it to not work. If you name the submit button to "submit" your added check should then work.[/quote]

With the hammer on the nail. Thanks! :-D Now I'm able to see that $POST is set. Wonderful! :-)

Now I'm just searching for a hint why I never come to the 'formsuccess.php'?...

I have the actual problem that $_POST is empty ...

My corrected controller 'form.php':
Code:
&lt;?php class Form extends CI_Controller {

function index()
{
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');

  if(isset($_POST)){
   echo '<br> $_POST is set! <br>';
   echo count($_POST) . '<br>';
  
   echo 'var_dump($_POST)--&gt;>';
   var_dump($_POST);
   echo '<<--var_dump($_POST) <br><br>';
  
  } else {
   echo '$_POST is NOT set! <br>';
  }
  
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required');

  echo 'var_dump($this->form_validation->run())--&gt;>';
  var_dump($this->form_validation->run());
  echo '<<--var_dump <br>';

  if ( $this->form_validation->run() == FALSE )
  {
   $this->load->view('myform');
  }
  else
  {
   $this->load->view('formsuccess');
  }
}
} ?&gt;

And here is the view 'myform.php' without any changes:

Code:
&lt;html&gt; &lt;head&gt; &lt;title&gt;My Form&lt;/title&gt; &lt;/head&gt; &lt;body&gt;

&lt;?php echo validation_errors(); ?&gt;
&lt;?php echo form_open('form'); ?&gt;

<h5>Username</h5>
&lt;input type="text" name="username" value="" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt; &lt;/body&gt; &lt;/html&gt;

I haven't changed anything in 'formsuccess.php' as it doesn't have any influence in the above.

Notice the following lines in 'form.php':
Code:
if(isset($_POST)){
   echo '<br> $_POST is set! <br>';
   echo count($_POST) . '<br>';
  
   echo 'var_dump($_POST)--&gt;>';
   var_dump($_POST);
   echo '<<--var_dump($_POST) <br><br>';
  
  } else {
   echo '$_POST is NOT set! <br>';
  }

- They return this:

Code:
$_POST is set!
0
var_dump($_POST)--&gt;>array(0) { } <<--var_dump($_POST)


... even thou I write something in every input field of the form.

So, it I'm not surprised that 'form_validation->run()' in the CI-library 'Form_validation.php' returns FALSE. This is meant so by the programmers, as they have these lines in 'Form_validation.php':
Code:
if (count($_POST) == 0)
  {
   return FALSE;
  }

The questions is why am I getting an empty $_POST ?... I do write something in every input field ... And every input field has a 'form_validation->set_rules' .... and these rules only demands 'required' and nothing more ...

I would be delighted for further suggestions :-D ...

EDIT-01:
Just saw that the topic of $_POST being empty has ruled before:
https://www.google.com/search?client=ubu...niter+from+$_POST+empty&ie=utf-8&oe=utf-8 ... I'm investigating this right now ...

EDIT-02:
As some in other threads suggest it to have with '.htaccess' to do i post this file her:
Code:
<Files .htaccess>
order allow,deny
deny from all
</Files>

DirectoryIndex index.php

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Canonicalize codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce www
    # If you have subdomains, you can add them to
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

    ###

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

Please share your thoughts to that - and maybe your '.htaccess' ...
#5

[eluser]CroNiX[/eluser]
See if you are receiving ANYTHING via post. I'd do that in index.php at the very top before CI is actually loaded. Then submit your form and see if $_POST is populated.

You might try testing some of the various settings for
$config['uri_protocol'] in /application/config/config.php

Another thing you might try is sticking it in a different method other than 'index' in your formcontroller. And then you'd also need to change your form open tag to point to the new method as it is currently only pointing to the classname (which should call index method), so it'd be form_open('form/your_method');

Are you using an htaccess to remove index.php from the url?
#6

[eluser]CroNiX[/eluser]
Try using only this for your htaccess:
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#7

[eluser]futo[/eluser]
Started with changing my '.htaccess' to what CroNiX suggested. - No difference.

With $config['uri_protocol'] = 'PATH_INFO'; I'm thrown back to '/index.php' when I try to access the controller 'form.php' through 192.168.2.115/form ... the rewrite rules from above acts in this.

Setting my '.htaccess' back to what was before. Still nothing useful - just a redirection to the main '/index.php' page in the root (/var/www/index.php - where /var/www/ is the root).

Setting my '.htaccess' to what CroNiX suggested again.

Changing to $config['uri_protocol'] = 'QUERY_STRING';. Shows no difference to 'PATH_INFO'.

Changing to $config['uri_protocol'] = 'ORIG_PATH_INFO';. Shows no difference to 'PATH_INFO'.

Changing back to original value: $config['uri_protocol'] = 'REQUEST_URI'; Now the 'form.php' is shown as expected - and not the main '/index.php' - when i write 192.168.2.115/form in the browser.

So this definitive rules out $config['uri_protocol'] under the given circumstance.

In the beginning af the main '/index.php' file in the root (/var/www/) I set this code:

Code:
if(isset($_POST)){
   echo '<br> index - $_POST is set! <br>';
   echo count($_POST) . '<br>';
  
   echo 'index - var_dump($_POST)--&gt;>';
   var_dump($_POST);
   echo '<<--var_dump($_POST) <br><br>';
  
  } else {
   echo '$_POST is NOT set! <br>';
  }

This returns the following:

Code:
index - $_POST is set!
0
index - var_dump($_POST)--&gt;>array(0) { } <<--var_dump($_POST)

Changing the name of the method inside 'form.php' from 'index' to 'yobro' and calling the class and the method in the 'myform.php' with 'form_open('form/yobro')'. The form shows up in the browser as before - and still no values inside $_POST. Also no values inside $_POST by the above code in the main '/index.php'.

Yes, I would say that I'm using an htaccess to remove index.php from the url. But looking through my original '.htacces', I'm uncertain now where ...
#8

[eluser]futo[/eluser]
DONE! :-D ...

This did it:

Code:
sudo a2enmod rewrite
sudo service apache2 restart
(written in the terminal - in each its own line.)

Source: http://stackoverflow.com/questions/52005...able-empty
Quote: All you had to do was enable apache's mod rewrite.

Thought this was enabled by standard from Apache ... It wasn't.
#9

[eluser]futo[/eluser]
Thank you very much for standing by and giving advice CroNiX! :-D Have a nice weekend+! :-D




Theme © iAndrew 2016 - Forum software by © MyBB