Solved

Put Value into OnSubmit?

Posted on 2010-09-10
4
413 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
ID: 33647784
What exactly do you want to achieve?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 33647814
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
ID: 33647914
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
ID: 33650118
Thanks for the points!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

830 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