show dialog for option submit data - submit

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.

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
}
}

Related

OData success message even though no new entry?

I have an SAPUI5 application which is deployed to ABAP server and accessible with Fiori Launchpad. I use this app to create a new interaction (OData Service CUAN_IMPORT_SRV) in Hybris Marketing. My problem is that even though the OData call created no new entry (because such entry already exists), I get the success message. When I add invalid data to the upload data, I get the error message.
This is my code:
var oModel = new sap.ui.model.odata.v2.ODataModel("https://hostname:port/sap/opu/odata/sap/CUAN_IMPORT_SRV/", true);
var oData = { some json... }
oModel.create("/ImportHeaders", oData, {
success: function() {
sap.m.MessageBox.success("Interaction successfully created!", {
title: "Success"
});
},
error: function() {
sap.m.MessageBox.error("Interaction could not be created.", {
title: "Error"
});
}
});
When I run /n/iwfnd/traces it is marked as "successful execution" (even though no new entry was created).
How can it be that the success message appears even though no new entry was created? How can I avoid that?
First thing is to add your business error in the ABAP backend:
DATA:
lt_bapi_return type table of bapiret2,
lo_message_container type ref to /iwbep/if_v4_message_container.
#Error handling
if lt_bapi_return is not initial.
#check if an error message is in lt_bapi_return
loop at lt_bapi_return into ls_bapi_return.
if ls_bapi_return-type = 'E'.
lo_message_container = io_response->get_message_container( ).
loop at lt_bapi_return into ls_bapi_return.
lo_message_container->add_t100(
exporting
iv_msg_type = ls_bapi_return-type
iv_msg_id = ls_bapi_return-id
iv_msg_number = ls_bapi_return-number
iv_msg_v1 = ls_bapi_return-message_v1
iv_msg_v2 = ls_bapi_return-message_v2
iv_msg_v3 = ls_bapi_return-message_v3
iv_msg_v4 = ls_bapi_return-message_v4 ).
endloop.
"raise exception
raise exception type zcx_e2e001_odata_v4_so
exporting
message_container = lo_message_container.
endif.
endloop.
endif.
And at UI:
error: function(response) {
//response will have message details
//each message can have business text, technical info, error code.
sap.m.MessageBox.error("Interaction could not be created.", {
title: "Error"
});
}
You can add this part of code to every redefined method..
better is create a util method and reuse.

409 http error in Couchbase lite in phonegap on iOS

I'm getting a 409 on PUT, POST and DELETE actions.
I have successfully created a database and have PUT one document successfully ONCE. I have tried local and "normal" documents. I haven't spend any focus on revisions but think it has to do with this. I only want to save and update this one JSON string in my app - thats it.
It's like I have created this one document to stay forever :-)
Will sample code help? I'm really only using Angular's $http.
On a side note: I need a save mechanism in phonegap that is html5 cache-clear resistent.
You need to check if your document exists first before you update it. that's why it worked the first time and not the second.
config.db.get( "myDocumentID", function(error, doc) {
if (error) {
if (error.status == 404) {
//document does not exist insert it
config.db.put( "myDocumentID", myDocument, function(error,ok) {
if(error) { alert( "error" + JSON.stringify( error ) } else {
alert("success");
}
} )
} else {
alert( "error:" + JSON.stringify( error ) )
}
} else {
//update your document
doc.my_new_key = "value";
config.db.put( "myDocumentID", doc, function(error, ok) {
if( error ) { alert( "error:" + JSON.stringify( error ) ) } else {
alert("success");
}
} );
}
} )

jQuery JsTree and JSON error handling

I am using MVC to pass JSON data to JsTree and show a hierarchical view of information.
Everything is working just fine, however, there are times when the user does not have access the the data or for some reason the MVC action throws an exception:
In these cases, the action passes a JSON error message and sets the HttpStatusCode to NotAccepted or InternalServerError.
However the jsTree's sinner keeps spinning and I don't seem to find a way to make it stop and show the error message.
Has anyone solved this issue before? How can one does error handling when using JsTree's JSON data plugin?
UPDATE:
I figured out how to capture the error:
$("#jstree1").jstree({
"json_data": {
"ajax": {
"url": serviceUrl,
"data": function (n) {
return { pid: n.attr ? n.attr("id") : "" };
},
"error": function (x, s, r) { var err = $.parseJSON(x.responseText); if (err!="") { alert(err); } }
}
}
It seems that JsTree does get the MVC http statusCode and the error, now I need to figure out how to tell the JsTree to stop waiting and remove the spinner image!
I am also looking for a good way of showing the error in JsTree, or should I manage the error message outside of it?
I've solved this problem.
Just a note- the code example above for handling ajax call errors is incorrect, please see a complete example below:
$('#YourTree').jstree({
"json_data": {
"ajax": {
"url": "/Controller/Action",
"data": function () {
return { Parameter1: "Value1", Parameter2: "Value2" }
},
"type": "POST",
"dataType": "json",
"error": function (jqXHR, textStatus, errorThrown) { $('#YourTree').html("<h3>There was an error while loading data for this tree</h3><p>" + jqXHR.responseText + "</p>"); }
}
}
});
And in the actual action, you need to set the http response status code to 1 and write the error. e.g.
Response.StatusCode = 1
Response.Write("Error you want to go into jqXHR.responseText here");
Enjoy :)
Maybe you should look into handling this error a layer above the .jstree. Maybe by handling the window.onerror event you can achieve this. In here you could call a function that will rebuild the tree or something? Be sure to include this script as the first one in your page.
<script type="text/javascript">
window.onerror = function(x, s, r){
alert('An error has occurred!')
}
</script>

How to detect that unobtrusive validation has been successful?

I have this code that triggers when a form is submitted:
$("form").submit(function (e) {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("Address").value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$("#LatitudeLongitude").val(results[0].geometry.location);
$("form").submit();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
$('form').unbind('submit');
return false;
});
What it does: it calls google geocoding service to translate an address into latitude/longitude which is set into a hidden field of the form. If there is a result, then the form is submitted.
The problem is that if validation fails (for instance a required field has not been set) then the call to geocoding is still made. Moreover, if I click a second time on the submit button, even if the required field has not been set, the form is posted.
How can I call the geocoding service only if the unobtrusive validation has been successful?
Rather than attaching to the submit() event, you need to capture an earlier event, and exercise control over how to proceed.
First, let's assume your original button has an id of submit and you create a new submit button with an id of startSubmit. Then, hide the original submit button by setting the HTML attribute display="false". Next, bind to the click event of your new button, and add your code, as modified:
$("#startSubmit").live("click", function() {
// check if the form is valid
if ($("form").validate().form()) {
// valid, proceed with geocoding
var geocoder = new google.maps.Geocoder();
var address = $("#Address").val();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$("#LatitudeLongitude").val(results[0].geometry.location);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
// proceed to submit form
$("#submit").click();
}
}
return false;
});
This will invoke validation, so that geocoding will only occur if the form is valid, then, after geocoding has returned a response, it will submit the form by incoking the click event on the submit button.
I implemented something similar (with thanks to cousellorben), in my case I wanted to disable and change the text of the submit button but only on success. Here's my example:
$("#btnSubmit").live("click", function() {
if ($("form").validate().form()) {
$(this).val("Please Wait...").attr("disabled", "disabled");
$("form").submit();
return true;
} else {
return false;
}
});
The only difference is that my example uses a single button.
This answer may help you, it gives a quick example of how a function is called when the form is invalid, based on the use of JQuery Validate, which is what MVC uses.

jquery ui autocomplete - open event (handling no results)

I wanted to use the open event to return "no results found" when ui == null but in firebug it looks like that even after the open event is fired the ui list is always empty even if there are results.
My source is an XML file
I'm attempting:
open: function(event,ui) {
if( ui.item == null ) {
console.log("no results");
}
}
Add to autocomplete.js:
if(retMatch.length == 0) {
var noResult = new Array();
noResult[0] = {
label: "No results found",
title: "No results found",
description: "Your search did not return any results. Please refine your search criteria. If you believe this message is in error please fill out a support case."
}
return noResult;
}
to:
$.extend

Resources