Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 12th, 2009, 02:35 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default Submit form data from a Javascript message box


Chaps,

I have been working on a JavaScript message box for want of a better word to essentially pop up for informational tasks.

What i have been trying to do is add a form within the message box which transfers the data to a new page.

The frustration is that my rambling code does work in Explorer but not in Firefox.

Firstly i have some code for controlling a Div element which produces the message box

Code:
<script language="JavaScript" type="text/javascript">
<!--
(document.getElementById) ? dom = true : dom = false;
function hideIt() {
  if (dom) {document.getElementById("layer1").style.visibility='hidden';}
}
function showIt() {
  if (dom) {document.getElementById("layer1").style.visibility='visible';}
}
// -->
</script>


I then give the Div some style - No pun intended

<style>
div.visibleMsg {
position: absolute;
top: 300px;
left: 300px;
width: 300px;
background: #6382d2 url("/images/icons/info-i-icon.gif");
background-repeat:no-repeat;
color: #FFF;
border: 1px solid #3a5daf;
border-right-width: 2px;
border-bottom-width: 2px;
padding-left:30px;
padding-right:3px;
padding-bottom:10px;
padding-top:3px;
text-align:left;
visibility:hidden;
}
</style>


The Div Element and Form

Code:
<div id="layer1" class="visibleMsg">
<form name="form" onSubmit="SendData(this.form);return false;">
<br><b>Please Change Name</b><br><br>
Old Name <input type="text" name="txt1" value=""><br>
New Name <input type="text" name="txt2" value=""><br>
<input type="image"  src="images/buttons/b-update.png">
</form>
</div>


Then i have tried as best to create a function to transfer the form data to a new page

Code:
<script language="javascript" type="text/javascript">
function SendData (form){
var oldNm = document.all.txt1.value;
var NewNm = document.all.txt2.value;
window.navigate("default.asp?on="+oldNm+"&nn="+NewNm+"");
}
</script>


Lastly for testing purposes i am just using a form button to trigger the showIt function

Code:
<form><input type="button" value="Open pop-up" onClick="showIt()"></form>


What i am trying to achieve is to transfer the data to a new page which is IE and Mozz compatible.

I hope my ramblings make sense. Any thoughts greatly appreciated

Cheers

Aspless
 
Old June 12th, 2009, 03:32 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Aspless,

I assume you can see the form and enter the details OK but the form isn't submitting properly?

window.navigate is an IE-only function, and has never been properly implemented Firefox. You should use window.location = "http://..." instead.
See https://bugzilla.mozilla.org/show_bug.cgi?id=310191

HTH
Phil

P.S. You may want to consider giving the text boxes ids and in SendData() use document.getElementById("txt1") instead of document.all.txt1 - another standards thing
The Following User Says Thank You to philip_cole For This Useful Post:
aspless (June 15th, 2009)
 
Old June 12th, 2009, 06:50 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

document.all is *STRICTLY* an MSIE "feature" (or, rather, lack thereof).

The only browsers that *NEED* to use document.alll are MSIE 4 and earlier. Unless you really need to support 1999-vintage browsers, stop using document.all.

Instead, use ONLY document.getElementById(...)

But you don't need to use EITHER one of those here! You passed along the form, so why not USE it??

Well...you would have, but you goofed a little.

So:
Code:
<form name="form" onSubmit="SendData(this);return false;">

<script language="javascript" type="text/javascript">
function SendData (form)
{
    var oldNm = form.txt1.value;
    var NewNm = form.txt2.value;
    location.href = "default.asp?on=" + escape(oldNm) + "&nn=" + escape(NewNm)
}
</script>


Now explain to me how this is any different than simply letting the <FORM> be submitted????????
The Following User Says Thank You to Old Pedant For This Useful Post:
aspless (June 15th, 2009)
 
Old June 12th, 2009, 06:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
P.S. You may want to consider giving the text boxes ids and in SendData() use document.getElementById("txt1") instead of document.all.txt1 - another standards thing
I don't get it. Why??

Field names are not only standard, they are *REQUIRED* if you want a form field to be submitted to the next page. Try it some time: Omit the name= from a form field sometime and then try to find it in your PHP/ASP/JSP code.

And if you give a form field a name, then the ID is *TOTALLY UNNECESSARY*.

Instead of using document.getElementById("xxx") you can just do document.FormName.xxx. If you need the form name and/or field name to be in variables, you just do document.forms[formname].elements[fieldname] (though there are shorter variants of those).
 
Old June 13th, 2009, 06:40 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Cool

The point I was making was that as you said yourself:
Quote:
Originally Posted by Old Pedant View Post
document.all is *STRICTLY* an MSIE "feature"
I simply suggested using document.getElementById to get rid of document.all as it is pretty much the direct equivalent and is used elsewhere in the script. As always there are a dozen ways to do anything - you could use document.getElementsByTagName('input')[1] and avoid setting any names or ids altogether if you reeeeally wanted to. But yes, your solution of using document.form is probably a couple of ticks quicker than mine.

And where exactly did I say to remove the name attributes??
 
Old June 14th, 2009, 03:36 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

No no...I overenthused. I just meant that *IF* you give a form field a NAME attribute--and you have to if you want the field to be submitted to the next page via the form posting--*THEN* there is no need to *ALSO* give it an ID.

There are, of course, times when you want a form field but don't want it to be submitted, and then of course using an ID alone gives you all you want. And of course for non-form-fields the ID is the only way to go.

I think we are in violent agreement. Just I wasn't careful in my wording.
 
Old June 15th, 2009, 06:10 AM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default

Guys,

Thanks again for the assisstance.

Pleased to say the example Old Pedant provided and the correct 'location.href =' navigation has solved my issue.

Your further conversation has proved that i need to understand element control and the effective use of ID's more.

Cheers


Aspless





Similar Threads
Thread Thread Starter Forum Replies Last Post
Submit Form with QueryString Data Phil06 Javascript 4 June 15th, 2007 06:11 PM
submit form using javascript with html:button 752004 Struts 2 November 23rd, 2006 12:39 AM
Message Box (Show if form not filled out) misskaos Classic ASP Basics 38 October 10th, 2006 02:05 PM
Javascript Confirm Message box itHighway Javascript How-To 5 September 25th, 2006 02:51 AM
Javascript submit form to popup window don_t Javascript 1 December 23rd, 2005 07:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.