Solved

Display html element depending on file input extension

Posted on 2010-11-23
10
430 Views
Last Modified: 2012-05-10
How can I display an html element depending on the extension of the file selected in a file input. My code (part of) I have so far is below. In IE the function fires and detects the file extension but does not display the select box. But it does work when a value in the type field is selected.
<script type="text/javascript">

// <![CDATA[

function display_field(obj) {

  if(obj.name == "photo") {

	var pos = obj.value.lastIndexOf(".");

	if(pos != -1 && strlen != pos+1){

	  var parts = obj.value.split('.');

	  var ext = parts[parts.length-1];

	  if(ext in {'pdf':'', 'tif':'','eps':''}) {

	   /* *** THIS NEXT LINE FAILS *** */	

             document.getElementById('thumb').style.display = '';

	  }

	}

  } else if(obj.name == "type") {

	selected_value = obj.options[obj.selectedIndex].innerHTML;

	document.getElementById("thumb").style.display = "none";

	if ( selected_value == '' || selected_value in {'pdf':'', 'tif':'','eps':''} ) {

	   /* *** THIS NEXT LINE WORKS *** */

            document.getElementById('thumb').style.display = '';

	}

  }

}

// ]]>

</script>



<form action="<?=tep_href_link('gallery_edit.php','action=add&page='.$page)?>" method="POST" enctype="multipart/form-data">

<table cellpadding="0" cellspacing="3">

	<tr>

		<td width="170">Upload image: </td>

		<td><input type="file" name="photo" onchange="display_field(this);"></td>

	</tr>

	<tr>

		<td>Image file type: </td>

		<td><?php echo tep_draw_pull_down_menu('type', $type_array, '', 'onchange="display_field(this);"'); ?></td>

	</tr>

	<tr id="thumb" style="display:none;">

		<td>Upload thumbnail image: </td>

		<td><input type="file" name="thumb"></td>

	</tr>

	<tr>

		<td colspan="2"><input type="submit" value="Upload"></td>

	</tr>

</table>

</form>

Open in new window

0
Comment
Question by:ncw
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:boraganesh
ID: 34196318
you can use the javascript to do it.
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34196319
I'm not sure of what do you try with:

document.getElementById('thumb').style.display = '';


But usual values for display are: "none", "block", "inline", etc... I'm not quite sure that assigning it an empty string will have any result.

Also I haven't very clear the situation of your problem. Can you explain it a bit deeper?
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34196330
you can chk the file type with javascript if and then use innerHtml to set the html contents, elements etc.
0
 
LVL 1

Author Comment

by:ncw
ID: 34196625
@boraganesh: My javascript reads the file extension ok, but the 'thumb' element does not display.

@Bardobrave: If the user selects a file to upload which has a file extension of pdf, tif, or eps then I want another file upload input box called 'thumb' to display. I can change the display type tp 'block' but it makes no difference.

You can see from the code that I have a selection box below the first file input called 'type'; If I change the value here to eg 'pdf' then the second file input correctly displays.
0
 
LVL 5

Accepted Solution

by:
SimonDard earned 500 total points
ID: 34196787
In jQuery (a JavaScript library that makes work a lot easier and works cross browser, http://jquery.com ):

(add an id="photo" to the file upload)

$(document).ready(function () {
         $('#photo').change(function() {
              $('#thumb').hide();
              if (${this).val().match(/\.(pdf|tif|eps)$/)) {
                    $('#thumb').show();
              }
           });
});
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Bardobrave
ID: 34197418
Ok, I see the problem in your code

<input type="file" name="thumb"> can't be reached through getElementById because it hasn't an id attribute.

Change it to <input type="file" id="thumb" name="thumb"> and it should work.
0
 
LVL 1

Author Comment

by:ncw
ID: 34197516
The <tr> has an id of "thumb", I want the whole table row to display. Like I say it works when the type select box is changed, just not when the file upload input is changed.
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34201366
Ok, then it should be a problem with the firing of the onChange event or with the if - else statement.

First of all, set an alert on your function's first line and check if it fires when file upload input is changed. If it is move the alert to first line inside the else if statement and so on.

We need to detect exactly where the problem is to fix it.

0
 
LVL 1

Author Comment

by:ncw
ID: 34201823
I had copied the code from a web post and had not copied the bit that defined strlen. Once I replaced strlen with obj.value.length it worked.
0
 
LVL 1

Author Closing Comment

by:ncw
ID: 34867207
I solved my own problem, but in future I will try using this Jquery script. I'm a Jquery beginner, I can see it's easy to use if only I could learn the syntax.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now