Welcome Guest, Not a member yet? Register   Sign In
Form Validation display errors
#1

[eluser]learning_php[/eluser]
Hi,

I have been trying to mt form validation error to display in it own div tag but when the form in reloaded the error is underneath the input box. I added a basic stylesheet and none of that is on the reloaded page either.

Any help would be most appreciated

Controller
Code:
<?php
class test extends Controller
{

    function test()
    {
        parent::Controller();
    }
    function index()
    {
        $data['page_title'] = "Add Contact";
        

        $this->load->view('test_view', $data);

        
    }
    function testadd()
    {
        $data['page_title'] = "Add Contact";
         $this->form_validation->set_rules('CompanyName', 'CompanyName','required|xss_clean');

        if ($this->form_validation->run() == false) {
            $this->load->view('test_view',$data);
        } else {
            $this->db->insert('contacts', $_POST);
            redirect('contacts', 'contacts');
        }
    }

}

View
Code:
<html>
<head>
<title><?= $page_title ?></title>
<link rel="stylesheet" type="text/css" href="./stylesheet.css" />
</head>
<body>
    <div>
    <ul>
    <li>&lt;?= anchor('', 'Homepage'); ?&gt;</li>
    </ul>
    </div>
&lt;?= form_open('test/testadd') ?&gt;
        <div id="contacts_form">
            <div id="Contacts_companyName">
    Company Name: &lt;input type="text" name="CompanyName" id="CompanyName" AUTOCOMPLETE = "off"
value="&lt;?php echo set_value('CompanyName'); ?&gt;" /&gt;
             </div>
             <div id="Contacts_companyName_error">&lt;?php echo form_error('CompanyName'); ?&gt;</div>
        
        &lt;input type="submit" value="Add Contact"  /&gt;
        &lt;?= form_close() ?&gt;
        </div>

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

Model
Code:
php
class contacts_model extends Model
{

    function contacts_model()
    {
        parent::model();
    }

    function getData()
    {
        $query = $this->db->get('contacts');

        if ($query->num_rows() < 0) {
            //show_error('databse is empty')

        } else {
            return $query->result();
        }
    }
}
#2

[eluser]Aken[/eluser]
Without additional styling, your error's <div> will automatically show up underneath the input. If your CSS isn't showing up, it's likely because it has a wrong relative URL. Try using either an absolute URL with a beginning slash, which will start from your root directory, or CI's URL helper with the site_url() function.

Code:
// Absolute URL starting from root.
&lt;link rel="stylesheet" type="text/css" href="/css/stylesheet.css" /&gt;

// Full URL, IE http://www.example.com/css/stylesheet.css
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo site_url('css/stylesheet.css'); ?&gt;" /&gt;

I personally like the latter as if I need to move servers or anything, it makes portability easy.




Theme © iAndrew 2016 - Forum software by © MyBB