Validation data in JS
I'm trying to use the function verifica() in JavaScript to validate data populated in the form, but it doesn't run. What's the problem?
Please, help me in this question.
Code:
<?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 myblog (nome, email, coment) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['nome'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['coment'], "text"));
mysql_select_db($database_conn, $conn);
$Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
$insertGoTo = "Guest.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_conn, $conn);
$query_myRdset = "SELECT * FROM myblog ORDER BY id DESC";
$myRdset = mysql_query($query_myRdset, $conn) or die(mysql_error());
$row_myRdset = mysql_fetch_assoc($myRdset);
$totalRows_myRdset = mysql_num_rows($myRdset);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
body {
<script language="JavaScript">
function verifica()
{
if(document.form1.nome.value==""){
window.alert("Por favor, forneça seu nome."); form1.nome.focus();
return;
}
if(document.form1.email.value==""){
window.alert("Por favor, forneça seu e-mail."); form1.email.focus();
return;
}
if (form1.email.value.indexOf('@',0)==-1||form1.email.value.indexOf('.',0)==-1{window.alert("Por favor, forneça corretamente seu e-mail."); form1.email.focus();
return;
}
if(document.form1.coment.value==""){
window.alert("Por favor, coloque um comentário."); form1.coment.focus();
return;
}
document.form1.action=<?php echo $editFormAction; ?>;
document.form1.submit();
}
</script>
<table width="100%" border="1" align="center" bgcolor="#CC6666">
<tr>
<td><form name="form1" action="#" onsubmit="return verifica()" method="POST">
<H3>
<div align="center"><em>DEIXE AQUI SEU COMENTÁRIO, DÚVIDA OU SUGESTÃO</em>
</H2>
<br />
<br />
<span class="style6">Nome: </span></div>
<span class="style6">
<label>
<input name="nome" type="text" id="nome" size="50" />
</label>
</span>
<p class="style6">E-mail:
<label>
<input name="email" type="text" id="email" size="50" maxlength="80" />
</label>
</p>
<p class="style6">Comentário
<br />
<label>
<textarea name="coment" cols="80" rows="5" id="coment"></textarea>
</label>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Enviar" />
</p>
<p> </p>
<?php do { ?>
<p class="style6">Em <?php echo $data = implode("/", array_reverse(explode("-", substr($row_myRdset['data'],0,10)))); ?> às <?php echo substr($row_myRdset['data'],11); ?>
<?php echo $row_myRdset['nome']; ?>
(mailto:<?php echo $row_myRdset['email']; ?>
) escreveu: <?php echo $row_myRdset['coment']; ?> </p>
<?php } while ($row_myRdset = mysql_fetch_assoc($myRdset)); ?><input type="hidden" name="MM_insert" value="form1">
</form></td>
</tr>
</table>
</div>
</div>
</div><div style="clear:both;"></div>
</body>
</html>
<?php
mysql_free_result($myRdset);
mysql_free_result($myRdset);
?>
you should return a false Boolean in order to stop the action.
Code:
if(document.form1.nome.value==""){
window.alert("Por favor, forneça seu nome."); form1.nome.focus();
return false ;
}
// and so on
Not only that. There were syntax errors in your code.
The correted code:
Code:
function verifica()
{
if(document.form1.nome.value==""){
window.alert("Por favor, forneça seu nome."); form1.nome.focus();
return false;
}
if(document.form1.email.value==""){
window.alert("Por favor, forneça seu e-mail."); form1.email.focus();
return false;
}
if (form1.email.value.indexOf('@',0)==-1||form1.email.value.indexOf('.',0)==-1){
window.alert("Por favor, forneça corretamente seu e-mail."); form1.email.focus();
return false;
}
if(document.form1.coment.value==""){
window.alert("Por favor, coloque um comentário."); form1.coment.focus();
return false;
}
document.form1.action=<?php echo $editFormAction; ?>;
document.form1.submit();
}
Originally Posted by
Granite
Not only that. There were syntax errors in your code.
The correted code:
Code:
function verifica()
{
document.form1.action=<?php echo $editFormAction; ?>;
document.form1.submit();
}
The red part is redundant. Useless. The function is called onsubmit, so that if the function returns anything but false (and by default it will return itself), the submit will be done anyway.
Originally Posted by
Kor
The red part is redundant. Useless. The function is called onsubmit, so that if the function returns anything but false (and by default it will return itself), the submit will be done anyway.
now I got it. I've changed also the last lines putting:
return true;
document.form1.submit();
Now it works.
Thank you very much.
Originally Posted by
atomicc
now I got it. I've changed also the last lines putting:
return true;
document.form1.submit();
Now it works.
Thank you very much.
As I said. You may remove the line
Code:
document.form1.submit();
as the code will stop anyway on one of the return .
And you don't need the return true . As I said, if a return is required, the function will return itself (thus not a false Boolean) by default
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks