Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/117048-script-issue/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/117048-script-issue/#findComment-602041
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/117048-script-issue/#findComment-602065
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.