Click here to Skip to main content
15,352,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1.display error message using the javascript and ajax
2.validate the name and the group if it exists display error message and if it doesn't exist insert into the database successfully

What I have tried:

this the html with ajax and it displays successful and inserts into the database but how can I display error message if the name and group is already exist after the validation
	

    <form action="" id="manage-project">
<form action="" id="manage-project">

				
					<label for="" class="control-label">Project Title</label>
					<input type="text" class="form-control form-control-sm" name="name"  value="<?php echo isset($name) ? $name : '' ?>">
				</div><div class="form-group">
          <label for="" class="control-label">Project Group </label>
          <input type="text" class="form-control form-control-sm" name="pname" required value="<?php echo isset($pname) ? $pname : '' ?>">

        </div>
    <div class="card-footer border-top border-info">
    		<div class="d-flex w-100 justify-content-center align-items-center">
    			<button class="btn btn-flat  bg-gradient-primary mx-2" form="manage-project">Save</button>
    			<button class="btn btn-flat bg-gradient-secondary mx-2" type="button" onclick="location.href='index.php?page=project_list'">Cancel</button>
    		</div>
    	</div>
</form>

    $('#manage-project').submit(function(e){
		e.preventDefault()
		start_load()
		$.ajax({
			url:'ajax.php?action=save_project',
			data: new FormData($(this)[0]),   

		    cache: false,
		    contentType: false,
		    processData: false,
		    method: 'POST',
		    type: 'POST',
			success:function(resp){
				if(resp == 1){
					alert_toast('Data successfully saved',"success");
					setTimeout(function(){
						location.href = 'index.php?page=project_list'
					},2000)
				}
			}
		})
	


This is the  insertion query and I guess I need select query to validate the name and the group. help me out here  

    function save_project(){
		extract($_POST);
		$data = "";
		foreach($_POST as $k => $v){
			if(!in_array($k, array('id','user_ids')) && !is_numeric($k)){
				if($k == 'description')
					$v = htmlentities(str_replace("'","’",$v));
				if(empty($data)){
					$data .= " $k='$v' ";
				}else{
					$data .= ", $k='$v' ";
				}
			}
		}
		if(isset($user_ids)){
			$data .= ", user_ids='".implode(',',$user_ids)."' ";
		}
		if(empty($id)){
			$save = $this->db->query("INSERT INTO project_list set $data");
		}else{
			$save = $this->db->query("UPDATE project_list set $data where id = $id");
		}
		if($save){
			return 1;
		}
	}
Posted
Updated 27-Jun-21 5:38am
v2
Comments
Richard Deeming 23-Jun-21 5:58am
   
If "Dafuq" is your real name, and not a reference to the vulgar contraction of an English expletive, then you need to complain loudly to your parents.

Otherwise, I strongly suggest you change your nickname here if you want to be taken seriously. Using "clever" ways to circumvent the language filter makes you look childish at best, and potentially a troll.
[no name] 23-Jun-21 6:07am
   
alright how about any comment or any solution about my problem here?
CHill60 23-Jun-21 6:38am
   
You've answered your own question "I guess I need select query to validate the name and the group". Are you saying you don't know how to write a select or don't know what to put in the where clause - what?
[no name] 23-Jun-21 7:09am
   
yes I'm confused what to put in the where clause and where to write the query. help me here

1 solution

You need to look here for how to make a SELECT SQL SELECT Statement[^]

Your WHERE is going to be the fields and values you need to test.

Easy Test method: get the count (0 or more) answers your question.


   
Comments
[no name] 26-Jun-21 11:59am
   
I know how to use the sql statement. my question is after which line of code should I use the query in my code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900