ainoy31 Posted July 28, 2008 Share Posted July 28, 2008 Hello- I have a javascript function that validates a form. My scenario is that I am using the form twice in my process. So, on my form, there is an option to select a product attribute. Once they enable this attribute, the page will refresh and there will be a drop down menu on the form. So, I have a function validation that checks to make sure that a product is selected from the drop down menu. if(document.edit_product.primary_rating_product.value == 0) { errmsg += 'You must select a Primary Rating Product\n'; } if( errmsg != '' ) alert( errmsg ); else window.document.edit_product.submit(); } function alertuser(field,msg) { alert(msg); field.focus(); If the user decides they do not want to have the product attribute enabled, they can go back and disable it. The problem is when the save button is clicked, I get the error message of document.edit_product.primary_rating_product has no properties. Much appreciation. AM Quote Link to comment https://forums.phpfreaks.com/topic/117048-script-issue/ Share on other sites More sharing options...
KevinM1 Posted July 28, 2008 Share Posted July 28, 2008 Hello- I have a javascript function that validates a form. My scenario is that I am using the form twice in my process. So, on my form, there is an option to select a product attribute. Once they enable this attribute, the page will refresh and there will be a drop down menu on the form. So, I have a function validation that checks to make sure that a product is selected from the drop down menu. if(document.edit_product.primary_rating_product.value == 0) { errmsg += 'You must select a Primary Rating Product\n'; } if( errmsg != '' ) alert( errmsg ); else window.document.edit_product.submit(); } function alertuser(field,msg) { alert(msg); field.focus(); If the user decides they do not want to have the product attribute enabled, they can go back and disable it. The problem is when the save button is clicked, I get the error message of document.edit_product.primary_rating_product has no properties. Much appreciation. AM This generally means your script is being run before the document (the HTML) is fully loaded. That element 'has no properties' because it hasn't been loaded yet. Stick your code within a window.onload callback, like so: <script type="text/javascript"> window.onload = function() { //script goes here } </script> That will ensure that the HTML is completely loaded before the script attempts to run. Quote Link to comment https://forums.phpfreaks.com/topic/117048-script-issue/#findComment-602041 Share on other sites More sharing options...
ainoy31 Posted July 28, 2008 Author Share Posted July 28, 2008 Thanks for the advice. I tried what you suggested and now I get a message that my function is not defined. Here is what I am trying: window.onload = function validateProductForm() { name = document.edit_product.product.value; thecheckbox = document.edit_product.single_sale.checked; res_recurring = document.edit_product.gl_id_recurring; bus_recurring = document.edit_product.gl_id_recurring_business; errmsg = ''; if( name == '' ) { errmsg += 'You must enter a Product Name\n'; } if(document.edit_product.hosts) { hosts = document.edit_product.hosts.value; if( hosts == '' ) { errmsg += 'You must enter the number of Hosts\n'; } } if( !thecheckbox ) { if( res_recurring.selectedIndex == 0 ) { errmsg += 'You must select a Residential Recurring General Ledger\n'; } if( bus_recurring.selectedIndex == 0 ) { errmsg += 'You must select a Business Recurring General Ledger\n'; } } if(document.edit_product.primary_rating_product.value == 0) { errmsg += 'You must select a Primary Rating Product\n'; } if( errmsg != '' ) alert( errmsg ); else window.document.edit_product.submit(); } function alertuser(field,msg) { alert(msg); field.focus(); } Thanks, AM Quote Link to comment https://forums.phpfreaks.com/topic/117048-script-issue/#findComment-602065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.