Page 1 of 1

JavaScript Function Problem [from onSubmit]

Posted: Fri Feb 17, 2006 9:44 am
by jayshields
Another JS prob...

My function:

Code: Select all

//Define a function to alert the user of cross entered details if necessary
function cross_entry() {
	//If the user has chosen to use an existing make but has entered something in the new make box *OR*
	//If the user has chosen to add a new make but has chosen an existing make from the selection box
	if ((document.getElementById('makechoice').value == 'existing' && document.getElementById('newmake').value != '') ||
		(document.getElementById('makechoice').value == 'addnew' && document.getElementById('make').value != 'none')) {
			//Show a confirmation alert
			return confirm("You have cross-entered make details.\n\nAre you sure you want to use the details corresponding to the radio button choice?");
	}
}
How I call it:

Code: Select all

<form action="<?php echo basename(__FILE__); ?>" method="post" enctype="multipart/form-data" onSubmit="javascript: cross_entry();">
If the user clicks Cancel, I do NOT want the page to submit. If the user clicks OK, the page should submit, but the page is submitting on Cancel aswell as on OK.

I though this would work because the below code only submits the form if the user clicks OK, not when the user clicks Cancel:

Code: Select all

<form name="editrecord" action="' . basename(__FILE__) . '" method="post" onsubmit="return confirm(\'Are you sure?\');">
Have I made a very simple error that someone can point out for me?

Thanks :)

Posted: Fri Feb 17, 2006 10:10 am
by Chris Corbyn
Yes... you've put the word javascript: in your onsubmit attribute :)

You only use that keyword for href's ;) You should have done like the bottom example and put "return functionName()". That's because you pass back the return value of the function to the form handling.

The clearest way to see how that works is to kill a hyperlink using JS.

<a href="somewhere_valid.php" onclick="return false">Click me and nothing will happen... I'm dead</a>

Posted: Mon Feb 20, 2006 12:14 pm
by jayshields
Ahhh. Right.

I'm learning.

Thanks :)