Jump to content

[SOLVED] PHP effecting JS Valiidation??


yandoo

Recommended Posts

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 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?? ...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>

 

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 finally 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));
}
?>

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

In your JavaScript validation function, if there is a syntax error, it won't return false, and therefore will not stop the submit.

 

The JS syntax error is here:

    form.entered.focus();

it should be:

    form.field2.focus();

 

For debugging, add this button somewhere on the page, and click it to validate the form:

<input type="button" value="Validate" onclick="validate(document.forms[0]);" />

That way, it won't submit if there are JS errors

Hi, thanks for replies

 

Thats brilliant, silly mistake i should have seen it...With the change to field2 it works properly.

 

Thanks :)

 

p.s. i know it goes slightly off topic but how would add the other fields to be included in the vallidation too???

 

 

function validate(form){
  if(!form.field1.value.length){
    alert('Please enter a value');
    form.field1.focus();
    return false;
  }
  if(!form.field2.value.length){
    alert('Please enter a value');
    form.field2.focus();
    return false;
  }
  //etc
  return true;
} 

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.