Create a form to remember previously submitted values ​​with CodeIgniter

I have this contact form with codeigniter, and what I want to do when the form is submitted but does not pass the verification, I want the fields to contain the previously submitted values.

There is one thing: when the form loads, all the fields already have a certain value, therefore, for example, the "name field" shows the "name" inside the field. And I want this to remain so if the "name" is not changed and the form is not submitted, in which case it should have a new meaning.

So, at the moment I have this:

<?php echo form_input('name', 'Name*');?>

<?php echo form_input('email', 'Email*');?>

But I don’t know how to create a form to remember any new values ​​presented.

Any idea?

+3
3

CodeIgniter set_value.

<?php echo form_input('name', set_value('name', 'Name*')); ?>
+6

, $_POST ( $_GET , ).

// use $_POST['name'] if set, else use 'Name*'
<?= form_input('name', (!empty($_POST['name']) ? $_POST['name'] : 'Name*'); ?>
+1

I think the answer lies with the controller.

Personally, I started letting the form controller function handle the validation:

<?php

class Page extends Controller
{

    ...

    function showform()
    {
      $this->load->helper(array('form', 'url'));
      $data = array("name" => "Name*", "email" => "Email*");
      $failure = false;

      if( $this->input->post("name") )
      {
        $data["name"] = $this->input->post("name");
        $data["email"] = $this->input->post("email");

        if( !valid_email($data["email"]) )
        {
            $failure = true;
            $data["error_message"] = "E-mail couldn't validate";
        }

        if( !$failure )
             redirect('/page/thankyou/', 'refresh');
      }



        $this->load->vars($data);
        $this->load->view("theform");   

    }

    ...

}

And, in your opinion, you would do something like this:

<?php echo form_input('name', $name);?>
<?php echo form_input('email', $email);?>
+1
source

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


All Articles