yandoo Posted April 15, 2008 Share Posted April 15, 2008 Hi there, I was hoping for a little help I have a very simple form that has 2 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 the message boxes appears saying ""Please enter a value" but as soon as the message box closes, the record is inserted anyway!!?? I think somethings conflicting here as unlikely as it it sounds. If anybody knows why that would be great i cant understand why?? ...Please Help Kind Regards Heres the javascrpt code: <script type="text/javascript"> function formValidator(){ // Make quick references to our fields var field1 = document.getElementById('field1'); var field2 = document.getElementById('field2'); if(isEmpty(field1, "Please enter")){ if(isEmpty(field2, "Please enter 1")){ }} function isEmpty(elem, helperMsg){ if(elem.value.length == 0){ alert(helperMsg); elem.focus(); // set the focus to this input return true; } return false; }} </script> Heres the form: <form method="post" name="form1" action="<?php echo $editFormAction; ?>" onsubmit="return formValidator()"> ID: <input type="text" name="ID" value="" size="32"> Field1: <input type="text" name="field1" value="" size="32"> Field2: <input type="text" name="field2" value="" size="32"> <input type="submit" value="Insert record"> <input type="hidden" name="MM_insert" value="form1"> </form> And finally heres the code to INSERT the record query: <?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)); } ?> Thank you Quote Link to comment https://forums.phpfreaks.com/topic/101273-vallidation-ignored/ Share on other sites More sharing options...
epic_era1 Posted April 21, 2008 Share Posted April 21, 2008 trys your function this way, might work if not sorry function formValidator(){ // Make quick references to our fields var field1 = document.getElementById('field1'); var field2 = document.getElementById('field2'); if(isEmpty(field1, "Please enter")){ if(isEmpty(field2, "Please enter 1")){ }} function isEmpty(elem, helperMsg){ if(elem.value != ""){ alert(helperMsg); elem.focus(); // set the focus to this input return true; } return false; }} Quote Link to comment https://forums.phpfreaks.com/topic/101273-vallidation-ignored/#findComment-522870 Share on other sites More sharing options...
Psycho Posted April 21, 2008 Share Posted April 21, 2008 Well, there's a couple of problems. 1) The isEmpty funtion is written inside the formValidator function. While this will work, it is inefficient as the function is recreated each time formValidator is called. 2) When you do call isEmpty() with an IF statement you have no action - so nothing that the isEmpty function returns is returned to the FORM object (i.e. the form will submit anyway. 3) The isEmpty function returns true is the field is empty - so even if that return value was returned to the form, the form would submit if validation failed and not submit if it passed. Try this: <script type="text/javascript"> function formValidator(){ // Make quick references to our fields var field1 = document.getElementById('field1'); var field2 = document.getElementById('field2'); //Check each field and return false if invalid if (isEmpty(field1, 'Please enter')) return false; if (isEmpty(field2, 'Please enter 1')) return false; return true; } function isEmpty(elem, helperMsg){ if(elem.value.length == 0){ elem.focus(); alert(helperMsg); return true; } return false; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/101273-vallidation-ignored/#findComment-522894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.