Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
Kira-kira cara saya ini bener ngga ya?
Cara ini berhasil tapi saya pengen tau mnurut agan2 cara ini ada kelemahannya ngga ya?
JAVASCRIPT
// LOGIN TEMPLATE
Vue.component("login", {
template: "#login-template",
methods: {
login: function () {
let email = $("#email-login").val();
let password = $("#pass-login").val();
let action = $(".submit-btn").data("name");
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
$("#hasil").html(xhr.responseText);
}
}
xhr.open("GET", `ajax.php?
action=${action}&
email=${email}&
password=${password}`, true);
xhr.send();
}
}
})
// REGISTER TEMPLATE
Vue.component("regis", {
template: "#regis-template",
methods: {
validasi: function () {
let forms = document.forms[0];
let lolos = true;
for (i = 0; i < forms.length - 1; i++) {
if (forms[i].value.length <= 0) {
forms[i].classList.add("err");
lolos = false;
} else {
forms[i].classList.remove("err");
}
}
return lolos;
},
regis: function () {
let nama = $("#nama-regis").val();
let email = $("#email-regis").val();
let profesi = $("#profesi-regis").val();
let foto = $("#foto-regis").val();
let password = $("#pass-regis").val();
let action = $(".submit-btn").data("name");
if (this.validasi()) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText)
$("#hasil").html(xhr.responseText);
}
}
xhr.open("GET", `ajax.php?
nama=${nama}&
email=${email}&
profesi=${profesi}&
foto=${foto}&
action=${action}&
password=${password}`, true);
xhr.send();
}
}
}
})
PHP
<?php
$conn = mysqli_connect("localhost","root","","trips");
$nama = $_GET["nama"];
$email = $_GET["email"];
$profesi = $_GET["profesi"];
$foto = $_GET["foto"];
$password = $_GET["password"];
$action = $_GET["action"];
switch($action) {
case "regis" :
// REGISTER
$emailcek = mysqli_query($conn, "SELECT email FROM users WHERE email='$email'");
if(mysqli_num_rows($emailcek)) {
echo "<script>alert('Email Sudah Terdaftar')</script>";
} else {
$password = password_hash($password, PASSWORD_DEFAULT);
mysqli_query($conn, "INSERT INTO users VALUES ('','$nama','$email','$profesi','$foto','$password')");
if(mysqli_affected_rows($conn)) {
echo "<script>alert('Berhasil Registrasi')</script>";
}
}
break;
case "login" :
$emailcek = mysqli_query($conn, "SELECT * FROM users WHERE email='$email'");
if(mysqli_num_rows($emailcek)) {
$passCek = mysqli_fetch_assoc($emailcek);
if(password_verify($password, $passCek["password"])) {
session_start();
$_SESSION["nama"] = $passCek["nama"];
echo "<script>location.replace('index.php')</script>";
} else {
echo "<script>alert('Password Salah')</script>";
}
} else {
echo "<script>alert('Email dan Password tidak cocok')</script>";
}
break;
}
?>
0
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban