Solved

Put Value into OnSubmit?

Posted on 2010-09-10
4
412 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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