Monday, 26 September 2011

Kejadian (Event) di javascript ke-2

| Monday, 26 September 2011 | 0 comments


Event On Submit
Event on submit akan dibangkitkan apabila seorang user menekan tombol submit. Dengan event ini data yang diinputkan akan dikirimkan ke tempat lain (email, file teks atau ke dalam suatu tabel).

Contoh Program JavaScript
<Html>
<Body>
<SCRIPT LANGUAGE="JavaScript"> 
function cari()
{
var kata = document.formcari.keyword.value; 
var hasil = "http://www.google.com/search?q=" + kata ; 
window.open(hasil, 'google', config='height=500,width=750 scrollbars=yes location=yes') 
} 
</SCRIPT> 
<FORM NAME="formcari" onSubmit="cari()">
<center> 
<table>
<tr>
    <td colspan="4" bgcolor="orange"><h1 align="center">S e a r c h - E n g i n e</h1><hr color="black" size="4" ></td>
  </tr>
<tr>
   <td><b>Cari pakai</b></td>
   <td><img src="logoogle.gif"></td>          
   <td><INPUT NAME="keyword" SIZE="40" TYPE="text"></td>
   <td><INPUT TYPE="submit" VALUE="Cari .. !!"><input type="reset" Value="Ulang"></td>
</tr>
<tr>
    <td colspan="4" bgcolor="orange"><hr color="black" size="4" ></td>
  </tr> 
</table>
</center>
</FORM> 
</body>
</html>


Berikut diberikan contoh event submit yang terjadi pada form pengisian data guestbook yang hasilnya akan dikirim ke suatu email.

Contoh Program JavaScript
<Html>
<Body>
<SCRIPT LANGUAGE="JavaScript"> 
function isiform(form)
{
isinama(form);
isiemail(form);
isikomentar(form);
kosongkan(form);
} 
function kosongkan(form)
{
if((isinama(form) && isiemail(form) && isikomentar(form)))
   {
    form.submit;
   }
if((isinama(form)== false || isiemail(form)== false || isikomentar(form)== false))
  {
   salahisi(form);
  }
}
function salahisi(form)
{
 var teks ="Ada Kesalahan Isian :";
 if (isinama(form)== false)
 {teks +="\nNama Anda";}
 if (isiemail(form)==false)
{teks +="\nEmail Anda";}
if (isikomentar(form)==false)
 {teks +="\nKomentar Anda";}
 alert(teks);
}
function isinama(form)
{
if (form.nama.value=="")
{return false;}
else
{return true;}
}
function isiemail(form)
{
if((form.email.value=="" || form.email.value.indexOf('@',0)==-1) || form.email.value.indexOf('.')==-1)
{return false;}
else
{return true;}
}
function isikomentar(form)
{
if(form.cs.value=="")
{return false;}
else
{return true;}
}
</script>
<form name="f" method="post" action="ababilnew.com subject=Form">
<font face="Arial">
<table align="center">
<tr bgcolor="gray">
    <td colspan="2" align="center"><h1>G u e s t B o o k</h1></td>
</tr>
  <tr bgcolor="peachpuff">
    <td>Nama Anda :</td>
    <td><input type="text" value="" name="nama" size="30"</td>     
 </tr>
<tr bgcolor="peachpuff">
    <td>Email Anda :</td>
    <td><input type="text" value="" name="email" size="30"</td>     
 </tr>
<tr bgcolor="peachpuff">
    <td>Komentar Anda :</td>
    <td><textarea name="cs" rows="5" cols="50"></textarea></td>     
 </tr>
<tr bgcolor="orange">
    <td colspan="2" align="center">
    <input type="button" name="thesubmit" value="kirim" onClick="isiform(this.form)">
    <input type="reset" value="hapus">
    </td>
</tr> 
 <tr bgcolor="gray">
    <td colspan="2" align="center"><b>Terima Kasih Telah Mengisi Guest Book</b> </td>
</tr>
</table>
</font>
</form>
</body>
</html>

0 comments:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment

 
Copyright 2011. w3-id.blogspot.com. | Template by o-om.com - zoomtemplate.com