Solved

Put Value into OnSubmit?

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

732 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