Welcome Guest, Not a member yet? Register   Sign In
Check if form has been submitted?
#1

[eluser]RS71[/eluser]
Hey

Is there a general way to check that the form has been submitted (or basically that validation has failed?) I don't really want the controller/model to run the queries again in case of validation failure (since I'd be using the POST values and not the DB ones there is no need to run the queries)

Another need of mine that depends on this check is that I have dynamically created fields via JavaScript. Initially I'd like to create as many field as I have in the DB but in case of validation error, I'd need to create only as many as there are in the POST (since the user could have decided to remove a field)

I need a check sorta like how the validation does it. How when you initially display a form it doesn't display the errors/checks for errors.

Thanks in advance.
#2

[eluser]Rey Philip Regis[/eluser]
try to read this http://ellislab.com/codeigniter/user-gui...input.html
#3

[eluser]RS71[/eluser]
Thank you for your help but I think I had already found myself an answer within the code for the form validation class.

Haven't put it to use yet but I would be doing the following route:

Code:
if (count($_POST[]) > 0) {

}
#4

[eluser]Colin Williams[/eluser]
Code:
if ( ! $this->validation->run() && $this->validation->error_string == '')
{
   // Form was not submitted
}

if ( ! $this->validation->run() && $this->validation->error_string != '')
{
   // Form was submitted and there were errors
}

if ($this->validation->run())
{
   // Form was submitted and there were no errors
}

Probably slightly different with Form_validation in 1.7.0, but I've yet to play around with that much.

(Note, you wouldn't necessarily set up your code like that, I just wanted to show all possible conditions individually.)
#5

[eluser]RS71[/eluser]
Thank you for your reply Colin.

I think its

Code:
$this->form_validation->run()

instead of

Code:
$this->validation->run()

I tried checking if the form was submitted but it didn't exactly work. Not sure if this has any relevancy but I'm currently returning errors one by one next to each field, would I still check for error_string?
#6

[eluser]RS71[/eluser]
Code:
if (count($_POST) == 0)
        {
            $data['ttt'] = 'default';
        }

The above seems to be working but I should probably use your method no? (although it doesn't seem to be working atm)
#7

[eluser]Ch!ps[/eluser]
simple way to check if a form was submitted:

if(array_key_exists('submit',$_POST))
{
// form submitted, where 'submit' is the name of submit button
}
#8

[eluser]RS71[/eluser]
Wouldn't count() be more foolproof?
#9

[eluser]built2fall[/eluser]
Here's two other methods to determine if a form has been submitted:

Code:
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
// do something
}

OR

if ($this->input->post('hidden_form_field'))
{
// do something
}

Checking for a existence of a hidden form in POST, this method, and counting the size of the POST array are probably the most foolproof methods. I didn't verify this but going by what I've read, IE allows for form submissions by hitting enter without focus on the submit button, so in that case checking for $_POST['submit-btn'] would fail. Also if you enabled GET, checking the REQUEST_METHOD wouldn't work in the case of someone not submitting the form and just typing the URL with the query string.
#10

[eluser]Bob Stein[/eluser]
Thanks for the information in this thread. It helped me with something yesterday.

But it seems an even simpler piece of code will work than any that have been suggested here, although the suggestions in this thread also work. Try this:

Code:
if ($_POST)
{
// do something
}

Pretty self-explanatory, I think. Simple but effective.




Theme © iAndrew 2016 - Forum software by © MyBB