...

View Full Version : File Name and Extension To An Alert



flapjacksmike
07-11-2012, 03:17 PM
I can't seem to figure why it will not return the file's name and extension.

It is suppose to return the XML file's name and extension in an alert message.

It appears that this part of the code is not working. I deduced this by just declaring an alert at the beginning of the called function (msg()), and with the onClick function of the button. The onClick function works, but the msg() is not being called or it doesn't know what to do.

"document.forms["myForm"]["libxml"].value"


<html>
<head>
<script type="text/javascript">
function msg(var URL)
{
if (URL==null || URL=="" || !fileTypeParser(URL))
return('Please enter a XML file');
if (fileTypeParser(URL))
return(URL);
else
return('You have the wrong data type');
}

function fileTypeParser(var URL)
{
var array = URL.split(".");
if(array[1]=="xml")
return true;
else
return false;
}
</script>
</head>
<body>

<!-- This code is to simply out put the file's name and extension as an alert -->

<form name="myForm">
<input type="file" name="libxml" accept="text/xml" />
<input type="button" value="Submit" onclick="alert(msg(document.forms["myForm"]["libxml"].value))"/>
</form>
</body>
</html>

Any help would be greatly appreciated.

Thanks.

WolfShade
07-11-2012, 03:43 PM
Remove var from the function arguments ( msg(URL) ).

And check the error console to see if any error messages exist.

DrDOS
07-11-2012, 03:43 PM
I may come back and 'speriment with this one a bit, it's an interesting puzzle to say the least. The form submits onmouseup so any script action before submission requires onmousedown. When the form submits a new page loads, you see? And I didn't know file type inputs returned a value! And I find that first if statement to be most puzzling.

flapjacksmike
07-11-2012, 04:32 PM
Remove var from the function arguments ( msg(URL) ).

And check the error console to see if any error messages exist.

So you mean like this:


<script type="text/javascript">
function msg(URL)
{
if (URL==null || URL=="")
return('Please enter a database');
if (fileTypeParser(URL))
return(URL);
else
return('You have the wrong data type');
}

function fileTypeParser(URL)
{
var array = URL.split(".");
if(array[1]=="xml")
return true;
else
return false;
}
</script>

I still can't enter the msg() function, just checked it.

WolfShade
07-11-2012, 04:37 PM
And check the error console to see if any error messages exist.
If there is an error that breaks the script, the console should let you know.

flapjacksmike
07-11-2012, 05:11 PM
The error console only tells part of the story. It only said the value outputted was null, yet it should have been displayed.

This is what I ended up with that worked.

Thanks for your help.


<html>
<head>
<script type="text/javascript">
function msg()
{
var done = Boolean();
var URL=document.getElementById("libxml").value;
if(URL=="" || URL == null)
{
alert("Please enter a XML file."); // You never selected a file.
done = true; // No need to continue through the other if and else statements.
}
if(fileTypeParser(URL) && !done)
{
alert(URL); // Output the XML file's name and extension.
}
else if(!done)
{
alert("Please use the right file type (XML)."); // You put the wrong file type in.
}
}

function fileTypeParser(URL)
{
var array = URL.split(".");
if(array[1]=="xml")
return true;
else
return false;
}
</script>
</head>
<body>
<form>
<input type="file" id="libxml" />
<button type="button" value="Submit" onclick="msg()">Submit</button>
</form>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum