Jump to content

Vallidation Ignored???


yandoo

Recommended Posts

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

Link to comment
Share on other sites

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;
}}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.