Solved

Put Value into OnSubmit?

Posted on 2010-09-10
4
405 Views
Last Modified: 2013-12-13
Hi, is it possible to put a value on in the submit line?

<form onSubmit="(put input value here)">
<input value="this is the value">
0
Comment
Question by:eNarc
  • 2
4 Comments
 
LVL 6

Expert Comment

by:DalHorinek
Comment Utility
What exactly do you want to achieve?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
Comment Utility
Check this :

< form onsubmit="eval( document.getElementById('myInput').value );" >
< input id="myInput" type="hidden" value="alert('red alert you are using eval !')" / >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form onsubmit="eval( document.getElementById('myInput').value );">
<input id="myInput" type="hidden" value="alert('red alert you are using eval !')" />
<input type="submit" value="do it!" />
</form>
</body>
</html>

Open in new window

0
 
LVL 2

Assisted Solution

by:johnwarde
johnwarde earned 200 total points
Comment Utility
You can only put javascript (i.e. a javascript expression) in the onSubmit attribute of the form tag and is generally used for user data validation before the form is sent to the server.  Just to be clear that the value in onSubmit (or the value returned from the javascript expression/function) does not get sent to the server.

Below is an an example, the javascript function validateThis() gets called; the "this" javascript keyword is used in the parameter which passes the entire form object i.e. dataform and you can refer any of the fields in that form using the name or id attribute of the input tag i.e. dataform.data1.value returns the value the user typed into the "" field

So you need to put a name attribute onto you input tag and use a similar javascript function


<HTML>
<HEAD>
 <TITLE>On Submit example</TITLE>
 <script>

function validateThis(dataform) { //This is the name of the function

if (dataform.data1.value == "") { //This checks to make sure the field is not empty
   alert("This field is empty.");
   return false; //This prevents the form from being submitted
   } else {
   return true;
   }

}
</script>
</HEAD>
<BODY>
       <FORM METHOD="POST" ACTION="yourfile.php" onSubmit="return validateThis(this)">
<INPUT TYPE="text" NAME="data1" SIZE=10 MAXLENGTH=60 VALUE="">
<INPUT TYPE="submit" NAME="submit" value="submit">
</FORM>
</BODY>
</HTML>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Thanks for the points!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now