Hi Team
I am unable to register new users, due to this issue to my js file. How do i resolve this issue? I have the correct js library. Let me share my logic below;
// register.js
JavaScript
(function($) {
$.fn.registerMessage = function(message, type) {
// create a new element to display the message
var messageElem = $('', {
class: 'register-messages ' + type,
text: message
});
// append the message element to the div card
$('#registration-messages').html(messageElem);
// set a timeout to remove the message element after 5 seconds
setTimeout(function() {
messageElem.remove();
}, 5000);
};
})
// html code
Submit
// register.js
/**
*@author:Gcobani Mkontwana
*@date:03/05/2023
*@Register users to the form and validate empty submission.
**/
$(document).ready(function() {
$('#registration-form').submit(function(event) {
event.preventDefault(); // prevent the form from submitting normally
var name = $('#name').val();
var email = $('#email').val();
var password = $('#password').val();
// validate the form data
if (!name || !email || !password) {
alert('Please fill in all fields');
return;
}
// submit the form data using AJAX
$.ajax({
type: 'POST',
url: 'register.php',
data: {
name: name,
email: email,
password: password
},
success: function(response) {
if (response === 'success') {
$('body').registerMessage('Registration successful', 'success');
} else {
$('body').registerMessage('Registration failed', 'alert');
}
}
});
});
});
Tuhin PaulRank #7 · 71.9K reputationPosted May 3, 2023, 2:47 PM EDT
I think you are trying to use the registerMessage function defined in register.js, but it has been wrapped inside an immediately invoked function expression (IIFE), which means it is only accessible within its own scope. To fix this issue, move the $(document).ready() block outside of the IIFE so that it can access the registerMessage function properly.
$(document).ready(function() { $('#registration-form').submit(function(event) { event.preventDefault(); // prevent the form from submitting normally var name = $('#name').val(); var email = $('#email').val(); var password = $('#password').val(); // validate the form data if (!name || !email || !password) { alert('Please fill in all fields'); return; } // submit the form data using AJAX $.ajax({ type: 'POST', url: 'register.php', data: { name: name, email: email, password: password }, success: function(response) { if (response === 'success') { $('body').registerMessage('Registration successful', 'success'); } else { $('body').registerMessage('Registration failed', 'alert'); } } }); }); }); jQuery(window).on("load", function(){ console.log("Script loaded!"); $('.registerMessage').each(function () { $(this).registerMessage($(this).attr('data-message'),$(this).attr('data-messageType')); }); });Tuhin PaulRank #7 · 71.9K reputationPosted May 3, 2023, 2:45 PM EDT
Based on the code you provided, it seems that the issue might be related to the use of
$.fn.registerMessagefunction. This function is not a built-in jQuery function, but rather a custom function that you have defined. Therefore, it is possible that the issue might be with the implementation of this function.