Akhirnya artikelnya bersambung, kali ini saya akan mencoba mempraktekkan cara menyimpan data dari form ke dalam database dengan menggunakan Idiorm dan Slim.
Membuat form
Masing menggunakan lanjutan kode sebelumnya, kita buat sebuah fungsi untuk menampilkan form inputan. Fungsi tersebut adalah sebagai berikut(index.php):
[php]$app->get(‘/input’, function() use($app){
$app->render(‘people/input.tpl’);
});[/php]
kemudian buat file input.tpl pada direktori templates->people, isi kode file tersebut adalah
[php]<!DOCTYPE html>
<html>
<head>
<title>Input Form</title>
</head>
<body>
<form action="/save" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="address">Address</label>
<textarea name="address" id="address" cols="30" rows="10"></textarea>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone">
<label for="email">Email</label>
<input type="email" name="email" id="email">
<input type="submit" name="submit" value="Save">
</form>
</body>
</html>[/php]
Memproses form
Dapat dilihat pada form, action mengarah pada url “/save”, sehingga kita harus membuat fungsi untuk memproses form tersebut. ISi fungsinya adalah sebagai berikut (index.php)
[php]$app->post(‘/save’,function() use($app){
$people = ORM::for_table(‘people’)->create();
$people->name = $app->request->post(‘name’);
$people->address = $app->request->post(‘address’);
$people->email = $app->request->post(’email’);
$people->phone = $app->request->post(‘phone’);
try {
$people->save();
}catch(PDOException $error){
$app->flash(‘info’, $error->getMessage());
$app->redirect(‘/input’);
}
$app->flash(‘info’, ‘Data saved successfully’);
$app->redirect(‘/’);
});[/php]
Pada kode di atas saya menggunakan fungsi save pada idiorm untuk menyimpan data yang dikirim dari form, kemudian saya tambahkan flash message untuk pesan jika data yang diisikan berhasil atau tidak.
Untuk menampilkan flash message, pada index.php tambahkan kode session_start() pada baris paling atas. Kemudian pada template (index.tpl dan input.tpl tambahkan kode berikut :
[php]
<?php if (isset($flash)): ?>
<?php echo $flash[‘info’] ?>
<?php endif ?>
[/php]
Sehingga kode lengkap untuk index.php adalah sebagai berikut
[php]
<?php
session_start();
require ‘vendor/autoload.php’; $app = new \Slim\Slim(); ORM::configure(array( ‘connection_string’ => ‘mysql:host=localhost;dbname=belajarslim’,
‘username’ => ‘root’,
‘password’ => ‘root’
));
$app->get(‘/’, function () use ($app) {
$people = ORM::for_table(‘people’)->find_many();
$app->render(‘people/index.tpl’,array(‘people’=>$people));
});
$app->get(‘/input’, function() use($app){
$app->render(‘people/input.tpl’);
});
$app->post(‘/save’,function() use($app){
$people = ORM::for_table(‘people’)->create();
$people->name = $app->request->post(‘name’);
$people->address = $app->request->post(‘address’);
$people->email = $app->request->post(’email’);
$people->phone = $app->request->post(‘phone’);
try {
$people->save();
}catch(PDOException $error){
$app->flash(‘info’, $error->getMessage());
$app->redirect(‘/input’);
}
$app->flash(‘info’, ‘Data saved successfully’);
$app->redirect(‘/’);
});
$app->run();
[/php]
Sedangkan untuk input.tpl adalah sebagai berikut
[php]<!DOCTYPE html>
<html>
<head>
<title>Input Form</title>
</head>
<body>
<?php if (isset($flash)): ?>
<?php echo $flash[‘info’] ?>
<?php endif ?>
<form action="/save" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="address">Address</label>
<textarea name="address" id="address" cols="30" rows="10"></textarea>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone">
<label for="email">Email</label>
<input type="email" name="email" id="email">
<input type="submit" name="submit" value="Save">
</form>
</body>
</html>[/php]
Semoga bermanfaat