Forum Moderators: open

Message Too Old, No Replies

Using submit() for form without form element name attribute?

The name attribute is invalid but I can't get getElementById to work.

         

JAB Creations

7:10 am on Jul 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In XHTML the name attribute is invalid on form elements yet I am unable to get getElementById to work with submit().

Suggestions to get this to work?

- John

rocknbil

5:51 pm on Jul 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, I don't know why either of those won't work, they should. I don't work in XHTML much, there's really no need to. But below is a working example. I found it strange onSubmit is not supported in the form tag, maybe I just had to use onsubmit instead. In any case, an external function is always better, note window.onload.

Tested against the W3C validator.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- doctype on one line -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>XML Form Test</title>
<script type="text/javascript">
function validate(form) {
var msg = '';
if (document.getElementById('txt').value=='') { msg = 'Fill in the field.'; }
if (msg != '') { alert(msg); }
else { form.submit(); }
return false;
}
window.onload=function () {
if (document.getElementById) {
document.getElementById('test-form').onsubmit = function() {
return validate(document.getElementById('test-form'));
}
}
}
</script>
</head>
<body>
<form action="test.cgi" id="test-form" method="post">
<label for="txt">Just enter something:</label>
<input type="text" name="txt" id="txt" size="24" maxlength="100" value=""/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Fotiman

1:05 am on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




In XHTML the name attribute is invalid on form elements

Invalid, no. Deprecated, yes. I'm not sure what you were trying to do, but I'm guessing rocknbil's answer will probably do what you were looking for.

JAB Creations

2:33 am on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys, I'm not sure what I did wrong (no error messages) but it's working fine now. I think I'll make a back up of the script. ;)

- John

rocknbil

4:02 pm on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In XHTML the name attribute is invalid on form elements

Caught this one but forgot to ask. If the name is deprecated, how does the server-side processor parse the key/value pairs?

janharders

4:18 pm on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think that input-elememts shall be name-less, but the form-element itself. that's fine, I think. You won't get it on the server-side anyway and id is much better suited for client-side scripting.

Fotiman

10:34 pm on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Right, name is still used on inputs, just not on the form element (that is, its deprecated on the form element).