button (javascript) |
button (javascript) |
X. Ma |
Nov 15 2015, 07:56 AM
Post
#1
|
Group: Members Posts: 3 Joined: 14-November 15 Member No.: 23,741 |
Dear all,
I am new in HTML code writing. In the following code, I want to make the "Name list" button interactive. For example, if the user select "Class 1" and "Teacher", the button will trigger a download window that allows the user to download the file c1_teacher.txt. THANK YOU very much! =========================== <html> <body> <form name="fm"> Class: <select name="class"> <option value="c1">Class 1</option> <option value="c2">Class 2</option> </select> <br> </select> Person: <select name="person"> <option value="teacher">Teacher</option> <option value="student">Student</option> </select> <br> <input type="button" value="Name list" onClick="list()"> </form> <script language = "javascript"> function list() { I don't know how to write here. } </script> </body> </html> |
Christian J |
Nov 15 2015, 08:30 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Instead of cumbersome SELECT menus and scripting you might use an ordinary list:
CODE <ul> <li><a href="c1_teacher.txt">Class 1 Teacher</a></li> <li><a href="c2_teacher.txt">Class 2 Teacher</a></li> <li><a href="c1_student.txt">Class 1 Student</a></li> <li><a href="c2_student.txt">Class 2 Student</a></li> </ul> See also http://www.nngroup.com/articles/drop-down-...-use-sparingly/ If there are too many files to list like that you might still use SELECT menus, but then the best solution would be to combine them with a server-side script like PHP instead of client-side javascript, since the latter is not always enabled/supported by all users and search engines. Can you use PHP on your server? |
X. Ma |
Nov 16 2015, 02:51 AM
Post
#3
|
Group: Members Posts: 3 Joined: 14-November 15 Member No.: 23,741 |
Thank you very much, Christian J.
I am really new in HTML coding field. If possible, would you please write a short PHP code to me (actutally I have many files, so need the menu)? I can contact our server manager to load my webpage stuff together with the PHP script on the server. Thanks a lot! |
Christian J |
Nov 16 2015, 09:10 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Something like this maybe. Don't add anything before the PHP at the top (not even whitespace), otherwise the redirect may not work properly:
CODE <?php if(isset($_POST['class']) && isset($_POST['person'])) { $class=$_POST['class']; $person=$_POST['person']; $url=$class.'_'.$person.'.txt'; header("Location: $url"); exit; } else { ?> <!doctype html> <html> <head> <title>Name list</title> </head> <body> <form method="post" target="_blank"> <p> <label>Class: <select name="class"> <option value="c1">Class 1</option> <option value="c2">Class 2</option> </select> </label> </p> <p> <label>Person: <select name="person"> <option value="teacher">Teacher</option> <option value="student">Student</option> </select> </label> </p> <p><input type="submit" value="Name list"></p> </form> </body> </html> <?php } ?> I don't do any sanitizing of the submitted form data (like you usually should), since I don't think it's necessary in this case. Browsers will probably display the text file in the browser window instead of showing a download dialog, see also http://htmlhelp.com/faq/html/links.html#force-download |
X. Ma |
Nov 17 2015, 07:19 AM
Post
#5
|
Group: Members Posts: 3 Joined: 14-November 15 Member No.: 23,741 |
Hi, Christian,
Thanks a lot! Your PHP code is really helpful for my work. I am learning HTML, JS, PHP, and hope get around in this Web Design field in the near future. If I have further questions, I will ask you. THANKS. |
Lo-Fi Version | Time is now: 26th April 2024 - 11:22 PM |