Jump to content

PHP effecting JS Valiidation Again??


yandoo

Recommended Posts

Hi there, I was hoping for a little help please

 

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 modified a little bit of JavaScript vallidation to vallidate ifEmpty on 2 testfields 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-again lol.

 

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 the final bit for the INSERT 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));
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/101276-php-effecting-js-valiidation-again/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.