By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Problem with onClick and submit()

P: n/a
I'm currently working on a page the redirects to a necessary page by a
drop-down menu. Currently it is only heading to one of the pages. I'm
not quite sure what I'm doing wrong, but I was hoping someone could
look at my code, and let me know what I'm doing wrong.

-- javascript snippit (in <head> ) -----
<script><!--
function doSubmit()
{
//based on the drop-down value submit to appropriate page
if (document.SelectForm.SelectPage.value = "CustomForm" &&
document.SelectForm.realname.value != "" &&
document.SelectForm.email.value != "" &&
document.SelectForm.PhoneNumber.value != "")
{
document.SelectForm.action = "CustomForms.php";
document.SelectForm.submit();
}
else if(document.SelectForm.SelectPage.value = "CustomReport" &&
document.SelectForm.realname.value != "" &&
document.SelectForm.email.value != "" &&
document.SelectForm.PhoneNumber.value != "")
{
document.SelectForm.action = "CustomReports.php";
document.SelectForm.submit();
}
else
{
alert ("Not all required fields have been entered")
}
}
//-->
</script>
-- End Javascript snippet in <head> ----
---- Begin Javascript in <form> of html ------
<form action="" method="POST" enctype="multipart/form-data"
name="SelectForm" target="_self" id="SelectForm">

<tr>
<th scope="row"><div align="right">*Selection</div></th>
<td><select name="SelectPage" size="1" id="SelectPage">
<option value="CustomForm">Custom Form</option>
<option value="CustomReport">Custom Report</option>
</select></td>
</tr>

</table>

<input name="Submit" type="submit" dir="ltr" lang="en"
Onclick=doSubmit() value="Submit"/>
</div></form>

--- End Javascript in <form> in html ------

Sep 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

mi******@gmail.com wrote:
I'm currently working on a page the redirects to a necessary page by a
drop-down menu. Currently it is only heading to one of the pages. I'm
not quite sure what I'm doing wrong, but I was hoping someone could
look at my code, and let me know what I'm doing wrong.

-- javascript snippit (in <head> ) -----
<script><!--
function doSubmit()
{
//based on the drop-down value submit to appropriate page
if (document.SelectForm.SelectPage.value = "CustomForm" && [snip] else if(document.SelectForm.SelectPage.value = "CustomReport" &&

[snip]

Correct the use of your operators. Change = to == since you're doing a
comparison not an assignment.

See if this fixes your application.

Sep 21 '05 #2

P: n/a
You should use the onsubmit event to validate a form.

Like this:

<form onsubmit="return validate()"

function validate(){
if( elRef.value == "correct value" )
{
return true;
}
else
{
alert("Please update...")
return false;
}
}
http://km0ti0n.blunted.co.uk/

Sep 21 '05 #3

P: n/a
Thank You so much!!! Works great now!!!

Sep 21 '05 #4

P: n/a
Thank You so much!!! It works great!!!

Sep 21 '05 #5

P: n/a
mi******@gmail.com wrote:
Thank You so much!!! Works great now!!!


A common programming practice is to put literals on the left when doing
comparisons. That way if you accidentally have an assignment, you
*will* get an error when the statement is executed.

e.g. in your case:

if (document.SelectForm.SelectPage.value = "CustomForm" ...

will assign the string 'customForm' to the value of the select. The
statement will then be evaluated as 'true' [1].

On the other hand:

if ("CustomForm" = document.SelectForm.SelectPage.value ...

will *always* generate an error that you will have to track down and fix.

So if you follow the convention of writing:

if ("CustomForm" == document.SelectForm.SelectPage.value ...

your bugs will bite sooner rather than later.
1. A small number of browsers under certain conditions will treat the
assignment as a comparison anyway.
--
Rob
Sep 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.