Rabu, 16 Desember 2015
Membuat Form Login
Untuk membuat form login seperti pada gambar di atas ada beberapa cara, berikut akan saya paparkan langkah-langkah nya.
Langkah-langkah :
1. Download dan pasang Code Igniter
2. Buat form dengan cara membuat file baru application/views/news/create.php
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<center>
<form method='post' action='success.php'>
<table width="400" align="center" cellpadding="5" cellspacing="5">
<tr>
<td height="39" colspan="3" bgcolor="#3498db"><div align="center">FORM LOGIN</div></td>
</tr></table>
<table width="400" align="center" cellpadding="5" cellspacing="5">
<tr>
<td height="39" colspan="3" bgcolor="#FA8072"><div align="center">
<label for="title">Nama</label>
<input type="input" name="title" /><br />
<br />
<label for="text">Email</label>
<input type="input" name="text" /><br />
<br />
<label for="password">Password</label>
<input type="password" name="password" /><br />
<br />
<input type="submit" name="submit" value="submit" />
<br />
</center>
</div></td>
</tr></table>
</form>
2. Buka file controller (application/controllers/News.php). Tambahkan code berikut sebelum kurung kurawal terakhir
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = '';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
3. Buat file baru di application/views/news/success.php
<center>
<table width="400" align="center" cellpadding="5" cellspacing="5">
<tr>
<td height="39" colspan="3" bgcolor="#FA8072"><div align="center">
<h2><?php echo "LOGIN sukses..."; ?></h2>
<?php
if(isset($_POST['submit']))
{
$nama = $_POST['title'];
$email = $_POST['text'];
echo "Nama : $nama <br>
Email : $email <br>
<br>";
}
?>
</div></td>
</tr></table>
</center>
4. Buka file application/models/News_model.php . Tambahkan code berikut sebelum kurung kurawal terakhir
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
5. Buka config/routes.php , comment out semua routing rules, lalu tambahkan rules berikut :
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
6. Apabila anda mendapati blank page maka ubah setting base url di application/config/config.php
$config[‘base_url’] = ‘http://localhost/ci/';
7. Buka form di http://localhost/ci/index.php/news/create
Dari langkah-langkah di atas apabila form login tersebut di submit akan muncul tampilan seperti dibawah ini
Langganan:
Postingan (Atom)


