/    Sign up×
Community /Pin to ProfileBookmark

Display message of details in form

[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>

<html>
<head>
<title>JavaScript</title>

<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-15″>

<!– The external style sheets –>
<link rel=”stylesheet” type=”text/css” href=”mystyle.css”>

<meta name=”keywords” content=”CO332″>
<meta name=”description” content=”CO332 Exercise Website”>
<meta name=”author” content=”Bradley Berwick”>

<script type=”text/javascript” src=”myjavascript.js”></script>
</head>

<body>

<div id=”container”>

<div id=”header”>
<h1>
Exercise Website
</h1>
</div>

<div id=”leftnav”>
<ul>
<li><a href=”index.html” >Home</a>
<li><a href=”myjavascript.html” ><span class=”currentpage”>Services</span></a>
<li><a href=”#” >TBA</a>
<li><a href=”#” >TBA</a>

</ul>
</div>

<div id=”content”>
<h2>About Yourself</h2>

<form name=”exampleform”>
<h3>Name: <input name=”yourname” type=”text”></h3>

<h3>Age: <input name=”age” type=”text” size=”3″
Onchange = ‘
if ( !( this.value > 0 && this.value < 150 ) ) {
alert( this.value + ” is not a valid age.”);
this.value=””; // clear age text box
}’ > </h3>

<h3>Gender:
<input name=”gender” type=”radio” value=”male”> Male
<input name=”gender” type=”radio” value=”female”> Female
</h3>

<h3>Your favourite colour:
<select name=”colours”>
<option value=”None”>
<option value=”red”>Red
<option value=”green”>Green
<option value=”blue”>Blue
<option value=”pink”>Pink
<option value=”orange”>Orange
<option value=”purple”>Purple
</select>
</h3>
<h3>Your hobbies are: <br>
<input name=”running” type=”checkbox”> Running <br>
<input name=”football” type=”checkbox”> Football <br>
<input name=”golf” type=”checkbox”> Golf <br>
<input name=”cycling” type=”checkbox”> Cycling
</h3>

<textarea rows=”10″ cols=”30″>
Add comments.
</textarea>

<p>
<input type=”reset” value=”Clear”>
&nbsp; &nbsp;

<input name=”submit” type=”button” value=”Submit”
onclick=’displayMessage(“hey”)’;>
</p>

</form>

</div>

<div id=”footer”>
<div id=”mailto”> Last updated on 28th Oct 2011<br>
Maintained by Bradley Berwick
</div>
</div>

</div>

</body>
</html>
[/code]

[code]
function displayMessage(msg) {
// Open a new window
var msgWindow = window.open(”, ‘Message’);

// Write message in the new Window
msgWindow.document.write(msg);
msgWindow.document.close();

// Raise this window, in case it’s not visible
msgWindow.focus();
}

function submitForm(add vars) {

displayMessage(msg);
}
[/code]

When I click the Submit button on the form, I want an alert box showing all the details that the user has entered into the form.

I have a function called displayFunction which I want to use in another function called submitForm, but I’m not sure what to write in the submitForm function.

Help is appreciated.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoNov 25.2011 — an alert box? because the code you have at the moment puts it in a new window.

but anyway, if you just want an alert, the below code is easy enough to adapt:

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
<title>JavaScript</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15">

<!-- The external style sheets -->
<link rel="stylesheet" type="text/css" href="mystyle.css">

<meta name="keywords" content="CO332">
<meta name="description" content="CO332 Exercise Website">
<meta name="author" content="Bradley Berwick">


</head>


<body>

<div id="container">

<div id="header">
<h1>
Exercise Website
</h1>
</div>

<div id="leftnav">
<ul>
<li><a href="index.html" >Home</a>
<li><a href="myjavascript.html" ><span class="currentpage">Services</span></a>
<li><a href="#" >TBA</a>
<li><a href="#" >TBA</a>

</ul>
</div>


<div id="content">
<h2>About Yourself</h2>


<form name="exampleform">
<h3>Name: <input name="yourname" type="text"></h3>

<h3>Age: <input name="age" type="text" size="3"
Onchange = '
if ( !( this.value > 0 && this.value < 150 ) ) {
alert( this.value + " is not a valid age.");
this.value=""; // clear age text box
}' > </h3>


<h3>Gender:
<input name="gender" type="radio" value="male"> Male
<input name="gender" type="radio" value="female"> Female
</h3>

<h3>Your favourite colour:
<select id="color" name="colours">
<option value="None">
<option value="red">Red
<option value="green">Green
<option value="blue">Blue
<option value="pink">Pink
<option value="orange">Orange
<option value="purple">Purple
</select>
</h3>
<h3>Your hobbies are: <br>
<input name="hobby" id="running" type="checkbox"> Running <br>
<input name="hobby" id="football" type="checkbox"> Football <br>
<input name="hobby" id="golf" type="checkbox"> Golf <br>
<input name="hobby" id="cycling" type="checkbox"> Cycling
</h3>

<textarea rows="10" cols="30" id="comments">
Add comments.
</textarea>

<p>
<input type="reset" value="Clear">
&nbsp; &nbsp;

<input name="submit" type="button" value="Submit"
onclick='displayMessage()';>
</p>




</form>

</div>

<div id="footer">
<div id="mailto"> Last updated on 28th Oct 2011<br>
Maintained by Bradley Berwick
</div>
</div>

</div>

<script type="text/javascript">
var hobstring="";
function displayMessage() {
var nam=document.exampleform.elements["yourname"].value;
var age=document.exampleform.elements["age"].value;

var gend = document.getElementsByName('gender');
for (var i = 0; i < gend.length; i++) {
if (gend[i].checked==true) {
var sex=gend[i].value;
}
}
var sel=document.getElementById('color');
var color=sel.options[sel.selectedIndex].value;

var hobbs = document.getElementsByName('hobby');
for (var a = 0; a < hobbs.length; a++) {
if (hobbs[a].checked==true) {
hobstring+=hobbs[a].id+", ";
}
}
var msg="Name: "+nam+"<br>Age: "+age+"<br>Gender: "+sex+"<br>Favorite color: "+color+"<br>Hobbies: "+hobstring;
// Open a new window
var msgWindow = window.open('', 'Message');

// Write message in the new Window
msgWindow.document.write(msg);
msgWindow.document.close();

// Raise this window, in case it's not visible
msgWindow.focus();

}

function submitForm() {

displayMessage();
}</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@ryan1234authorNov 25.2011 — Thanks. I meant a window sorry.
Copy linkTweet thisAlerts:
@ryan1234authorNov 25.2011 — When I click submit nothing is happening :S
Copy linkTweet thisAlerts:
@xelawhoNov 25.2011 — what do you want to happen?

in the three browsers I checked in, it opens a new window with the details of what has been entered in the form.

there's nothing in your original code that would actually submit a form to a server
Copy linkTweet thisAlerts:
@ryan1234authorNov 25.2011 — A new window doesn't open. The JavaScript I put a file called myjavascript.js and link it in my header.

<i>
</i>var hobstring="";
function displayMessage() {
var nam=document.exampleform.elements["yourname"].value;
var age=document.exampleform.elements["age"].value;

var gend = document.getElementsByName('gender');
for (var i = 0; i &lt; gend.length; i++) {
if (gend[i].checked==true) {
var sex=gend[i].value;
}
}
var sel=document.getElementById('color');
var color=sel.options[sel.selectedIndex].value;

var hobbs = document.getElementsByName('hobby');
for (var a = 0; a &lt; hobbs.length; a++) {
if (hobbs[a].checked==true) {
hobstring+=hobbs[a].id+", ";
}
}
var msg="Name: "+nam+"&lt;br&gt;Age: "+age+"&lt;br&gt;Gender: "+sex+"&lt;br&gt;Favorite color: "+color+"&lt;br&gt;Hobbies: "+hobstring;
// Open a new window
var msgWindow = window.open('', 'Message');

// Write message in the new Window
msgWindow.document.write(msg);
msgWindow.document.close();

// Raise this window, in case it's not visible
msgWindow.focus();

}

function submitForm() {

displayMessage();
}


<i>
</i>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;

&lt;html&gt; <br/>
&lt;head&gt;
&lt;title&gt;JavaScript&lt;/title&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"&gt;

&lt;!-- The external style sheets --&gt;
&lt;link rel="stylesheet" type="text/css" href="mystyle.css"&gt;

&lt;meta name="keywords" content="CO332"&gt;
&lt;meta name="description" content="CO332 Exercise Website"&gt;
&lt;meta name="author" content="Bradley Berwick"&gt;

&lt;script type="text/javascript" src="myjavascript.js"&gt;&lt;/script&gt;
&lt;/head&gt;


&lt;body&gt;

&lt;div id="container"&gt;

&lt;div id="header"&gt;
&lt;h1&gt;
Exercise Website
&lt;/h1&gt;
&lt;/div&gt;

&lt;div id="leftnav"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="index.html" &gt;Home&lt;/a&gt;
&lt;li&gt;&lt;a href="myjavascript.html" &gt;&lt;span class="currentpage"&gt;Services&lt;/span&gt;&lt;/a&gt;
&lt;li&gt;&lt;a href="#" &gt;TBA&lt;/a&gt;
&lt;li&gt;&lt;a href="#" &gt;TBA&lt;/a&gt;

<i> </i> &lt;/ul&gt;
&lt;/div&gt;


&lt;div id="content"&gt;
&lt;h2&gt;About Yourself&lt;/h2&gt;


&lt;form name="exampleform"&gt;
&lt;h3&gt;Name: &lt;input name="yourname" type="text"&gt;&lt;/h3&gt;

<i> </i>&lt;h3&gt;Age: &lt;input name="age" type="text" size="3"
<i> </i>Onchange = '
<i> </i>if ( !( this.value &gt; 0 &amp;&amp; this.value &lt; 150 ) ) {
<i> </i>alert( this.value + " is not a valid age.");
<i> </i>this.value=""; // clear age text box
<i> </i>}' &gt; &lt;/h3&gt;


&lt;h3&gt;Gender:
&lt;input name="gender" type="radio" value="male"&gt; Male
&lt;input name="gender" type="radio" value="female"&gt; Female
&lt;/h3&gt;

&lt;h3&gt;Your favourite colour:
&lt;select name="colours"&gt;
&lt;option value="None"&gt;
&lt;option value="red"&gt;Red
&lt;option value="green"&gt;Green
&lt;option value="blue"&gt;Blue
&lt;option value="pink"&gt;Pink
&lt;option value="orange"&gt;Orange
&lt;option value="purple"&gt;Purple
&lt;/select&gt;
&lt;/h3&gt;
&lt;h3&gt;Your hobbies are: &lt;br&gt;
&lt;input name="running" type="checkbox"&gt; Running &lt;br&gt;
&lt;input name="football" type="checkbox"&gt; Football &lt;br&gt;
&lt;input name="golf" type="checkbox"&gt; Golf &lt;br&gt;
&lt;input name="cycling" type="checkbox"&gt; Cycling
&lt;/h3&gt;

&lt;textarea rows="10" cols="30"&gt;
Add comments.
&lt;/textarea&gt;

&lt;p&gt;
&lt;input type="reset" value="Clear"&gt;
&amp;nbsp; &amp;nbsp;

<i> </i>&lt;input name="submit" type="button" value="Submit"
<i> </i> onclick='displayMessage()';&gt;
&lt;/p&gt;




&lt;/form&gt;

&lt;/div&gt;

<i> </i>&lt;div id="footer"&gt;
<i> </i>&lt;div id="mailto"&gt; Last updated on 28th Oct 2011&lt;br&gt;
<i> </i> Maintained by Bradley Berwick
<i> </i>&lt;/div&gt;
<i> </i>&lt;/div&gt;

&lt;/div&gt;


&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ryan1234authorNov 25.2011 — I put your code in another html file and it does work
×

Success!

Help @ryan1234 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...