JavaScript Function Problem [from onSubmit]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

JavaScript Function Problem [from onSubmit]

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Ahhh. Right.

I'm learning.

Thanks :)
Post Reply