Solved

Put Value into OnSubmit?

Posted on 2010-09-10
4
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

688 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