Welcome Guest, Not a member yet? Register   Sign In
Better than if(count($_POST) > 0)
#21

[eluser]Mauricio de Abreu Antunes[/eluser]
Why are you checking this?
#22

[eluser]Matalina[/eluser]
Me personally I check to see if a post was made to cut down on repetitive code.

Instead of having a controller method to display the form and then one to process it I do them in the same controller method.

So I process any info I need to to make the form. Check to see if the form was submitted. If it was submitted I validate the data, and if it was successful I do whatever calls I need to do like storing information in the database, retreive data etc. If the info validated then I redirect to another page. If the form didn't validate I process the form to be displayed.

for example:
Code:
public function cust_search() {
    $data = array();
    $this->load->model('Customer_model');
    if($this->input->post('cust_search')) {
      $this->form_validation->set_rules('first', 'First Name', 'min_length[2]|alpha');
      $this->form_validation->set_rules('last','Last Name','min_length[2]|alpha');

      $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

      if ($this->form_validation->run()) {
        $first = $this->input->post('first');
        $last = $this->input->post('last');

        if($first OR $last) {
          $this->load->model('Customer_model');
          $data['customer'] = $this->Customer_model->cust_search($first,$last);
        }
        else {
          $data['error'] = '<div class="error">You must have a partial First or Last Name</div>';
        }
      }
    }

    $this->load->view('reports/cust_search',$data);
  }
#23

[eluser]sarcastron[/eluser]
I agree with Matalina. In most cases, it makes more sense to submit the data from a form to the same page the form is on. Seems like less code to worry about.
#24

[eluser]xerobytez[/eluser]
This is what I always to check for POST requests.

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

}
#25

[eluser]CroNiX[/eluser]
[quote author="Matalina" date="1330714451"][quote author="johnpeace" date="1330708813"][quote author="veledrom" date="1330708084"]
This way doesn't work.
Code:
if(! $this->input->post('submit_name')) {
// do something
}
[/quote]

This does though:
Code:
if($this->input->post('submit_button') != 'Login') {
// do something
}
[/quote]

I do it all the time The above quoted code is wrong tho there is no !
Code:
if($this->input->post('submit_name')) {
// do something
}

if $this->input->post('submit_name') has something in it it will return TRUE because it's not empty if('hi') is always true.
[/quote]

This is not exactly true. If there is not a post value (like a checkbox that isn't checked doesn't get sent in POST), input::post('field_name') returns boolean FALSE. If it does exist, it will return the value of that field (not TRUE as stated above). It's best to use the === operator because if the value of the field is an actual 0, then if($this->input->post('field_name')) will evaluate to FALSE even though the field value was sent and is a zero.

I usually use
Code:
$submitted = $this->input->post('name_of_submit_button');
if ($submitted !== FALSE)
{
  // Form was submitted, set up validation rules and run validation
  // Inefficient setting up and running validation if form was not submitted.
}
else
{
  // Form was not submitted
}
#26

[eluser]Matalina[/eluser]
Typically you check to see if the button was pushed ... submit buttons have names for that reason. It should never be null or false or 0. or empty.
#27

[eluser]CroNiX[/eluser]
Yes, but that doesn't really have to do with my response to the inaccurate info I was pointing out (that a input::post('field') will return TRUE if it exists instead of it's value, hence it's best to use the === operator when checking inputs).

AFAIK a submit with no name will still submit. It's name and value just won't be transmitted in the POST array, but the rest of the "successful form controls" will.
#28

[eluser]toopay[/eluser]
[quote author="veledrom" date="1330686793"]Hi,

I want to check if POST occurred or not. Currently I'm using code below. I wonder if there is a better way of doing it with CI?

Thanks

Code:
do_save()
{
   if(count($_POST) > 0)
   {
      //Validate fields with form validator
   }
   else
   {
      redirect('index');
   }
}
[/quote]

form_validation class already including the POST inspection within its procedure, so this is more than enough :
Code:
function do_save()
{
   // Set validation rule
   // ...

   if($this->form_validation->run())
   {
      // save
   }
   else
   {
      redirect('index');
   }
}
#29

[eluser]veledrom[/eluser]
@toopay - No need to load form_validation and carry out all those validation rules if POST doesn't occur properly.

@all others - I shall just go ahead use this code then. I relay on you!

Code:
if ($this->input->post('name_of_submit_button') === FALSE)
{
  //FAILURE
}




Theme © iAndrew 2016 - Forum software by © MyBB