views:

15

answers:

1

dear all.. i have one submit form. i want if someone submit same data can show jquery dialog "This data already exists! Are you sure to input?" Then select OK or CANCEL. can you tell me the step that must i do?thanks.

+1  A: 

Send an ajax request with data to server-side, and if data exist return some kind of errorcode. Then in ajax handler check for error, if it is present show message "Data already exist", if not show message "Data was added". Server-side should check for duplicates and insert new data as well.

If user select "ok" in "Data exists" dialog, send another request with parameter to suppress duplicate error.

Client:

$.post("server.php", { "data": somedata }, function(result) {
 if (result.error && result.error == 1)
  if (confirm("Duplicate data, continue?"))
    $.post("server.php, { "data": somedata, "suppress": 1 }, function(result) {
      alert("Data was added");
    });
 else
  alert("Data was added");
}, "json"); // we accept result in json format, jQuery will process it into JS object

Server:

if (isset($_POST['data']) && $_POST['data'] != "") {
  if ( check_duplicate($_POST['data']) // don't forget to implement this
       && $_POST['suppress'] != 1 )
    return '{ "error": 1 }';
  else {
    insert_data($_POST['data']);
    return '{ ok }'; // you can return empty string as well
  }
}
fuwaneko
you say "data":somedata..can you give me example for this?
klox
Let's say you have 2 form inputs with ids "i1" and "i2". Then you send data like this: { "i1": $("#i1").val(), "i2": $("#i2").val() }. Put this as second parameter to $.post() function. And change PHP code appropriately.
fuwaneko