473,574 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which Submit button was pressed?

Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

THank you!
Jul 20 '05 #1
8 14634
> I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.


You could do something along the lines of:

var canSubmit;

...

<form onsubmit="retur n canSubmit;">

<submit onclick="canSub mit = validator1( );" />
Jul 20 '05 #2
try to write a function that will check to see
if the forms are blank or not when the button is pressed.

Can be done with one function or 2 functions. One woulds check all
alternatively create 2 and assign one to each button.

al**@paul.rutge rs.edu (Syed Ali) wrote in message news:<30******* *************** ***@posting.goo gle.com>...
Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

THank you!

Jul 20 '05 #3
al**@paul.rutge rs.edu (Syed Ali) wrote in message news:<30******* *************** ***@posting.goo gle.com>...
Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.


Try using both methods. Have the submit button's onclick() methods
set a global status variable to the result of their check of the
text fields. Have the form's onsubmit() method check that variable.

If you want to have an error message that varies depending
on which button was clicked, have the onclick() methods store
an error message if there's a problem. Otherwise they store
null. The form's onsubmit() would display the error
message, if any, and return false, otherwise return true.
Jul 20 '05 #4
Lee
Syed Ali said:

Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?


You can't check that for sure, because you don't know that the visitor
has JavaScript enabled. If they have JavaScript enabled, you can check
it for their convenience, but you also have to be able to validate on
the server side, so you also need to provide a way for the server to
know which button was pressed.

This works on the client side, and also sends enough information to the
server to allow it to perform the same sort of validation.

Note that I'm taking your specification literally to mean that if the
second button is pressed, that *only* the first field should be filled:
<html>
<head>
<script type="text/javascript">
function validate(f,whic hCase){
var textfieldCount= 0;
var hasValueCount=0 ;
var firstHasValue=0 ;
for(var i=0;i<f.element s.length;i++){
if(f.elements[i].type=="text"){
textfieldCount+ +;
if(-1!=f.elements[i].value.search(/\S/)){
hasValueCount++ ;
if(textfieldCou nt==1){
firstHasValue++ ;
}
}
}
}
if(whichCase==" button1"){ // all must have values
if(hasValueCoun t==textfieldCou nt){
return true;
}else{
alert ("all fields must have values.");
return false;
}
}else{ // only first may have value
if((hasValueCou nt==1)&&(firstH asValue)){
return true;
}else{
alert("the first field (only) must have a value");
return false
}
}
}
</script>
</head>
<body>
<form onsubmit="retur n validate(this,w hichPressed)">
<input><input>< input><input>
<input name="whichSubm it" type="submit" value="button1"
onclick="whichP ressed=this.val ue">
<input name="whichSubm it" type="submit" value="button2"
onclick="whichP ressed=this.val ue">
</form>
</body>
</html>

Jul 20 '05 #5
Lee wrote on 16 dec 2003 in comp.lang.javas cript:
<input name="whichSubm it" type="submit" value="button1"
onclick="whichP ressed=this.val ue">
<input name="whichSubm it" type="submit" value="button2"
onclick="whichP ressed=this.val ue">


If you want the texts on the buttons to be the same, do:
<input name="button1" type="submit" value="Submit me"
onclick="whichP ressed=this.nam e">
<input name="button2" type="submit" value="Submit me"
onclick="whichP ressed=this.nam e">
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #6
@SM
Syed Ali a ecrit :

Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?


<script type="text/javascript"><!--
function verifOne(nameOf Field){
if(nameOfField. value=='') {
alert(nameOfFie ld.name+' not filled');
nameOfField.foc us();
return false;
}
else return true;
}
function verifAll(MyForm ){
ok=0;
for(var i=0;i<MyForm.le ngth;i++) {
if(MyForm.eleme nts[i].type=="text")
if(!(verif1(MyF orm.elements[i])))
{ ok=1; return; }
}
if(ok==0) return true;
else return false;
}
// --></script>

<form action="blabla" etc>
<input type=button value="Submit one field"
onclick="if(ver ifOne(this.form .Firstname)) this.form.submi t();">
<input type=button value="Submit All"
onclick="if(ver ifAll(this.form )) this.form.submi t();">
</form>

Not tried !

-- -----
@SM
move away *OTEZ-MOI* from my reply url
Jul 20 '05 #7
Thank you to everyone who responded.

What worked me for me is setting a global var with -onlick on each
submit button and then checking the value of that global var in
-onSubmit for the form.

For example, submit button1 -onclick sets global var
whichButton=but ton1.
And in -onSubmit function, I check the value of whichButton, if it is
button1 then I know that all textfields need to be filled, if it is
button2 then I make sure that only the 1 needed textfield is filled.

Thanks again.

al**@paul.rutge rs.edu (Syed Ali) wrote in message news:<30******* *************** ***@posting.goo gle.com>...
Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

THank you!

Jul 20 '05 #8
Syed Ali wrote:
What worked me for me is setting a global var with -onlick on each
submit button and then checking the value of that global var in
-onSubmit for the form.

For example, submit button1 -onclick sets global var
whichButton=but ton1.
And in -onSubmit function, I check the value of whichButton, if it is
button1 then I know that all textfields need to be filled, if it is
button2 then I make sure that only the 1 needed textfield is filled.
There is no need to spoil namespaces. Use `whichbutton' as a property
of a DOM object, e.g. the `HTMLFormElemen t' object itself. Or use the
`value' property of a input[type="hidden"] `HTMLInputEleme nt' object
instead.
[Top post]


Please do not waste scarce resources.
PointedEars
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8047
by: Frances Del Rio | last post by:
I have a form in which submit button is an img.. would like to do it w/a rollover, but can't get rollover to work.. can you do rollovers w/img submit buttons? this is code I have: <input type="image" name="log" src="images/login.gif" value="Submit" alt="Submit" onMouseover = "log.src='images/login-roll.gif'" onMouseout...
7
2769
by: M | last post by:
i have a form which i would like to input different "action" url depending on the button that was clicked. is there a way that javascript can prefill a defined action based on the button pressed? thanks!!!
1
10266
by: Frances Del Rio | last post by:
I have a form in which submit button is an img.. would like to do it w/a rollover, but can't get rollover to work.. can you do rollovers w/img submit buttons? this is code I have: <input type="image" name="log" src="images/login.gif" value="Submit" alt="Submit" onMouseover = "log.src='images/login-roll.gif'" onMouseout...
6
2143
by: stellstarin | last post by:
I have a HTML page containing two submit buttons in the same form.When the form is submitted,I want to know through which submit button the form was submitted. Is there any event or property which identifies this?
1
1948
by: josephChiaWY | last post by:
Hi all, I am a newbie in php. I have met a problem in echoing a few submit buttons using while loop. How do i differentiate which submit button is being click in order to grab the correct data. Below are a part of my codes: while($requestMatch = mysql_fetch_array($friendRequest)){ echo "<table width=425 height=130 border=0>"; ...
2
3716
by: F159753 | last post by:
Hi, I have 2 text box and a "search " button. I would like to check if the text boxes are empty when the search button has been pressed. How should I do that? Regards, FF
3
2495
by: Daniel | last post by:
Hi all, I am using .NET 1.1. I have a form with a cancel button, something like this: <form method="post"....> <input type="submit" value="Submit"> <input type="submit" value="Cancel" id="btnCancel"> </form>
8
1703
by: jodleren | last post by:
Hi It is late and I am tired. I cannot remember how to check which of my buttons on the form was pressed. There are all submit's. Like echo "P=".$_POST; // and eventually using isset() But how was it it was?
3
3795
by: webster5u | last post by:
Hi, i have a question about JavaScript. When we click a submit button from the form with multi-submit button. Can JavaScript detect which submit button has been submit? I make a form with 3 submit button (select, update and delete) and i put validation javascript function in onSubmit event handler. May i apply different validation according to...
0
7813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8106
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6461
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5305
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3743
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2251
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1350
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.