Solved

use javascript to submit a form

Posted on 2010-08-17
9
376 Views
Last Modified: 2013-11-19
On my page I have a custom rollover button and I want to use onclick to submit a form. Here is what i have:

<a href="" class="button" onclick="enrAdSubmit()" ></a>

<script type="text/javascript">
function enrAdSubmit() {
document['enrAd'].submit();
}
</script>

but this doesn't seem to be working. Any ideas what I'm doing wrong here?

Thanks
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Product Spotlight Submission</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

	<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
    
<!-- CKEditor include files -->
	<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
	<link href="ckeditor/_sample/sample.css" rel="stylesheet" type="text/css"/>
<!-- End CKEditor include files -->
    
<script type="text/javascript">
//function CKeditor_OnComplete() {
  //  iframes = document.getElementsByTagName("iframe");
    //if ( window.addEventListener ) // FireFox and other browsers of the type
      //  iframes[0].contentWindow.addEventListener('keypress', makePreviewBody, false);
   // else if ( window.attachEvent ) // The root of all evil (IE)
     //   iframes[0].contentWindow.document.body.attachEvent('onkeypress', makePreviewBody);
//}
</script>
    
    <!-- create title preview -->
<script type="text/javascript">
	function makePreview(divId,txtField){
	document.getElementById( divId ).innerHTML=document.enrAd[txtField].value;
	}
</script>
	<!-- end create title preview -->
    
        <!-- create body preview -->
<script type="text/javascript">
	function makePreviewBody(){
	document.getElementById('preview_body').innerHTML=CKEDITOR.instances.editor1.getData();
	}
</script>
	<!-- end create body preview -->
    
<script type="text/javascript">

</script>
    
<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $('#submit').hover(
            function(){ // Change the input image's source when we "roll on"
                $(this).attr({ src : 'product_images/submit_button_on.jpg'});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : 'product_images/submit_button_off.jpg'});             }
        );
    });
</script>


<style type="text/css">
<!--
.title_overlay {
	background-image: url(product_images/title_field.jpg);
	height: 26px;
	width: 290px;
	overflow: hidden;
	z-index: 1000;
	padding-top: 4px;
	background-repeat: no-repeat;
	padding-left: 10px;
}
.title_field {
	font-size: 14px;
	font-weight: bold;
	width: 280px;
	border: none;
	background: none;
	background-image: none;
}
.body_field {
	border: none;
	background: none;
	background-image: none;
	font-size: 14px;
	font-weight: normal;
	width: 360px;
}
.body_overlay {
	background-image: url(product_images/text_field.jpg);
	background-repeat: no-repeat;
	height: 180px;
	width: 280px;
	padding-top: 10px;
	padding-right: 11px;
	padding-bottom: 10px;
	padding-left: 9px;
}
.cke_contents {
height: 150px !important;
width: 300px !important;
}
.center_n_justify {
	padding-right: 5%;
	padding-left: 5%;
	padding-top: 10px;
	padding-bottom: 15px;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: normal;
	color: #000000;
	text-decoration: none;
	width: 250px;
	text-align: justify;
}
.preview_title {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-style: normal;
	font-weight: bold;
	color: #000000;
	text-decoration: none;
	text-align: left;
	margin: 0px;
	clear: both;
	line-height: normal;
	padding-top: 8px;
	padding-right: 0px;
	padding-bottom: 0px;
	padding-left: 0px;
}
.center_entrybox {
	padding-top: 10px;
	padding-right: 5px;
	padding-bottom: 10px;
	padding-left: 5px;
}
.preview_body {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: normal;
	color: #000000;
	margin: 0px;
	padding: 0px;
	clear: both;
	line-height: normal;
}
p {
	margin: 0px;
	padding: 0px;
}
a.button {
display: block;
width: 185px;
height: 30px;
padding: 0px 0px 0px 0px;
color:#666666;
text-decoration: none;
background:url(product_images/submit_button.jpg) no-repeat 0px 0px;
}
a.button:hover {
color:#333333;
background:url(product_images/submit_button.jpg) no-repeat 0px -30px;
}
-->
</style>
</head>

<body>

  <table width="595" border="1" cellspacing="0" cellpadding="0" onClick="makePreviewBody();makePreview('preview_title','title_field');">
    <tr>
      <td width="307" height="200">
      <div class="center_entrybox">
      <form id="enrAd" name="enrAd" method="post" action="email_sender.asp">
  <div class="title_overlay">
    <div>
      <label>
      <input name="title_field" type="text" class="title_field" id="title_field" onChange="makePreviewBody();makePreview('preview_title','title_field');"/>

      </label>
    </div>
  </div><br>

  <textarea class="ckeditor" cols="30" id="editor1" name="editor1" rows="10" onKeyUp="makePreviewBody();" >Enter the body text of your ad here.</textarea>
    <br>
    <br>
    <input type="image" name="submit" id="submit" src="product_images/submit_button_off.jpg">
    <a href="" class="button" onclick="enrAdSubmit()" ></a>

    <br />
  <br />
</form>      </td>
      <td width="282" valign="top"><div class="center_n_justify">
        <p><img src="product_images/line_break.jpg" width="250" height="3" /><br />
        <div class="preview_title" id="preview_title"></div>
        <div class="preview_body" id="preview_body"></div>
        <br />
          <img src="product_images/line_break.jpg" alt="" width="250" height="3" /></p>
      </div>
    </div>
      </td>
    </tr>
</table>

<script type="text/javascript">
function enrAdSubmit() {
document['enrAd'].submit();
}
</script>

</body>
</html>

Open in new window

0
Comment
Question by:elliottbenzle
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:mahadazad
ID: 33460920
as you have also specified the name attribute you can do this:
document.enrAd.submit();
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33460930
Use :

document.form[0].submit();
or
document.form["enrAd"].submit();
or
document.getElementById("enrAd").submit();
or
$("#enrAd").submit(); //jQuery
or
$("form[name='enrAd']").submit(); //jQuery

<a href="" class="button" onclick="document.form["enrAd"].submit();" ></a>

Open in new window

0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33460952
It's not working. Try the link below. The page just reloads.

http://www.glowfishtw.com/emailsend/enr_classified_ads.html

(it's the bottom button, not the top one)
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 82

Expert Comment

by:leakim971
ID: 33460961
you put a space before enrad and iT Is NOt the rIgHT case : enrAd

< a href="" class="button" onclick="document.form[" enrad"].submit();"="" >< /a >
< a href="" class="button" onclick="document.form["enrAd"].submit();" >< /a >
0
 
LVL 3

Accepted Solution

by:
mahadazad earned 250 total points
ID: 33460978
actually this is the problem:   onclick="document.form["enrAd"].submit();" in line:
    <a href="" class="button" onclick="document.form["enrAd"].submit();" ></a>

you have enclosed enrAd in double quotes which is already enclosed in double quotes of onclick attribute change you code to:

    <a href="" class="button" onclick="document.form['enrAd'].submit();" ></a>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33461003
@mahadazad good eye
0
 
LVL 3

Expert Comment

by:mahadazad
ID: 33461004
@leakim971: Thanks :D
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 33461656

    <a href="#" class="button" onclick="document.form['enrAd'].submit(); return false" ></a>

IMPERATIVE!
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33489796
Thank you. It was both the # and the ' (single quotes) wish I could give two 500pts.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

696 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