...

View Full Version : Form Validation code problem



eli14
10-27-2010, 06:15 PM
Hi

can anyone tell me why this code is not working on my site.

<script type="text/javascript">
function validate_required(website, alerttxt)
{
with (website)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false)
{website.focus();return false;}
}
}
</script>


and i have added this to the form
<form id="myForm" action="data.php" method="post" onsubmit="return validate_form(this)">

Yes the required field is "website"

Basically the alert box pops up and says the error yet the forms still submits.

I have no idea why. Please help.

eli14
10-27-2010, 08:43 PM
i also have this code in my html

<script type="text/javascript">
$(document).ready(function() {
$('#myForm').ajaxForm({
target: '#showdata',
success: function() {
$('#showdata').fadeIn('slow');
}
});
});
</script>

becuause my form refreshes without refreshing the page i think thats why i have got the problem.

stratocaster
10-27-2010, 11:20 PM
I just tried this locally on my system and it seems to be working fine for me. I added the div showdata to complete the example.


<html>

<head>
<script type="text/javascript">

function validate_required(website, alerttxt) {
with (website) {
if (value==null||value=="") {
alert(alerttxt);return false;
} else {
return true;
}
}
}

function validate_form(thisform) {
with (thisform) {
if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false) {
website.focus();return false;
}
}
}
</script>
</head>
<body>
<form id="myForm" action="test.php" method="post" onsubmit="return validate_form(this);">
website : <input type="text" name="website"/> <input type="submit" value="submit">
<div style="display:none;" id="showdata">Here it is</div>
</form>

</body>
<script type="text/javascript">
$(document).ready(function() {
$('#myForm').ajaxForm({target: '#showdata',success: function() {
$('#showdata').fadeIn('slow');
}});
});

</html>

eli14
10-27-2010, 11:32 PM
yes that does work like that but for some reason it won't work on my php let me show you the whole document.

HTML/PHP Document
========================================================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Ajax JQuery Refresh Table </title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myForm').ajaxForm({
target: '#showdata',
success: function() {
$('#showdata').fadeIn('slow');
}
});
});
</script>

<script type="text/javascript">
function validate_required(website, alerttxt)
{
with (website)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false)
{website.focus();return false;}
}
}
</script>

</head>
<body onload = "setTimeout('enable()', 5000)">


<script type = "text/javascript">
function enable() {
var x = document.getElementById("reload");
x.value = "Submit";
x.disabled = false;
}

function disable() {
var x = document.getElementById("reload");


x.value = "Wait 30 secs";
x.disabled = true;
setTimeout('enable()', 30000);
}


</script>

<form id="myForm" action="data.php" method="post" onsubmit="return validate_form(this)">
<fieldset>
<div>
<label>slogan</label>
<input type="input" name="ad" style="width:300px; margin:0 0 0 16px; color:#999;" id="cname" class="required"/>

</div>
<div>
<label>Website </label>
<input type="input" o name="website" style="width:300px; color:#999" class="required" />
</div>
<div>
<input type ="submit" id = "reload" value = "Submit" onclick = "disable()" disabled>
</div>
</fieldset>
</form>


<div id="showdata">
<?php

$website= $_POST["website"];

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("dbname");

$result = mysql_query("SELECT * FROM ads ORDER BY uniqueid DESC");

echo "<table border='0'>
<tr>
<th align='left' style='font-size: 25px; color: gray; text-decoration: underline;'>table</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ad'] . "</td>";

echo "</tr>";
echo "<tr>";
echo "<td>" . $row['website'] . "</td>";

echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

</div>


</body>
</html>

stratocaster
10-28-2010, 01:30 AM
What browser are you using? IE8 is having an issue with the disable function. It seems to be ok in FireFox. Not sure why IE is having an issue. When you disable the button before the return it doesn't submit the form. If you comment out the line where you disable it, it submits the form fine in IE.

Also make sure your action in your form tag is a real action otherwise you'll never get to you ajaxForm success function.

strat

eli14
10-28-2010, 11:56 AM
What browser are you using? IE8 is having an issue with the disable function. It seems to be ok in FireFox. Not sure why IE is having an issue. When you disable the button before the return it doesn't submit the form. If you comment out the line where you disable it, it submits the form fine in IE.

Also make sure your action in your form tag is a real action otherwise you'll never get to you ajaxForm success function.

strat

I am using firefox

The form and everything works fine and even the validation works but after the popup box has appeared the form still submits.

Is it not something to do with the form submitting without the page refreshing?

abduraooft
10-28-2010, 12:47 PM
yes that does work like that but for some reason it won't work on my php let me show you the whole document.

HTML/PHP Document That's not pure HTML. It contains many PHP snippets which can't be parsed at our end to produce the complete HTML. Take the html source from your browser's "view source" option and post it here.

PS: Please use
][/COLOR] tags to wrap your code while posting here. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum