Question

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.

Was it helpful?

Solution

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
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top