seperti biasa klik kana pada folder kemudian new dan php file dengan nama "save-form"
---> <?php
session_start();
if($_POST)
{
$nama = $_POST['nama'];
$email = $_POST['email'];
$alamat = $_POST['alamat'];
$kota = $_POST['kota'];
$kodePos = $_POST['kode_pos'];
$captcha = $_POST['pin'];
$hari = $_POST['hari'];
$bulan = $_POST['bulan'];
$tahun = $_POST['tahun'];
$error = array();
$error_c = array();
$namaPattern = '/[^a-zA-Z ]/';
if(empty($nama))
{
$error['nama'] = 'Nama tidak boleh kosong';
}
elseif(preg_match($namaPattern, $nama)){
$error['nama']='harus huruf';
}
session_start();
if($_POST)
{
$nama = $_POST['nama'];
$email = $_POST['email'];
$alamat = $_POST['alamat'];
$kota = $_POST['kota'];
$kodePos = $_POST['kode_pos'];
$captcha = $_POST['pin'];
$hari = $_POST['hari'];
$bulan = $_POST['bulan'];
$tahun = $_POST['tahun'];
$error = array();
$error_c = array();
$namaPattern = '/[^a-zA-Z ]/';
if(empty($nama))
{
$error['nama'] = 'Nama tidak boleh kosong';
}
elseif(preg_match($namaPattern, $nama)){
$error['nama']='harus huruf';
}
//reguler expression (regex)
$kposPattern = '/^[0-9]{5}$/';
$emailPattern = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/';
if(empty($email))
{
$error['email'] = 'Email tidak boleh kosong';
}
elseif(!preg_match($emailPattern, $email)){
$error['email'] = 'Email penulisan salah (shasaelek@gmail.com)';
}
if(empty($alamat))
{
$error['alamat'] = 'Alamat tidak boleh kosong';
}
if(empty($kota))
{
$error['kota'] = 'Kota tidak boleh kosong';
}
if($hari == 'hari'){
$error['hari'] = $hari;
}
if($bulan == 'bulan'){
$error['bulan'] = $bulan;
}
if($tahun == 'tahun'){
$error['tahun'] = $tahun;
}
if(empty($kodePos))
{
$error['kode_pos'] = 'Kode Pos tidak boleh kosong';
}
elseif(!is_numeric($kodePos))
{
$error['kode_pos'] = 'Kode Pos harus berisi angka';
}
elseif(!preg_match($kposPattern, $kodePos)){
$error['kode_pos']='kode pos harus 5 angka';
}
if(empty($captcha))
{
$error['pin'] = 'Inputan Harus Diisi';
}
else if(md5($captcha) != $_SESSION['image_random_value'])
{
$error['pin'] = '<p>Are you human being?</p>';
}
else if(md5($captcha) == $_SESSION['image_random_value'])
{
$error_c['pin'] = '<p>You are people</p>';
}
if(empty($error))
{
echo "Successful";
}
else
{
$_SESSION['error'] = $error;
$_SESSION['post'] = $_POST;
header("location: contact-form.php");
}
}
?>
then new php file again "contact-form"
-->
<?php
session_start();
if(isset($_SESSION['error']))
{
$error = $_SESSION['error'];
$_POST = $_SESSION['post'];
unset($_SESSION['error']);
unset($_SESSION['post']);
}
?>
<form name="form-name" method="post" action="save-form.php">
<dl>
<dt>Nama:</dt>
<dd>
<!-- Jika $_POST['nama'] ada isinya, maka tampilkan, jika tidak gak usah ditampilkan -->
<input name="nama" id="nama" type="text" value="<?php echo isset($_POST['nama']) ? $_POST['nama'] : '';?>" />
<!-- Jika $error['nama'] ada isinya (jika error), maka tampilkan, jika tidak gak usah ditampilkan -->
<div style="color:red"><?php echo isset($error['nama']) ? $error['nama'] : '';?></div>
</dd>
<dt>Email:</dt>
<dd>
<input name="email" id="email" type="text" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '';?>" />
<div style="color:red"><?php echo isset($error['email']) ? $error['email'] : '';?></div>
</dd>
<dt>Alamat:</dt>
<dd>
<input name="alamat" id="alamat" type="text" value="<?php echo isset($_POST['alamat']) ? $_POST['alamat'] : '';?>" />
<div style="color:red"><?php echo isset($error['alamat']) ? $error['alamat'] : '';?></div>
</dd>
<dt>Kota:</dt>
<dd>
<input name="kota" id="kota" type="text" value="<?php echo isset($_POST['kota']) ? $_POST['kota'] : '';?>" />
<div style="color:red"><?php echo isset($error['kota']) ? $error['kota'] : '';?></div>
</dd>
<dt>Kode POS:</dt>
<dd>
<input name="kode_pos" id="kode_pos" type="text" value="<?php echo isset($_POST['kode_pos']) ? $_POST['kode_pos'] : '';?>" />
<div style="color:red"><?php echo isset($error['kode_pos']) ? $error['kode_pos'] : '';?></div>
</dd>
<dt>Tgl Lahir</dt>
<dd>
<?php
//membuat combo box hari
echo "<select name='hari'>
<option value=hari>hari</option>";
$hari = 1;
$haripilih = isset($_POST['hari']) ? $_POST['hari'] : "hari";
while($hari <=31)
{
echo "<option value=$hari";
echo $hari == $haripilih ? " selected" : "";
echo '>' .$hari.'</option>';
$hari++;
}
echo "</select>";
//membuat array bulan
$bulan=array("Januari" ,"Februari", " Maret" ,"April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
//membuat combo box bulan
echo "<select name='bulan'>
<option value=bulan>bulan</option>";
$bulanpilih = isset($_POST['bulan']) ? $_POST['bulan']: "bulan";
foreach($bulan as $bulan)
{
echo "<option value=$bulan";
echo $bulan == $bulanpilih ? " selected" : "";
echo ">". $bulan. "</option>";
}
echo "</select>";
//membuat combo box tahun
echo "<select name='tahun'>
<option value=tahun>tahun</option>";
$tahun = 2012;
$tahunpilih = isset($_POST['tahun']) ? $_POST['tahun'] : "tahun";
while($tahun >=1990)
{
echo "<option value=$tahun";
echo $tahun == $tahunpilih ? " selected" : "";
echo '>' .$tahun.'</option>';
$tahun--;
}
echo "</select>";
?>
<div style="color:red">
<?php echo isset($error['hari']) ? $error['hari'] : '';?>
<?php echo isset($error['bulan']) ? $error['bulan'] : '';?>
<?php echo isset($error['tahun']) ? ' ' . $error['tahun'] : '';?>
<?php echo isset($error['hari']) || isset($error['bulan'])
|| isset($error['tahun']) ? 'belum dipilih':'';?>
</div>
</dd>
<dt></dt>
<dt>
Inputkan Text Di Samping :
<img src=captcha.php?date=".date('YmdHis')." alt=security_image />
</dt>
<dd>
<div>
<input type=text name=pin value="<?php echo isset($_POST['pin']) ? $_POST['pin'] : '';?>" />
</div>
<div style=color:red><?php echo isset($error['pin']) ? $error['pin'] : '';?></div>
<div style=color:red><?php echo isset($error_c['pin']) ? $error_c['pin'] : '';?></div>
</dd>
<dd>
<br><input name="submit" id="submit" type="Submit" value="Submit" />
</dd>
</dl>
</form>
<?php
session_start();
if(isset($_SESSION['error']))
{
$error = $_SESSION['error'];
$_POST = $_SESSION['post'];
unset($_SESSION['error']);
unset($_SESSION['post']);
}
?>
<form name="form-name" method="post" action="save-form.php">
<dl>
<dt>Nama:</dt>
<dd>
<!-- Jika $_POST['nama'] ada isinya, maka tampilkan, jika tidak gak usah ditampilkan -->
<input name="nama" id="nama" type="text" value="<?php echo isset($_POST['nama']) ? $_POST['nama'] : '';?>" />
<!-- Jika $error['nama'] ada isinya (jika error), maka tampilkan, jika tidak gak usah ditampilkan -->
<div style="color:red"><?php echo isset($error['nama']) ? $error['nama'] : '';?></div>
</dd>
<dt>Email:</dt>
<dd>
<input name="email" id="email" type="text" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '';?>" />
<div style="color:red"><?php echo isset($error['email']) ? $error['email'] : '';?></div>
</dd>
<dt>Alamat:</dt>
<dd>
<input name="alamat" id="alamat" type="text" value="<?php echo isset($_POST['alamat']) ? $_POST['alamat'] : '';?>" />
<div style="color:red"><?php echo isset($error['alamat']) ? $error['alamat'] : '';?></div>
</dd>
<dt>Kota:</dt>
<dd>
<input name="kota" id="kota" type="text" value="<?php echo isset($_POST['kota']) ? $_POST['kota'] : '';?>" />
<div style="color:red"><?php echo isset($error['kota']) ? $error['kota'] : '';?></div>
</dd>
<dt>Kode POS:</dt>
<dd>
<input name="kode_pos" id="kode_pos" type="text" value="<?php echo isset($_POST['kode_pos']) ? $_POST['kode_pos'] : '';?>" />
<div style="color:red"><?php echo isset($error['kode_pos']) ? $error['kode_pos'] : '';?></div>
</dd>
<dt>Tgl Lahir</dt>
<dd>
<?php
//membuat combo box hari
echo "<select name='hari'>
<option value=hari>hari</option>";
$hari = 1;
$haripilih = isset($_POST['hari']) ? $_POST['hari'] : "hari";
while($hari <=31)
{
echo "<option value=$hari";
echo $hari == $haripilih ? " selected" : "";
echo '>' .$hari.'</option>';
$hari++;
}
echo "</select>";
//membuat array bulan
$bulan=array("Januari" ,"Februari", " Maret" ,"April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
//membuat combo box bulan
echo "<select name='bulan'>
<option value=bulan>bulan</option>";
$bulanpilih = isset($_POST['bulan']) ? $_POST['bulan']: "bulan";
foreach($bulan as $bulan)
{
echo "<option value=$bulan";
echo $bulan == $bulanpilih ? " selected" : "";
echo ">". $bulan. "</option>";
}
echo "</select>";
//membuat combo box tahun
echo "<select name='tahun'>
<option value=tahun>tahun</option>";
$tahun = 2012;
$tahunpilih = isset($_POST['tahun']) ? $_POST['tahun'] : "tahun";
while($tahun >=1990)
{
echo "<option value=$tahun";
echo $tahun == $tahunpilih ? " selected" : "";
echo '>' .$tahun.'</option>';
$tahun--;
}
echo "</select>";
?>
<div style="color:red">
<?php echo isset($error['hari']) ? $error['hari'] : '';?>
<?php echo isset($error['bulan']) ? $error['bulan'] : '';?>
<?php echo isset($error['tahun']) ? ' ' . $error['tahun'] : '';?>
<?php echo isset($error['hari']) || isset($error['bulan'])
|| isset($error['tahun']) ? 'belum dipilih':'';?>
</div>
</dd>
<dt></dt>
<dt>
Inputkan Text Di Samping :
<img src=captcha.php?date=".date('YmdHis')." alt=security_image />
</dt>
<dd>
<div>
<input type=text name=pin value="<?php echo isset($_POST['pin']) ? $_POST['pin'] : '';?>" />
</div>
<div style=color:red><?php echo isset($error['pin']) ? $error['pin'] : '';?></div>
<div style=color:red><?php echo isset($error_c['pin']) ? $error_c['pin'] : '';?></div>
</dd>
<dd>
<br><input name="submit" id="submit" type="Submit" value="Submit" />
</dd>
</dl>
</form>
new php file again "captcha"-->
<?php
session_start();
$alphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$random = substr(str_shuffle($alphaNumeric), 0, 5);
$image = imagecreatefromjpeg("background.jpg");
$textColor = imagecolorallocate($image, 0, 0, 0); // black
imagestring($image, 5, 5, 8, $random, $textColor);
$_SESSION['image_random_value'] = md5($random);
header("Expires: Mon, 36 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no cache");
header('Content-tyor: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
</body>
</html>
>> implement-captcha
<?php
session_start();
if($_POST['submit']) {
if (md5($_POST['pin']) == $_SESSION['image_random_value']) {
echo '<p>You are people</p>';
}else{
echo '<p>Are you human being?</p>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form name="form1" method="post" action="">
<img src="captcha.php?date=<?php echo date('YmdHis');?>" alt="security image" />
<div>
input Text Above: <input type="text" name="pin" />
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
?>
session_start();
$alphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$random = substr(str_shuffle($alphaNumeric), 0, 5);
$image = imagecreatefromjpeg("background.jpg");
$textColor = imagecolorallocate($image, 0, 0, 0); // black
imagestring($image, 5, 5, 8, $random, $textColor);
$_SESSION['image_random_value'] = md5($random);
header("Expires: Mon, 36 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no cache");
header('Content-tyor: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
</body>
</html>
>> implement-captcha
<?php
session_start();
if($_POST['submit']) {
if (md5($_POST['pin']) == $_SESSION['image_random_value']) {
echo '<p>You are people</p>';
}else{
echo '<p>Are you human being?</p>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form name="form1" method="post" action="">
<img src="captcha.php?date=<?php echo date('YmdHis');?>" alt="security image" />
<div>
input Text Above: <input type="text" name="pin" />
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
?>
pastekan gambar berikut dengan name "background" pada folder php kalian
berikut hasil nya
jika kode pos diisikan text :
jika tgl lahir belum diisi dengan sesuai :
jika kode captcha diisi salah
dan jika semua diidi dengan benar maka SUCCESFUL
Tidak ada komentar:
Posting Komentar