Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update form value on submit

Status
Not open for further replies.

macca2424

Programmer
Jul 23, 2007
21
0
0
GB
Hi i have a form below, i have some javascript to validate if the text box is empty.

what i need to do is

if the text box "qtext" is empty show alert

if the text box "qtext" is not empty and the value = 8 then update the value of the form element "answer" to "1"
then submit the form

else set the text box "qtext" to value of "0" and submit the form

**form**

<input type="text" name="qtext" />
<input id="qone" name="Answer" type="HIDDEN"/>
<input type="submit" value="Submit" onclick="return TextType();"


**current javascript**

function TextType()
{

if ((form1.qText.value.length < 1) || (form1.qText.value.length > 1))
{

mesg = "You have not answered the question!\n"
mesg = mesg + "Please select an image"
alert(mesg);

return (false);
}

return (true);
}
 
- Remove your "onclick" event from the submit button, and move it to the form, changing "onclick" to "onsubmit"

Code:
function TextType() {
	var qText = document.forms['form1'].elements['qtext'].value;

	if (qText == '') {
		alert('You have not answered the question!\nPlease select an image');
		return (false);
	} else if (qText == '8') {
		document.forms['form1'].elements['answer'].value = '1';
		return (true);
	} else {
		document.forms['form1'].elements['qtext'].value = '0';
		return (true);
	}
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi

Is it XHTML ? If yes, make that [tt]type="hidden"[/tt] lowercase, if not, remove the slashes ( / ) from the tag's ends.

Do yourself a favor and indent your code. And do a favor for us and enclose your posted code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] tags.
HTML:
<input type="text" name="qtext" />
<input id="qone" name="Answer" type="HIDDEN"/>
<input type="submit" value="Submit" onclick="return TextType();"[red]>[/red]
JavaScript:
function TextType()
{

  if (([red]document.[/red]form1.q[red]t[/red]ext.value.length < 1) || ([red]document.[/red]form1.q[red]t[/red]ext.value.length > 1))
  {

    mesg = "You have not answered the question!\n"
    mesg = mesg + "Please select an image"
    alert(mesg);

    return (false);
  }

  return (true);
}
Assuming that your [tt]form[/tt]'s [tt]name[/tt] is form1 and you have no global variable with the same form1 name.

Feherke.
 
Hi thanks,

i can not make the "onclick" to "onsubmit" as my form is dynamic and changes this needs to be button onclick.

what could i do to use the onclick
 
i can not make the "onclick" to "onsubmit"

You will not be able to cancel the form submission unless you do this.

The fact your form is "dynamic" is not in the least bit relevant, as far as I can see, to why you cannot move the "onclick" to an "onsubmit" on the form tag itself (which will always be present).

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
ok when i add the on submit code like below, my asp main content does not get displayed, i cant see why

<form name="form1" method="post" action="ModuleQuestions.asp?ques=<% Response.Write nextpag %>"onSubmit="return <% Response.Write QuesQuery("ValidateID") %>();">
 
Nor can I... because server-side code here is as much use as a chocolate teapot / a fart in a thunderstorm / breasts on a bull *

How about trying to view the client-side code and see what you're delivering to the browser.

* delete as appropriate

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
well it works when i use it as a onclick event, all the serverside code does is pull the name of the javascript function, which has to change depending on what is displayed.

so

<form name="form1" method="post" action="ModuleQuestions.asp?ques=<% Response.Write nextpag %>"onSubmit="return <% Response.Write QuesQuery("ValidateID") %>();">

is actually

<form name="form1" method="post" action="ModuleQuestions.asp?ques=<% Response.Write nextpag %>" onSubmit="return TextType();">
 
i have 10 dynamic pages with 4 differnt types of form elements each requires a different validation

so can i have the onsubmit pointing to a function, then in that function can i specify the 4 different validations

but then how would i know which validation is required?
 
so if i set a variable called ValValue which equals what i have in my database ie the function name (TextType)

then jsut use that variable in the form?

so it would be

Code:
<form name="form1" method="post" onsubmit="return ('ValValue');">
 
Why not try it and find out? Do some thinking about what you want and how you might achieve it. It's no fun if you get others to do all of your work for you, and you'll never learn, either.

Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan

thanks for yuor help, just need to know if what i surgest is a good way of doing it, if so i will play around with it.

so..

have my 4 different javascript functions, then call then using my new variable. would this be a good way?
 
yes but as i was saying the value "ValValue" will change dyamically from my database, depending what page is being displayed, the "ValValue" will equal the name of the function.

so in my table i will have a column called functions with a list of the functions and a content column with my html form element
 
ok i just thought can i have a different form on each page which i then can use static functions.

but then i have a server include with my submit button on.

so how can i submit the form from my include, would a onclick submit event work?
 
o yes forgot to say the include with the submit button will be outside the form tags
 
yes but as i was saying the value "ValValue" will change dyamically from my database

Yes - but as [!]I[/!] said, you're missing a function name. The parameter you're passing into the function may well be pulled from a DB, but you've got to at least have a function name to call. Your example did not have this. You cannot call a function with no name like that.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
i dont understand

if i have a function called formcheck

you would use onsubmit="return formcheck();"

so what i am trying to do is have

onsubmit="return "ValValue"();" and the ValValue will be formcheck in the database.

if this cant work, what else could i do?
 
The way you coded it, it looks like you're passing a string parameter into a function with no name. If you'd intended "ValValue" to be a function, you wouldn't have it in quotes. Like this:

Code:
onsubmit="return ValValue();"

Not as you had it:

Code:
onsubmit="return ('ValValue')"

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top