While working on my jQM project, I encountered an issue with form submission. Instead of using the "action" URL to submit the form, I opted to use JavaScript to submit the form after validation by another JS addon. Once validated, the form is submitted using jQM $.post().
I found this approach on the jQM page:
Example: Send form data using ajax requests $.post( "test.php", $( "#testform" ).serialize() );
Everything was functioning correctly, including the return data. However, my problem arose when trying to handle the "action" in the form. Removing it would result in the app redirecting to the jQM first page, and using "#" or "/" caused errors such as "Error Loading Page."
My question is, is there a way to eliminate the "Error Loading Page" message? I've attempted adding [data-ajax="false"] in the form tag, but it yielded the same result.
Please provide guidance, thank you.
Here is my JS code: This code resolved the issue. However, uncommenting the commented line to perform the actual task resulted in redirection to the first page of my multipage structure, seemingly ignoring the preventDefault function. What could be the root cause here? Please advise, thank you.
$(document).on("click", "#registerButton", function (e) {
e.preventDefault();
var registerForm = $( "#registerForm" );
if (registerForm.valid() === true) {
document.getElementById("regDeviceName").value = intel.xdk.device.model;
document.getElementById("regDeviceOs").value = intel.xdk.device.platform;
$.post("http://domain.com/api/user/register",
$("#registerForm").serialize(),
function(data,status){
alert(data);
// if (data == "registered") {
// $.mobile.pageContainer.pagecontainer("change", "#regSuccess");
// }
// elseif (data == "unverified") {
// $.mobile.pageContainer.pagecontainer("change", "#unverified");
// }
// elseif (data === "duplicate") {
// $.mobile.pageContainer.pagecontainer("change", "#duplicate");
// }
// else {
// $.mobile.pageContainer.pagecontainer("change", "#suspended");
// };
// $.mobile.pageContainer.pagecontainer("change", "#regSuccess");
});
}
});
Second attempt at JS code:
$("#registerButton").click(function (e) {
e.preventDefault();
var registerForm = $( "#registerForm" );
if (registerForm.valid() === true) {
document.getElementById("regDeviceName").value = intel.xdk.device.model;
document.getElementById("regDeviceOs").value = intel.xdk.device.platform;
$.post("http://domain.com/api/user/register",
$("#registerForm").serialize(),
function(data,status){
alert(data);
// if (data == "registered") {
// $.mobile.pageContainer.pagecontainer("change", "#regSuccess");
// }
// elseif (data == "unverified") {
// $.mobile.pageContainer.pagecontainer("change", "#unverified");
// }
// elseif (data === "duplicate") {
// $.mobile.pageContainer.pagecontainer("change", "#duplicate");
// }
// else {
// $.mobile.pageContainer.pagecontainer("change", "#suspended");
// };
// $.mobile.pageContainer.pagecontainer("change", "#regSuccess");
// $("#registerResult").html("Data: " + data + "<br>Status: " + status);
});
}
});
HTML Form Structure (jQM multipage layout):
<div data-role="page" id="register">
<div data-role="header"><h1>Register</h1></div>
<div data-role="content" class="pageWithHeader">
<div id="formContainer">
<form id="registerForm">
<label for="regFirstName">First Name</label>
<input class="registerVal" type="text" name="regFirstName" id="regFirstName" value="">
<label for="regLastName">Last Name</label>
<input class="registerVal" type="text" name="regLastName" id="regLastName" value="">
<label for="regEmail">Email</label>
<input class="registerVal" type="email" name="regEmail" id="regEmail" value="">
<label for="regPassword">Password</label>
<div id="regPassDiv">
<input class="registerVal" type="password" name="regPassword" id="regPassword" value="">
</div>
<input type="hidden" id="regDeviceName" name="regDeviceName" value="">
<input type="hidden" id="regDeviceOs" name="regDeviceOs" value="">
<div data-role="controlgroup" data-type="vertical">
<button id="registerButton" class="ui-btn ui-corner-all ui-icon-plus ui-btn-icon-left">Register</button>
<a href="#verifyRegister" class="ui-btn ui-corner-all ui-icon-check-square-o ui-btn-icon-left">Go to Account Verification</a>
</div>
</form>
</div>
<!-- <div id="registerResult"></div>-->
</div>
</div>