Internet Ohjelmointi 1 Example 1 4 <title>form</title> 5 </head> 6 <body> 7 <form method="post" action="calc.php"> 8 Loan Amount <input type="text" name="principle" /><br> 9 Monthly Repayment <input type="text" name="monthly" /><br> 10 Number of Years <input type="text" name="years" value="25" /><br> 11 Interest Rate <input type="text" name="rate" value="6" /><br> 12 <input type="submit" value="send" /></form> 13 </body> 14 </html> Example 2 4 <title>margie's Travel</title> 5 <link rel="stylesheet" type="text/css" href="default.css"> 6 <style> 7 table {width: 100%} 8 body { 9 background-color: #cccc00; 10 } 11 </style> 12 </head> 13 <body> 14 <div id="header"> 15 <h1 style="text-align: center; margin: 3px; font-size: 40px">Margie's Travel</h1> 16 <h5 style="text-align: center; margin: 5px; font-size: 16px">Leave the details to us, and leave your worries behind</h5> 17 </div> 18 <hr style="clear: left"> 19 <div id="main"> 20 <h2>sign Up for E-Mail Specials</h2> 21 <form method="post" action="handle.php"> 22 <table align="right" valign="top"> 23 <tr> 24 <td>name:</td> 25 <td><input type="text" name="name" id="name" required autofocus></td> 26 </tr> 27 <tr> 28 <td>address:</td> 29 <td><input type="text" name="address" required></td> 30 </tr> 31 <tr> 32 <td>city:</td>
Internet Ohjelmointi 2 33 <td><input type="text" name="city"></td> 34 </tr> 35 <tr> 36 <td>state:</td> 37 <td><input type="text" name="state"></td> 38 </tr> 39 <tr> 40 <td>zip:</td> 41 <td><input type="text" name="zip" required></td> 42 </tr> 43 <tr> 44 <td>e-mail address:</td> 45 <td><input type="email" name="email" maxlength="100" placeholder="enter your email address"></td> 46 </tr> 47 <tr> 48 <td>comments:</td> 49 <td><textarea name="comments" id="comments" rows="6" cols="50" placeholder="enter comments here"></textarea></td> 50 </tr> 51 <tr> 52 <td></td> 53 <td><input type="submit" value="submit" id="submit" onclick="show();">  54 <input type="reset" value="clear"></td> 55 </tr> 56 </table> 57 </form> 58 </div> 59 <div id="bottomnav"> 60 <hr style="clear: left"> 61 <p style="margin:0px; text-align:center"> 62 <a href="index.htm">home</a> 63 <a href="domestic.htm">domestic Travel</a> <a href="international.htm">international Travel</a> 64 <a href="faqs.htm">faqs</a> 65 <a href="about.htm">about</a> 66 <a href="mailing.htm">mailing List</a></p> 67 <hr> 68 </div> 69 <div> 70 <p class="copyright"> 71 Copyright Margie's Travel<br> 72 No material may be reproduced without written permission<br> 73 <a href="mailto:webmaster@margiestravel.com">contact the Webmaster</a></p> 74 </div> 75 </body> 76 </html>
Internet Ohjelmointi 3 Example 21 4 <script> 5 function myfunction() 6 { 7 var coffee = document.getelementsbyname("coffee"); 8 var txt = ""; 9 var i; 10 for (i = 0; i < coffee.length; i++) 11 { 12 if (coffee[i].checked) 13 { 14 txt = txt + coffee[i].value + " "; 15 } 16 } 17 document.getelementbyid("order").value = "You ordered a coffee with: " + txt; 18 } 19 </script> 20 </head> 21 <body> 22 <p>how would you like your coffee?</p> 23 <form action="form_action.asp"> 24 <input type="radio" name="coffee" value="cream">with cream<br> 25 <input type="radio" name="coffee" value="sugar">with milk<br> 26 <br> 27 <input type="button" onclick="myfunction()" value="send order"> 28 <br><br> 29 <input type="text" id="order" size="50"> 30 <input type="submit" value="submit"> 31 </form> 32 </body> 33 </html>
Internet Ohjelmointi 4 Example 22 4 <script> 5 function myfunction() 6 { 7 var x = document.getelementbyid("myselect"); 8 var txt = ""; 9 for (var i=0; i<x.length; i++) 10 { 11 if (x.options[i].selected) { 12 txt = txt + x.options[i].text + "<br>"; 13 } 14 } 15 document.getelementbyid("demo").innerhtml=txt; 16 } 17 </script> 18 </head> 19 <body> 20 21 <form> 22 <select id="myselect" multiple="multiple"> 23 <option>apple</option> 24 <option>orange</option> 25 <option>pineapple</option> 26 <option>banana</option> 27 </select> 28 </form> 29 30 <p>click the button to return the text of all options in the drop-down list.</p> 31 32 <p id="demo"></p> 33 34 <button type="button" onclick="myfunction()">get the selected options</button> 35 36 37 38 </body> 39 </html>
Internet Ohjelmointi 5 Example 23 2 <!-- 3 To change this license header, choose License Headers in Project Properties. 4 To change this template file, choose Tools Templates 5 and open the template in the editor. 6 --> 7 <html> 8 <head> 9 <title>todo supply a title</title> 10 <meta charset="iso-8859-15"> 11 <meta name="viewport" content="width=device-width"> 12 <script> 13 function show() { 14 var myradios = document.getelementsbyname('gender'); 15 for (var i = 0, length = myradios.length; i < length; i++) { 16 if (myradios[i].checked) { 17 myvalue = myradios[i].value; 18 break; //Because only one can be selected 19 } 20 } 21 window.alert(myvalue); 22 } 23 </script> 24 </head> 25 <body> 26 <form> 27 <label for="male">male</label> 28 <input type="radio" name="gender" id="male" value="male"><br> 29 <label for="female">female</label> 30 <input type="radio" name="gender" id="female" value="female"><br><br> 31 <input type="submit" value="submit" onclick="show();"> 32 </form> 33 </body> 34 </html> 35
Internet Ohjelmointi 6 Example 3 4 <title></title> 5 <meta http-equiv="content-type" content="text/html; charset=iso-8859-15"> 6 <style> 7 body { 8 width:100%; 9 } 10 form { 11 width:40%; 12 background-color: #ffcccc; 13 border: 1px solid; 14 } 15 h3 { 16 background-color: #009999; 17 width:40%; 18 color: #ffff00; 19 border: 1px solid; 20 } 21 </style> 22 </head> 23 <body> 24 <h3> Please answer the sports enquiry </h3> 25 <form action="do.php"> 26 <fieldset> 27 <table> 28 <legend>personal information</legend> 29 <tr><td><label for="name">name</label></td> 30 <td><input type="text" name="name" value="" /> </td></tr> 31 <tr><td><label for="address">address</label></td> 32 <td> <input type="text" name="address" value="" /> </td></tr> 33 <tr><td><label for="email">email</label></td> 34 <td><input type="email" name="email" value="" /></td></tr> 35 <tr><td><label for="male">male</label> </td> 36 <td> <input type="radio" name="gender" value="male" /></td></tr> 37 <tr><td><label for="female">female</label> 38 <td><input type="radio" name="gender" value="female" /></td></tr> 39 40 </table> 41 </fieldset> 42 <fieldset> 43 <table> 44 <legend>query</legend> 45 <tr><td><label for="sports">select sports</label></td> 46 <tr><td><label for="tennis">tennis</label></td> 47 <td><input type="checkbox" name="sports" value="tennis" /></td></tr> 48 <tr><td><label for="badminton">badminton</label></td> 49 <td><input type="checkbox" name="sports" value="badminton" /> </td></tr> 50 <tr><td><label for="squash">squash</label> </td> 51 <td> <input type="checkbox" name="sports" value="squash" /> </td></tr> 52 <tr><td><label for="gym">gym</label></td> 53 <td><input type="checkbox" name="sports" value="gym" /></td></tr> 54 <tr><td><label for="usage">usage</label></td></tr> 55 <tr><td><select name="usage" size="1"> 56 <option value="daily">daily</option> 57 <option value="weekly">weekly</option> 58 <option value="monthly">monthly</option> 59 <option value="morefreq">more frequently</option> 60 </select></td></tr> 61 </table> 62 </fieldset> 63 <fieldset> 64 <legend>feedback</legend> 65 <label for="sat">satisfaction</label> 66 1<input type="range" name="sat" min="1" max="100" step="1" value="1">100 67 <br><label id="label1" for="comment">comments</label> 68 <textarea name="comment" cols="60" rows="5" wrap="hard"> Give free comments...</textarea> 69 <hr> 70 <input type="submit" value="submit"><input type="reset" value="clear"> 71 </fieldset> 72 </form>
Internet Ohjelmointi 7 73 </body> 74 </html> Example 4 2 3 $filename = "/tmp/myfile.txt"; 4 $fp1 = fopen($filename,"w") OR die("can't open file"); 5 for($i=1; $i < 6;$i++) 6 { 7 fwrite($fp1,"<br>row". "$i"); 8 } 9 fclose($fp1); 10 11 $fp2 = fopen($filename,"r") OR die("can't open file"); 12 echo "<br><br>the contents of $filename file<br><br>"; 13 $filesize = filesize( $filename ); 14 $filetext = fread( $fp2, $filesize ); 15 echo ( "File size : $filesize bytes" ); 16 echo ( "<pre>$filetext</pre>" ); 17 fclose( $fp2 ); 18 19?> Example 5 2 3 $filename = "/tmp/myfile.txt"; 4 $fp1 = fopen($filename,"w") OR die("can't open file"); 5 for($i=1; $i < 6;$i++) 6 { 7 fwrite($fp1,"<br>row". "$i"); 8 } 9 fclose($fp1); 10 11 $fp2 = fopen($filename,"r") OR die("can't open file"); 12 echo "<br><br>the contents of $filename file<br><br>"; 13 $filesize = filesize( $filename ); 14 while(!feof($fp2)) { 15 $line = fgets($fp2); 16 echo $line; 17 } 18 echo ( "<br>file size : $filesize bytes" ); 19 echo ( "<pre>$filetext</pre>" ); 20 fclose( $fp2 ); 21 22?>
Internet Ohjelmointi 8 Example 6 2 $hak = opendir("images/"); 3 $nimi = readdir($hak); 4 echo "<br><br><br><br>directory Images/"; 5 while ($nimi) { 6 echo "<br>$nimi\n"; 7 $nimi = readdir($hak); 8 } 9 closedir($hak); 10?> Example 7 4 <title></title> 5 <meta http-equiv="content-type" content="text/html; charset=iso-8859-15"> 6 <style> 7 form { 8 width:40%; 9 background-color: #ffcccc; 10 border: 1px solid; 11 } 12 h3 { 13 background-color: #009999; 14 width:40%; 15 color: #ffff00; 16 border: 1px solid; 17 } 18 </style> 19 <script type="text/javascript"> 20 function clearstatus() { 21 document.getelementbyid("status").innerhtml = ""; 22 } 23 function senddata(event) 24 { 25 var gender; 26 var myname = document.getelementbyid("myname").value; 27 var address = document.getelementbyid("address").value; 28 var email = document.getelementbyid("email").value; 29 var age = document.getelementbyid("age").value; 30 var radios = document.getelementsbyname('gender'); 31 for (var i = 0, length = radios.length; i < length; i++) { 32 if (radios[i].checked) { 33 gender = radios[i].value; 34 break; 35 } 36 } 37 if (window.xmlhttprequest) 38 {// code for IE7+, Firefox, Chrome, Opera, Safari 39 xmlhttp=new XMLHttpRequest(); 40 } 41 else 42 {// code for IE6, IE5 43 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 44 } 45 xmlhttp.onreadystatechange=function() 46 { 47 if (xmlhttp.readystate==4 && xmlhttp.status==200) 48 { 49 document.getelementbyid("status").innerhtml=xmlhttp.responsetext; 50 } 51 } 52 xmlhttp.open("get","savedata.php?name="+myname + "&address=" +address + "&email=" +email + 53 "&age=" +age + "&gender=" +gender,true); 54 xmlhttp.send(); 55 }
Internet Ohjelmointi 9 56 </script> 57 </head> 58 <body> 59 <h3> Please enter the fields and submit </h3> 60 <form> 61 <fieldset> 62 <table> 63 <legend>personal information</legend> 64 <tr><td><label for="name">name</label></td> 65 <td><input type="text" name="myname" id="myname" value="" /> </td></tr> 66 <tr><td><label for="address">address</label></td> 67 <td> <input type="text" name="address" id="address" value="" /> </td></tr> 68 <tr><td><label for="email">email</label></td> 69 <td><input type="email" name="email" id="email" value="" /></td></tr> 70 <tr><td><label for="male">male</label> </td> 71 <td> <input type="radio" name="gender" id="male" value="male" /></td></tr> 72 <tr><td><label for="female">female</label></td> 73 <td><input type="radio" name="gender" id="female" value="female" /></td></tr> 74 <tr><td><label for="age">age: </label></td> 75 <td> <input id="age" type="number" min="0" max="120" step="1" value ="21"/></td></tr> 76 </table> 77 <hr> 78 <input id="submit" type="button" value="submit" onclick="senddata();"><input type="reset" value="clear" onclick="clearstatus();" > 79 <div id="status"></div> 80 </fieldset> 81 </form> 82 </body> 83 </html> savedata.php 2 error_reporting(e_all); 3 ini_set('display_errors', '1'); 4 $name=$_get["name"]; 5 $address=$_get["address"]; 6 $email=$_get["email"]; 7 $age=$_get["age"]; 8 $gender=$_get["gender"]; 9 $filename = "/tmp/persons6.txt"; 10 $fp1 = fopen($filename,"a"); 11 if($fp1) { 12 if(filesize($filename) == 0) 13 fwrite($fp1, "<tr><th>name</th><th>address</th><th>age</th><th>email</th><th>gender</th></tr> \r\n"); 14 fwrite($fp1, "<tr><td>". $name. "</td><td>". $address. "</td><td>". $age."</td><td>". $email. 15 "</td><td>". $gender. "</tr>\r\n"); 16 $status = "Save succesfull"; 17 } 18 else 19 $status = "Save not succesfull"; 20 fclose($fp1); 21 echo $status; 22?>
Internet Ohjelmointi 10 Example 8 4 <title></title> 5 <meta http-equiv="content-type" content="text/html; charset=iso-8859-15"> 6 <style> 7 table { 8 width:40%; 9 } 10 table, td, th{ 11 border: solid 2px; 12 background-color: #ffcccc; 13 } 14 </style> 15 <script type="text/javascript"> 16 function getdata(event) 17 { 18 if (window.xmlhttprequest) 19 {// code for IE7+, Firefox, Chrome, Opera, Safari 20 xmlhttp=new XMLHttpRequest(); 21 } 22 else 23 {// code for IE6, IE5 24 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 25 } 26 xmlhttp.onreadystatechange=function() 27 { 28 if (xmlhttp.readystate==4 && xmlhttp.status==200) 29 { 30 document.getelementbyid("data").innerhtml=xmlhttp.responsetext; 31 } 32 } 33 xmlhttp.open("get","getdata.php",true); 34 xmlhttp.send(); 35 } 36 </script> 37 </head> 38 <body> 39 <h3> Please press button to get data </h3> 40 <input id="fetch" type="button" value="get data" onclick="getdata();"> 41 <div id="data"></div> 42 </body> 43 </html> getdata.php 2 $filename = "/tmp/persons6.txt"; 3 $fp2 = fopen($filename,"r") OR die("can't open file"); 4 $filesize = filesize( $filename ); 5 $filetext = fread( $fp2, $filesize ); 6 fclose( $fp2 ); 7 echo ( "<table>$filetext</table>" ); 8?>
Internet Ohjelmointi 11 Example 9 4 <title>php Form Upload</title> 5 </head> 6 <body> 7 <form action="upload.php" method="post" enctype="multipart/formdata"> 8 <label for="file">filename:</label> 9 <input type="file" name="file" id="file"><br> 10 <input type="submit" name="submit" value="submit"> 11 </form> 12 </body> 13 </html> upload.php: 2 $allowedexts = array("jpg", "jpg", "jpeg", "gif", "png"); 3 $extension = end(explode(".", $_FILES["file"]["name"])); 4 if ((($_FILES["file"]["type"] == "image/gif") 5 ($_FILES["file"]["type"] == "image/jpeg") 6 ($_FILES["file"]["type"] == "image/png") 7 ($_FILES["file"]["type"] == "image/pjpeg")) 8 && ($_FILES["file"]["size"] < 20000) 9 && in_array($extension, $allowedexts)) 10 { 11 if ($_FILES["file"]["error"] > 0) 12 { 13 echo "Return Code: ". $_FILES["file"]["error"]. "<br>"; 14 } 15 else 16 { 17 echo "Upload: ". $_FILES["file"]["name"]. "<br>"; 18 echo "Type: ". $_FILES["file"]["type"]. "<br>"; 19 echo "Size: ". ($_FILES["file"]["size"] / 1024). " kb<br>"; 20 echo "Temp file: ". $_FILES["file"]["tmp_name"]. "<br>"; 21 22 if (file_exists("/home/user/". $_FILES["file"]["name"])) 23 { 24 echo $_FILES["file"]["name"]. " already exists. "; 25 } 26 else 27 { 28 move_uploaded_file($_files["file"]["tmp_name"], 29 "/home/user/images/". $_FILES["file"]["name"]); 30 echo "Stored in: ". "/home/user/". $_FILES["file"]["name"]; 31 } 32 } 33 } 34 else 35 { 36 echo "Invalid file"; 37 } 38?>