Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ive created a function but its not been recognized/defined by JS.

Error:
ReferenceError: user_register is not defined
at HTMLButtonElement.onclick

HTML:
<button type="button" class="fv-btn" onclick="user_register()">Register</button>


JavaScript:
function user_register(){
jQuery('.field_error').html('');
var email=jQuery("#login_email").val();
var password=jQuery("#login_password").val();
}

What I have tried:

user_register() is called at register button but it doesnt work at all.
Posted
Updated 27-Sep-20 3:23am
Comments
Richard MacCutchan 27-Sep-20 7:16am    
Is all the code in the same source file?
Captain Network 27-Sep-20 8:22am    
php code is in main folder and the JS code is in js folder,inside main folder
Richard MacCutchan 27-Sep-20 8:28am    
Then they are not in the same file? Have you used an include directive in the main one so that it includes the javascript? Please show the code properly so we can see what you are doing.
Captain Network 27-Sep-20 8:39am    
as you have said I've included main.js into login.php and I've put them in same folder, the problem is still the same.

in main.js file I have this function for form validation::

function user_register() {
jQuery('.field_error').html('');
var name=jQuery("#name").val();
var email=jQuery("#email").val();
var mobile=jQuery("#mobile").val();
var password=jQuery("#password").val();
var is_error='';
if(name==""){
jQuery('#name_error').html('Please enter name');
is_error='yes';
}if(email==""){
jQuery('#email_error').html('Please enter email');
is_error='yes';
}if(mobile==""){
jQuery('#mobile_error').html('Please enter mobile');
is_error='yes';
}if(password==""){
jQuery('#password_error').html('Please enter password');
is_error='yes';
}
if(is_error==''){
jQuery.ajax({
url:'register_submit.php',
type:'post',
data:'name='+name+'&email='+email+'&mobile='+mobile+'&password='+password,
success:function(result){
if(result=='email_present'){
jQuery('#email_error').html('Email id already present');
}
if(result=='insert'){
jQuery('.register_msg p').html('Thank you for registeration');
}
}
});
}

}


which is linked to a login.php page from where the function is called in a register button, like below::

<button type="button" class="fv-btn" onclick="user_register()">Register</button>
Richard MacCutchan 27-Sep-20 8:57am    
Does login.php include the .js file(s)?

Here, if I try a simple code like for you in any JS fiddle[^]:
HTML
HTML
<button type="button" class="fv-btn" onclick="user_register()">Register</button>

JS:
JavaScript
function user_register(){
 alert('Hi');
}

It works perfectly as expected.

Now, based on error you shared, all it can mean, you might have missed adding Javascript reference. A Javascript file/code in script tag of your HTML page.

Try:
HTML
<!DOCTYPE html>
<html lang="en-US">

<head>
    <title>Demo example</title>
    <script>
        function user_register()
        {
          alert('Hi');
        }
    </script>
</head>
<body>
    <button type="button" onclick="user_register()">Register</button>
</body>
</html>
 
Share this answer
 
Comments
Captain Network 27-Sep-20 8:26am    
About the script reference and tag, my JS code is in main.js file so it doesn't require script tag, however I have tried it now and same result.
The Register button isn't doing anything and same error.
i have even check the source code for that particular project and its same as mine,
can it be some compatibality issue with windows architecture ??
mine is windows 10 1709 x64.
Sandeep Mewara 27-Sep-20 8:56am    
No. There is nothing around compatibility here.

And you are wrong about not needing script tag. Based on what you share, you need to make sure that current page has the main.s file reference via script tag.
like:
<script src="myscripts.js"></script> //make sure to have the right relative path here
Captain Network 27-Sep-20 9:24am    
Thank You, it worked by putting the code together in one file.
Relaxation Relaxation 2-Dec-22 10:02am    
Im currently facing the same issue too, I already included the js file on my login page but the problem is when im clicking the button its not working Im not redirecting on the index page
so the problem has been solved thanks to these guys,
my js code was in main.js and registration code in login.php, somehow my functions didn't work even though main.js was connected to login.php.
When i put the js code within login.php using
<script type="text/javascript">
it worked fine.
 
Share this answer
 
v2
Comments
Member 14574315 6-Apr-21 20:12pm    
i am facing the same problem. can you please share the code snippet in which you have put the js code within the login.php file using . thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900