Exploit
Exploit

Reputation: 6386

jquery ajax how to validate if a record exists in the db

using jquery ajax i have a text field where a name is entered how can i have it where when a user enters a name ajax checks to see if the name already exists in the db and if it does a message is displayed that this name already exists and the form cannot be submitted until a name is entered that does not exist?

this would be the php

<?php
$name = $security->secure($_POST['name']) //security->secure() cleans the field
$query = $db->query("select name from table where name = '$name'");
if($db->num_rows($query) != 0) { "that record already exists"; }
//disable the form button until a name is entered that does not exist.
?>

Upvotes: 2

Views: 3049

Answers (2)

Rabea
Rabea

Reputation: 2024

in Jquery you can use the method $.ajax, $.post , and even $.get to make an easy ajax call example:

$(document).ready(function(){
  $.ajax({
     type: 'POST', /*the method u are using*/
     url: url, /* http://yourdomain.com/post.php */
     success: function(data){alert('Name exists')} /* What to do in case of success */
  });
}); 

I hope this was helpful enough to do the job for u .

Upvotes: 2

kobe
kobe

Reputation: 15835

@sammen

you need to use jquery ajax

http://api.jquery.com/jQuery.ajax/

send the user name to the backend , and execute your php and send a status to the UI.

on the success handler of ajax , you can call a method or show a error that name is not valid

you can use jquery ajax for this

Upvotes: 1

Related Questions