Hi there, I was hoping for a little help
I have a very simple form that has 3 text fields, submit button, hidden field that inserts a record into my database....
I have just added a little bit of JavaScript vallidation and im getting really wierd results because hte message box appears saying ""Please enter a value" but when the message box closes, the record is inserted anyway????
If anybody knows why that would be great i cant understand why...but somehow the insert php is conflicting with the JS??? Please Help
Kind Regards
Heres the form:
<form method="post" name="form1" action="<?php echo $editFormAction; ?>" onsubmit="return validate(this);">
ID:
<input type="text" name="ID" value="" size="32">
Field1:
<input type="text" name="field1" value="" size="32">
valign="baseline">
nowrap align="right">Field2:
<input type="text" name="field2" value="" size="32">
valign="baseline">
nowrap align="right">
nowrap align="right">
<input type="submit" value="Insert record">
<input type="hidden" name="MM_insert" value="form1">
</form>
And heres the JavaScript that is between <head tags>:
<script type="text/javascript">
function validate(form){
if(!form.field2.value.length){
alert('Please enter a value');
form.entered.focus();
return false;
}
return true;
}
</script>
And heres the php that INSERTS the record:
<?php require_once('Connections/woodside.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO test_table (ID, field1, field2) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['ID'], "int"),
GetSQLValueString($_POST['field1'], "text"),
GetSQLValueString($_POST['field2'], "text"));
mysql_select_db($database_woodside, $woodside);
$Result1 = mysql_query($insertSQL, $woodside) or die(mysql_error());
$insertGoTo = "test_table2.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>