Forum Moderators: open
So, my form fields are:
<input class="inputbox" type="file" name="user_files[]">
<input class="inputbox" type="file" name="user_files[]">
<input class="inputbox" type="file" name="user_files[]">
I really don`t know javascript, but this is what I came up with based on some code I found on a tutorial site:
function checkuploads()
{
for (i=0; i<document.userform[0].user_files.length; i++)
{
if (document.userform[0].user_files[i].value!= '')
{
user_input = document.userform[0].user_files[i].value;
}
}
if (user_input)
{
return false;
}
}
The error I get is something like document.userform[0].user_files.length is null or not an object.
Can anyone point me in the right direction?
Many thanks.
Next, do you have more than one FORM defined with the name "userform"? If not, then change the javascript to:
function checkuploads()
{
for (i=0; i<document.userform.user_files.length; i++)
{
if (document.userform.user_files[i].value!= '')
{
user_input = document.userform.user_files[i].value;
}
}
if (user_input)
{
return false;
}
}
Please post back here if that helps or not.
Unfortunately, the square brackets after the variable name tell PHP that's what makes it an array. ( How do I create arrays in a HTML <form>? [php.net] ) The link provided shows a number of different options for javascript processing as well, including some good stuff in the User Contributed notes. Something like this may work...
function checkuploads(form) {
for (i=0; i<form.elements['user_files[]'].length; i++) {
if (form.elements['user_files[]'][i].value!= '') {
return true;
}
}
return false;
}
</script>
...
<form method="post" enctype="multipart/form-data" onsubmit="checkuploads(this);">
Never thought to check the PHP.net website, thanks coopster. Based on your code and what I read there, finally got it working:
function checkuploads(form)
{
var file;for (i=0; i<form.elements['user_files[]'].length; i++)
{
if (form.elements['user_files[]'][i].value!= '')
{
file += form.elements['user_files[]'][i].value;
}
}
if (file)
{
return true;
}
else
{
alert('Please choose at least one file');
return false;
}
}
Thanks again! :)
Here's some jibbering on the topic:
[jibbering.com ]
>>Square bracket chars aren't valid for id or name attributes.
id attributes I agree, but you'll have to sell me on the name attribute. PHP requires you use the name [w3.org] attribute in this manner, not the id attribute.
The DTD specifies the syntax of HTML element content and attribute values using SGML tokens (e.g., PCDATA, CDATA, NAME, ID, etc.). The name attribute is defined in the DTD as type CDATA [w3.org], which is a sequence of characters from the document character set and may include character entities.
It is very confusing to me as well, but it seems there is a difference here.
[w3.org ]
..as DrDoc has already quoted.
Whether validation of this sort actually matters is another issue.
The link you provided, which is the quote mentioned by DrDoc earlier, refers to the ID and NAME tokens. Roll back in that link, one paragraph, to see the CDATA token. Now click the link I showed earlier, the name attribute. See it's token? CDATA, not ID and not NAME. Big difference here since CDATA is not restricting us to the character set mentioned in ID and NAME tokens.
It says that ID & NAME are type, CDATA. Then it says:
For some HTML 4 attributes with CDATA attribute values, the specification imposes further constraints on the set of legal values for the attribute that may not be expressed by the DTD.
"not expressed by the DTD" - hmmm
Under the same bullet point, STYLE & SCRIPT are mentioned. The specification that imposes our possible restriction isn't under the same bullet, but the next.
ID and NAME tokens must begin with a letter ([A-Za-z]) and...
On the one hand, you'd expect (via usual "list logic") this specification to be part of a sub-list of the previous bullet, if it is linked to the first quote. On the other hand, however located, the statement plainly says "No, you can't do that" - just like many "well-respected" sources appear to think.
BUT, I'm starting to have doubts, and think you are right..
I'm getting confused, possibly, by name as an attribute, and name as a type of token...and the same with id too (Surely they could have highlighted this trap).
name = cdata [w3.org] [CI] (*source) [w3.org]
id = name [w3.org] (*source) [w3.org]
As you say, only ID is restricted, as it has token type name. Whilst NAME having token type CDATA doesn't have this restriction.
or...