am trying to make a register form for my users using php, json and jquery request i think php code gose fine but i think problem is with jquery function..i am new to this all can u guys help me out..pardon me if am wrong and feel free to advice.
**php code goes here:**
<!--register.php-->
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['mobile'])) {
var_dump('here');
// receiving the post params
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$password = $_POST['password'];
$mobile = $_POST['mobile'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($fname, $lname, $email, $password, $mobile);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["id"];
$response["user"]["fname"] = $user["fname"];
$response["user"]["lname"] = $user["lname"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (fname, lname, email, password or mobile) is missing!";
echo json_encode($response);
}
?>
hear gose the DB_Functions.php code...!
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($fname, $lname, $email, $password, $mobile) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$stmt = $this->conn->prepare("INSERT INTO users(fname, lname, email, password, mobile)
VALUES('".$fname."', '".$lname."', '".$email."', '".$password."', '".$mobile."')";
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = '$email'");
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
}
And here goes the front end part for data insertion..
<html>
<head>
<title>jQuery Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#submit").click(function(){
$.ajax({
url: "localhost/web_login_api/register.php",
type: "POST",
data: {
fname: $("#fname").val(),
lname: $("#lname").val(),
email: $("#email").val(),
password: $("#password").val(),
mobile: $("#mobile").val()
},
dataType: "JSON",
success: function (jsonStr) {
$("#result").text(JSON.stringify(jsonStr));
}
});
});
});
</script>
</head>
<body>
<div id = "result"></div>
<!--html body-->
<form name = "register" action = "#" id = "register" method = "POST">
<label>First name:</label>
<input type = text name = "fname" id = "fname">
<label>Last name:</label>
<input type = "text" name = "lname" id = "lname">
<label>E-mail:</label>
<input type = "email" name = "email" id = "email">
<label>Password</label>
<input type = "password" name = "password" id = "password">
<label>Mobile no:</label>
<input type = "text" name = "mobile" id = "mobile">
<input type="button" value="Insert" name="submit" id="submit" href = "#">
</form>
</body>
</html>
Ok try this now.. Edited..
In DB_Functions.php File
<?php
class DB_Functions {
private $db;
// constructor
function __construct() {
require_once 'config.php';
try {
$hostname = DB_HOST ;
$dbname = DB_DATABASE;
$this->db = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
/**
* Storing new user
* returns user details
*/
public function storeUser($fname, $lname, $email, $password, $mobile) {
try {
$hash = md5($password);
$sql = "INSERT INTO db_name(fname, lname, email, password, mobile) VALUES ('$fname', '$lname', '$email', '$hash', $mobile)";
$result = $this->db->query($sql);
if ($result) {
// get user details
$id = $this->db->lastInsertId(); //last inserted id
$sql = "SELECT * FROM db_name WHERE email = $email";
$result = $this->db->query($sql);
$no_of_rows = $result->fetchColumn();
// returns confirmation message if completed
if ($no_of_rows > 0) {
return "existsandcompleted";
}
}
}
catch (Exception $e) {
$error = 'Error accessing database: ' . $e->getMessage();
}
}
}
?>
In main file
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['mobile'])) {
// receiving the post params
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$password = $_POST['password'];
$mobile = $_POST['mobile'];
// create a new user
$user = $db->storeUser($fname, $lname, $email, $password, $mobile);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["id"];
$response["user"]["fname"] = $user["fname"];
$response["user"]["lname"] = $user["lname"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (fname, lname, email, password or mobile) is missing!";
echo json_encode($response);
}
?>
This totally works.. If this not works then you have syntax errors somewhere ... Also, if you have int datatype to number field then dont make string.. Hope this works. !! Cheers !
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments