Codimentary form validation form ...

I create an input form on a specific post page, for example. (Property / rent / rec_id). Upon presentation, data should be inserted, verified and returned to the original page from which it was sent with any relevant messages.

  • If the validation fails, reload the form page to re-fill the form
  • Echo a specific validation error. Invalid email address etc.

  • If successful, insert the data, reload the page and display a success message.

    function class () {

    $this->load->library('form_validation');    
    $this->form_validation->set_rules('name','name','required|trim');
    $this->form_validation->set_rules('email','email','required|trim|valid_email');
    
    $fields['name'] = 'Name';
    $fields['email'] = 'Email Address';
    
    $this->validation->set_fields($fields);
    
    if ($this->form_validation->run() == FALSE) 
    {
        //failed - reload page and re-populate fields with any error messages
    }
    else
    {
         // display form with success message
    }
    

    }

" " , . "" . , .?

,

+3
5

CodeIgniter , "FlashData".

+3

set_value('field_name') . :

<input type="text name="name" value="<?php echo set_value('name'); ?>" />"

form_validation, validation_errors(). :

<?php echo validation_errors(); ?>

. .

+7
   $insertData=array(
            'name'=>NULL,
            'email'=>NULL);
 //setting validation rules
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|email');

//here you will get all data which user entered in form 
$insertData = $this->input->post(NULL, TRUE);

if ($this->form_validation->run() == FALSE) 
{
    //failed - reload page and re-populate fields with any error messages
    $this->load->view('view_name',$insertData)    

}
else
{
     //set flash data for displaying success message in redirected page
     $this->session->set_flashdata('success','Success message')
     redirect('controller_name');

}

<?php if ($this->session->flashdata('succes_message')) { echo    $this->session->flashdata('succes_message'); ?> } ?>

<form action='action page url' method='post'>
<input type='text' name='name' value='<?php echo $name ?>'/>
<input type='text' name='email' value='<?php echo $email ?>'/>
<input type='submit' value='Save'/>
+1

/ . cookie, ( cookie HTTP-).

- , , .

POST/GET SESSION, , .

session_start();
$_SESSION['POST_DATA'] = $_POST;
0
source

To alleviate the problem, you can simply do client-side validation using Javascript / jQuery. So basically, instead of going directly to a new page, the β€œSubmit” action will simply be redirected to your javascript function, which will decide if the form is valid. Then you can use jQuery to make everything look beautiful for invalid elements.

-4
source

Source: https://habr.com/ru/post/1712481/


All Articles