Click here to Skip to main content
14,733,013 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 4: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)?
Richard MacCutchan 27-Sep-20 9:23am
   
I am really getting the feeling that either you do not understand what we are asking, or you do not understand the relationship between these files when they are being used in an actual web application. It does not matter what folder they are stored in they must all be read together by the server application, and the HTML and JS must be passed together to the browser for it to work.
Captain Network 27-Sep-20 9:25am
   
when i put js code within login.php , it worked
Dave Kreskowiak 27-Sep-20 10:59am
   
What people are asking you about "include" the .js file, they are referring to the PHP generated HTML. Does that HTML have tags in it that loads the .js file?
    <script type="text/javascript" src="blahblah.js" />

Oh, and tags belong in the HTML <head> tag.

The .js code is not executed by the PHP file on the server-side. It has to be downloaded by the client and executed on the client-side.
Richard MacCutchan 27-Sep-20 11:18am
   
It's amazing how things work when done correctly.

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

JS:
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:
<!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>
   
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.
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.
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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