Minggu, 15 Januari 2017

Validasi dalam PHP


Langsung saja kodingannya seperti berikut:


database.php
<?php
include('simpan_data.php');
$host = "localhost";
$user = "root";
$password = "";
$name = "p8";
$connect = mysql_connect($host, $username, $password) or die("Koneksi ke database gagal!");
mysql_select_db($name, $connect) or die("Tidak ada database yang dipilih!");

?>


simpan_data.php
<?php
if(isset($_POST['submit'])){
include "database.php";
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$tgllhr = $_POST['tgllhr'];
$jk = $_POST['jk'];
$input = mysql_query("INSERT INTO form VALUES(NULL, '$username', '$password', '$email', '$tgllhr', '$jk')") or die(mysql_error());

if($input){
echo '<center><h1>Data berhasil di tambahkan! ';
echo '<a href="validasi.php"><h1>Kembali</h1></a>';
}else{
echo '<h1>Gagal menambahkan data! </h1>';
echo '<a href="validasi.php"><h1>Kembali</a></h1>';
}
}

?>
validasi.php
<html>
<head><title>form postest </title></head>
<body>
<form name="form" action="simpan_data.php" method="POST" onSubmit="return validasi()">
<table border="2" align="center" height="1" width="20" cellpadding="1">
<td>
<b><font color="black" size="6"> Form mahasiswa</font></b>
<p>
<input type="text" id="username" name="username" size="50" placeholder=" username anda ">
<br><br><br>
<input type="password" id="password" name="password" size="50" placeholder=" password anda ">
<br><br><br>
<input type="text" id="email" name="email" size="50" placeholder=" email anda ">
<br><br><br>
<input type="text" id="tgl" name="tgllhr" size="50" placeholder=" tanggal lahir anda">
<br><br>
<h3><p><b> Jenis Kelamin : </h3>
<h4><input type="radio" name="jk" value="Laki Laki" id="pria">Laki Laki</h4>
<h4><input type="radio" name="jk" value="Perempuan" id="wanita">Perempuan</h4>
<input type="submit" value="Submit" id="Submit" name="submit" onClick="validasi()">
</table>
</form>
<script type="text/javascript">
function validasi(){
var numbers=/^[0-9]+$/;
var huruf=/^[a-zA-Z ]+$/;
var email = document.getElementById('email').value;
pola_email=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var pria=form.jk[0].checked;
var wanita=form.jk[1].checked;
var validtanggal=/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-[0-9]{4}/;
var tgl=document.getElementById('tgl').value;
if (username==null || username==""){
        alert("Username harus di isi !!!");
       }else if (username.length <6 || username.length>19){
        alert ("Username Harus Antara 6 sampai 19 karakter !!!");
       }
       if (password==null || password==""){
        alert("Password harus di isi");
        return false;
       }else if (password.length<6 || password.length>19){
        alert ("Password Harus Antara 6 sampai 19 karakter !!!");
       }else if (password==username){
        alert ("Password dan Username harus beda !!!");
       }
       if (email==null || email==""){
        alert("Email harus di isi !!!");
       }else if (email.length<5){
        alert ("Panjang Email Harus Lebih Dari 4 Karakter !!! ")
       }else if (!pola_email.test(email)){
        alert ("Format Email yang Anda Masukkan Salah !!!");
        return false;
       }
       if (!tgl.match(validtanggal)) {
        alert ("Format Tanggal Harus dd-mm-yyyy ");
        ("document.getElementById").focus(tgl);
        return false;
       }
       if (pria==false && wanita==false){
        alert ("Pilih Jenis Kelamin anda !!!");
       }
   }
</script>
</body>
</html>

Nama databasenya "p8"
tabelnya "form"

Session dalam PHP


index.php
<?php
include('login.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>

</html>


login.php
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("company", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}

?>


logout.php
<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
header("Location: index.php"); // Redirecting To Home Page
}

?>


profile.php
<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>

</html>



session.php
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("company", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}

?>




Koding Upload Foto dalam PHP


index.php
<html>
<head>
<title>kodingan upload php</title>
</head>
<body>
<h1>Upload Gambar</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>nama</td>

<td><input type="text" name="nama"></td>
</tr>
<tr>
<td>upload</td>
<td><input type="file" name="gambar"></td>
</tr>
<tr>
<td><button type="submit">simpan</button></td>
</tr>
</body>

</html>



upload.php
<?php
$gambar=$_FILES["gambar"]["tmp_name"];
$nama=$_POST['nama'];

move_uploaded_file($_FILES['gambar']['tmp_name'],"gambar/".$_FILES['gambar']['name']);
echo "berhasil <br>";
echo $nama."<br>";
echo "<img src='gambar/".$gambar."' width='100px' height='100px'/>";

?>


format file dibawah ini

Program CRUD dalam PHP


edit.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p><a href="tampil.php">Tampil database</a> / <a href="tambah.php">Tambah Data</a></p>

<h1>Edit Data Mahasiswa

<?php
include('koneksi.php');
$id = $_GET['id'];
$show = mysql_query("SELECT * FROM addmahasiswa WHERE mhs_id='$id'");
if(mysql_num_rows($show) == 0){
echo '<script>window.history.back()</script>';
}else{
$data = mysql_fetch_assoc($show);
}
?>

<form action="edit-proses.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<table cellpadding="3" cellspacing="0">
<tr bgcolor="#FF1493">
<td>NIM</td>
<td>:</td>
<td><input type="text" name="nim" value="<?php echo $data['nim']; ?>" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" value="<?php echo $data['nama']; ?>" required></td>
</tr>
<tr bgcolor="#B22222">
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" value="<?php echo $data['alamat']; ?>" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" value="<?php echo $data['email']; ?>" required></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td><input type="submit" name="simpan" value="Simpan"></td>
</tr>
</table>
</form></h1>
</body>

</html>



edit-proses.php
<?php
if(isset($_POST['simpan'])){
include('koneksi.php');
$id = $_POST['id'];
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$email = $_POST['email'];

$update = mysql_query("UPDATE addmahasiswa SET nim='$nim', nama='$nama', alamat='$alamat', email='$email' WHERE mhs_id='$id'") or die(mysql_error());
if($update){
echo '<center><h1>Data berhasil di simpan! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}else{
echo 'Gagal menyimpan data! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}
}else{
echo '<script>window.history.back()</script>';

}

?>


hapus.php
<?php
if(isset($_GET['id'])){
include('koneksi.php');
$id = $_GET['id'];
$cek = mysql_query("SELECT mhs_id FROM addmahasiswa WHERE mhs_id='$id'") or die(mysql_error());

if(mysql_num_rows($cek) == 0){
echo '<script>window.history.back()</script>';
}else{
$del = mysql_query("DELETE FROM addmahasiswa WHERE mhs_id='$id'");
if($del){
echo '<center><h1>Data mahasiswa berhasil di hapus! ';
echo '<a href="tampil.php">Kembali</a>';
}else{
echo 'Gagal menghapus data! ';
echo '<a href="tampil.php">Kembali</a>';
}
}
}else{
echo '<script>window.history.back()</script>';

}

?>


koneksi.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$name = "databasepwd5";

$koneksi = mysql_connect($host, $user, $pass) or die("Koneksi ke database gagal!");
mysql_select_db($name, $koneksi) or die("Tidak ada database yang dipilih!");

?>


tambah.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p> <a href="tampil.php">Tampil Database gan!</a><br>
<a href="tambah.php">Tambah Data gan!</a>
</p>
<br>
<h1>Tambah Data Mahasiswa

<form action="tambah-proses.php" method="post">
<table cellpadding="3" cellspacing="0">
<tr bgcolor="#FF1493">
<td>Nim</td>
<td>:</td>
<td><input type="text" name="nim" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" required></td>
</tr>
<tr bgcolor="#B22222">
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" required></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td><input type="submit" name="tambah" value="Tambah"></td>
</tr>
</table>
</form></h1>
</body>

</html>



tambah-proses.php
<?php
if(isset($_POST['tambah'])){
include('koneksi.php');
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$email = $_POST['email'];

$input = mysql_query("INSERT INTO addmahasiswa VALUES(NULL, '$nim', '$nama', '$alamat', '$email')") or die(mysql_error());

if($input){
echo '<center><h1>Data berhasil di tambahkan! ';
echo '<a href="tambah.php"><h1>Kembali</h1></a>';
}else{
echo '<h1>Gagal menambahkan data! </h1>';
echo '<a href="tambah.php"><h1>Kembali</a></h1>';
}
}else{
echo '<script>window.history.back()</script>';
}

?>


tampil.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p> <a href="tampil.php">Tampil Database gan!</a><br>
<a href="tambah.php">Tambah Data gan! </a>
</p>
<br>
<h1>Data Mahasiswa

<table border="2" class="table">
<tr bgcolor="#FF1493">
<th>No</th>
<th>Nim</th>
<th>Nama</th>
<th>Alamat</th>
<th>Email</th>
<th>Opsi</th>
</tr></h1>
<?php
include "koneksi.php";
$query_mysql=mysql_query("SELECT * FROM addmahasiswa")or die(mysql_error());
$no=1;
while($data = mysql_fetch_assoc($query_mysql)){
?>
<tr bgcolor="#E9967A">
<td><?php echo $no++; ?></td>
<td><?php echo $data['nim']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['alamat']; ?></td>
<td><?php echo $data['email']; ?></td>
<td>
<a class="edit" href="edit.php?id=<?php echo $data['mhs_id'];?>">Edit</a> |
<a class="hapus" href="hapus.php?id=<?php echo $data['mhs_id'];?>">Hapus</a>
</td>
</tr>
<?php }?>
</table>
</body>

</html>

Pertemuan #2 PWD

Pertemuan #2 PWD
 <?php
session_start ();

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<form action="input.php" method="post" >
<input type="text" name="nama" placeholder="nama">
<input type="text" name="alamat" placeholder="alamat">
<input type="submit" value="simpan">
<input type=reset value=Bersih>
</form>

<?php

for($saya=0;$saya<10000;$saya++) {
 if ($_SESSION ['a'][$saya][0]=="") {
 $_SESSION['a'][$saya][0]=$_POST['nama'];
 $_SESSION['a'][$saya][1]=$_POST['alamat'];

 break; }

}


for ($i=0;$i<count($_SESSION['a']);$i++) {
if ($i==0) {
echo "<table border=0 ";
echo "<tr><th>Nama<th>Alamat<tr>";
echo "<tr><td>".$_SESSION['a'][$i][0]."<td>".$_SESSION['a'][$i][1];
}
else { echo "<tr><td>".$_SESSION['a'][$i][0]."<td>".$_SESSION['a'][$i][1];
}}




 ?>

</body>
</html>



 

tugas OOP PHP

Tugas OOP PHP (B)
<?php
class Bangunruang{

    protected $panjang;
    private $lebar;
    function setPanjang($panjang){

        $this->panjang= $panjang;
    }

    Public function setLebar($lebar){

        $this->lebar= $lebar;
    }


    function luas(){

        return $this->panjang*$this->lebar;
    }

}

class Balok extends Bangunruang{

    protected $tinggi;


    function setTinggi($tinggi){
        $this->tinggi=$tinggi;
    }


    function volume(){

        return $this->tinggi*$this->luas();
    }


}



$Bangun1 = new Balok();

$Bangun1->setPanjang(8);
$Bangun1->setLebar(5);
$Bangun1->setTinggi(20);

echo "OBJECT 1 </br>";
echo "Luas Persegi Panjang = ".$Bangun1->luas()."</br>";
echo "Volume Balok = ".$Bangun1->volume()."</br>";

$Bangun2 = new Balok();
$Bangun1->setPanjang(10);
$Bangun1->setLebar(15);
$Bangun1->setTinggi(6);


echo "OBJECT 2 </br>";
echo "Luas Persegi Panjang = ".$Bangun1->luas()."</br>";
echo "Volume Balok = ".$Bangun1->volume()."</br>";

$Bangun3 = new Balok();
$Bangun1->setPanjang(6);
$Bangun1->setLebar(6);
$Bangun1->setTinggi(6);

echo "OBJECT 3 </br>";
echo "Luas Persegi Panjang = ".$Bangun1->luas()."</br>";
echo "Volume Balok = ".$Bangun1->volume()."</br>";

?>

Sabtu, 14 Januari 2017

Matakuliah : Pemrograman Web Dinamis
Kelas : A
Anggota:
Panggah Widiandana (1400018111)
Joana Rosaa Miranda(1400018084)
Panji Bintoro(1400018085)
Mukhti Wibowo(1300018091)

Link Youtube : https://www.youtube.com/watch?v=W1bKARLmckQ