I'm trying to create a form to Add new users into the website. I created HTMl form and trying to connect 'http://localhost/r/svr/user.php?action=add&name=Aa&[email protected]&role=1&pass=1234' post API to send data to the database.
HTML file
<div class="container-fluid">
<button onclick="openForm()">Add new</button>
<br>
<br>
<div class="form-popup" id="myForm">
<form method="post" action="dashboard-users.php" class="form-container">
<h3>Add user</h3>
<label for="name"><b>Name</b></label>
<input type="text" placeholder="Enter Name" id="username" name="username" required>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" id="email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" id="psw" name="psw" required>
<label for="role"><b>Role</b></label>
<input type="text" placeholder="Student/Teacher" id="role" name="role" required>
<button type="submit" class="btn" onclick="addUser()">Add</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
</div>
JS file
function addUser(){
var name = $("input#username").val(); //comes from a drop-down select box whose id="cmbGender" on the html form.
var email = $("input#email").val(); //comes from an input textbox whose id="txtDOB" on the html form.
var password = $("input#psw").val(); //comes from an input textbox whose id="txtHeight" on the html form.
var role = $("input#role").val(); //comes from a drop-down select box whose id="cmbCountries" on the html form.
var role_id = '';
if(role == 'student'){
role_id = '3';
}else if(role == 'teacher'){
role_id = '2';
}else{
role_id = '1';
}
//now create an array to hold all the data in one structure
var dataArray = {
"name":name,
"email":email,
"pass":password,
"role":role_id
};
//now invoke jQuery to finish of the job:
jQuery.ajax({
type: "POST",
url: ".http://localhost/r/svr/user.php?action=add&",
data: {u_data : JSON.stringify(dataArray)},
cache: false,
success: function(svr_resp){
if(svr_resp == '1'){
alert("Data successfully captured on the server.");
//you may do more things here, depending on your needs...
} else {
alert("Error: Data not captured on the server.");
//you may do more things here, depending on your needs...
}
}
});
}
when i click Add button it will not send data to the database. I'll appreciate if someone could help me to resolve it
Update
user.php
<?php
require_once 'db_user.php';
require_once 'util.php';
$action = process_request_and_get_action();
info(__FILE__, __FUNCTION__, __LINE__, $action, reset_pass($_REQUEST));
switch ($action) {
case ACTION_READ:
db_user_read($_REQUEST);
break;
case ACTION_ADD:
db_user_add($_REQUEST);
break;
case ACTION_MOD:
update_user_profile($_REQUEST);
break;
default:
exit(fail_return(ERR_UNKNOWN_ACTION, false));
}
?>
db_user_add()
function db_user_add($args){
$role = $args['role'];
add_basic_info($args);
add_role_user($args);
if ( $role == 2){
add_teachers_details($args);
}
}
function add_basic_info($args){
$name = $args['name'];
$email = $args['email'];
$password = $args['pass'];
$query = "INSERT INTO `users` (
`id`, `name`, `email`, `password`, `created_at`, `updated_at`
) VALUE (
NULL, '$name', '$email', sha1('$password'), NULL, NULL
)";
debug(__FILE__,__FUNCTION__,__LINE__, $query);
db_execute($query);
}
function add_role_user($args){
$id = get_user_id($args);
$role = $args['role'];
$query = "INSERT INTO `role_user`(
`role_id`, `user_id`
) VALUE (
$role, $id
)";
debug(__FILE__,__FUNCTION__,__LINE__, $query);
db_execute($query);
}
function get_user_id($args){
$name = $args['name'];
$query = "select id from users where name = '$name'
";
$result = db_execute($query);
debug(__FILE__,__FUNCTION__,__LINE__, $result);
return $result['0']['id'];
}