Hello.
i have a trouble with my php and javascript code. I want to get database from form that use javascript that use onSubmit as form validator. It work when i refer to another page as addDb.php then add to database. I want to stay on current page then php code get data from that FORM. Okay maybe i have bad on expresing what i mean but this is my code:

<!DOCTYPE HTML PUBLIC
                 "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Pelanggan</title>
</head>
<body>

<?php
	include ("db.inc");
	$result=mysql_query($query);
	$count=mysql_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
	<td bgcolor="#FFFFFF">&nbsp;</td>
	<td colspan="9" bgcolor="#FFFFFF" align="center"><strong>Data Pelanggan</strong> </td>
</tr>
<tr>
	<td align="center" bgcolor="#FFFFFF">#</td>
	<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>NAMA DEPAN</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>NAMA BELAKANG</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>ALAMAT</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>PROVINSI</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>KODE POS</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>KODE NEGARA</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>TELPON</strong></td>
	<td align="center" bgcolor="#FFFFFF"><strong>EMAIL</strong></td>
</tr>
<?php
	while($rows=mysql_fetch_array($result)){
?>
<tr>
	<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['ID']; ?>"></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['ID']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['FIRST_NAME']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['LAST_NAME']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['ADDRESS']; ?></td>
	<td bgcolor="#FFFFFF" ><?php echo $rows['CITY_STATE']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['ZIP']; ?></td>
	<td bgcolor="#FFFFFF" align="center"><?php echo $rows['COUNTRY_ID']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['PHONE']; ?></td>
	<td bgcolor="#FFFFFF"><?php echo $rows['EMAIL']; ?></td>
	</tr>
<?php
	}
?>
<tr>
	<td colspan="10" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
	<input name="insert" type="submit" id="insert" value="Tambah">
	</td>
</tr>
<?php

	// Check if delete button active, start this 

	$value = $_POST['checkbox'];
	if($_REQUEST['delete'])
	{
		for($i=0;$i<$count;$i++){
			$del_id = $value[$i];
			$sql = "DELETE FROM $tbl_name WHERE ID ='$del_id'";
			$result = mysql_query($sql) or die ('Could not connect: ' . mysql_error()); 
		}

		// if successful redirect to delete_multiple.php 
		if($result)
		{
			echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
		}
	}
	
	
	mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<br>
<br>
<?php
if($_REQUEST['insert'])
{
?>
	<!-- Including the form validator script-->
	<SCRIPT src="gen_validatorv31.js"  language="JavaScript"></SCRIPT>
	<!-- Function handling the action switching-->
	<SCRIPT language="JavaScript">
	function OnSubmitForm()
	{ 
	  if(document.pressed == 'Tambahkan')
	  {
		document.myform.action ="addDb.php";
	  }
	  else
	  if(document.pressed == 'Cari')
	  {
		document.myform.action ="search.html";
	  }  
	// Trigger the validations 
	 return document.myform.runvalidation();
	}
	</SCRIPT>

	<!-- The HTML form goes here-->
	<FORM name="myform" method="post" onSubmit="return OnSubmitForm();" >
	<TABLE BORDER="1">
	 <TR>
		<TD> Name: </TD> <TD><INPUT TYPE="TEXT" Name="Nama_Depan"></TD>
		</TR>
		<TR>
		<TD>Nama Belakang: </TD> <TD> <INPUT TYPE="TEXT" Name="Nama_Belakang"></TD>
		</TR>
		<TR>
		<TD>Alamat: </TD> <TD> <INPUT TYPE="TEXT" Name="Alamat"></TD>
		</TR>
		<TR>
		<TD>Provinsi: </TD><TD><INPUT TYPE="TEXT" Name="Provinsi"></TD>
		</TR>
		<TR>
		<TD>Kode Pos: </TD><TD><INPUT TYPE="TEXT" Name="Kode_Pos"></TD>
		</TR>
		<TR>
		<TD>Kode Negara: </TD><TD><select name="Kode_Negara"><option value="" selected="selected">-- pilih --</option>
															<option value="1">1 - Asal</option>
															<option value="2">2 - Asal2</option>
															<option value="62">62 - Indonesia</option>
									</select></TD>
		</TR>
		<TR>
		<TD>Telpon: </TD><TD><INPUT TYPE="TEXT" Name="Telpon"></TD>
		</TR>
		<TR>
		<TD>Email: </TD><TD><INPUT TYPE="TEXT" Name="Email"></TD>
		</TR>
	</TABLE>
	<INPUT TYPE="SUBMIT" name="Operation" onClick="document.pressed=this.value" VALUE="Tambahkan">

	</FORM>

	<!-- Adding the validations for the validator script-->
	<SCRIPT LANGUAGE="JavaScript">
	var myformValidator = new Validator("myform");
	myformValidator.addValidation("Nama_Depan","req","Masukkan Nama Depan Anda");
	myformValidator.addValidation("Nama_Depan","maxlen=100","Batas maksimum Nama Depan terlampaui");
	myformValidator.addValidation("Nama_Belakang","maxlen=100","Batas maksimum Nama Belakang terlampaui");
	myformValidator.addValidation("Alamat","req","Masukkan Alamat Anda");
	myformValidator.addValidation("Alamat","maxlen=200","Batas maksimum Alamat terlampaui");
	myformValidator.addValidation("Provinsi","req","Masukkan Provinsi Anda");
	myformValidator.addValidation("Provinsi","maxlen=100","Batas maksimum Provinsi terlampaui");
	myformValidator.addValidation("Kode_Pos","req","Masukkan Kode Pos Anda");
	myformValidator.addValidation("Kode_Pos","maxlen=50","Batas maksimum Kode Pos terlampaui");
	myformValidator.addValidation("Kode_Negara","req","Pilih Kode Negara Anda");
	myformValidator.addValidation("Telpon","maxlen=50","Batas maksimum Telpon terlampaui");
	myformValidator.addValidation("Email","maxlen=100","Batas maksimum Email terlampaui");
	</SCRIPT>


	<!-- Reassign the onsubmit handler. Notice that the runvalidation function is initialized here (which is called in OnSubmitForm() )-->
	<SCRIPT LANGUAGE="JavaScript">
	document.myform.runvalidation = document.myform.onsubmit;
	document.myform.onsubmit = OnSubmitForm;
	</SCRIPT>
<?php
	if($_REQUEST['Tambahkan']){
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
	}
}
?>
</body>
</html>

masukkan Source code di addDb.php ke dalam file aja.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.