this the html with ajax and it displays successful and inserts into the database but how can I display error message if the name is already exist after the validation
JavaScript
x
2
1
<form action="" id="manage-project">
2
JavaScript
1
14
14
1
<label for="" class="control-label">Project Title</label>
2
<input type="text" class="form-control form-control-sm" name="name" value="<?php echo isset($name) ? $name : '' ?>">
3
</div><div class="form-group">
4
<label for="" class="control-label">Project Group </label>
5
<input type="text" class="form-control form-control-sm" name="pname" required value="<?php echo isset($pname) ? $pname : '' ?>">
6
7
</div>
8
<div class="card-footer border-top border-info">
9
<div class="d-flex w-100 justify-content-center align-items-center">
10
<button class="btn btn-flat bg-gradient-primary mx-2" form="manage-project">Save</button>
11
<button class="btn btn-flat bg-gradient-secondary mx-2" type="button" onclick="location.href='index.php?page=project_list'">Cancel</button>
12
</div>
13
</div>
14
JavaScript
1
22
22
1
$('#manage-project').submit(function(e){
2
e.preventDefault()
3
start_load()
4
$.ajax({
5
url:'ajax.php?action=save_project',
6
data: new FormData($(this)[0]),
7
8
cache: false,
9
contentType: false,
10
processData: false,
11
method: 'POST',
12
type: 'POST',
13
success:function(resp){
14
if(resp == 1){
15
alert_toast('Data successfully saved',"success");
16
setTimeout(function(){
17
location.href = 'index.php?page=project_list'
18
},2000)
19
}
20
}
21
})
22
This is the insertion query and I guess I need select query to validate the name. help me out here
JavaScript
1
27
27
1
function save_project(){
2
extract($_POST);
3
$data = "";
4
foreach($_POST as $k => $v){
5
if(!in_array($k, array('id','user_ids')) && !is_numeric($k)){
6
if($k == 'description')
7
$v = htmlentities(str_replace("'","’",$v));
8
if(empty($data)){
9
$data .= " $k='$v' ";
10
}else{
11
$data .= ", $k='$v' ";
12
}
13
}
14
}
15
if(isset($user_ids)){
16
$data .= ", user_ids='".implode(',',$user_ids)."' ";
17
}
18
if(empty($id)){
19
$save = $this->db->query("INSERT INTO project_list set $data");
20
}else{
21
$save = $this->db->query("UPDATE project_list set $data where id = $id");
22
}
23
if($save){
24
return 1;
25
}
26
}
27
Advertisement
Answer
JavaScript
1
6
1
$check = $this->db->query("select name from project_list where and pname='$pname' or name='$name'")->num_rows;
2
if($check > 0){
3
return 2;
4
exit;
5
}
6