PDA

View Full Version : Script not working upon submit?



digic25
04-27-2006, 01:49 PM
This scripts runs at first but then it brings up an error message when I hit submit. Any help is appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>The Result of the course</title>
<script language="JavaScript">

function trim(s) {
while (s.charAt(0) == ' ')
s = s.substring(1);
while (s.charAt(s.length - 1)== ' ')
s = s.substring(0, s.length - 2);
return s;

}

function check(name)
{
var x = document.getElementById(ALastName);
var s = new String(x.value);
s = trim(s);
if(s == "")
{
alert("Please enter your " + LastName);
javascript:history.go(0);
}
var x = document.getElementById(AFirstNname);
var s = new String(x.value);
s = trim(s);
if(s == "")
{
alert("Please enter your " + FirstName);
javascript:history.go(0);
}
else
x.value = s;
}
</script>
</head>

<body>
<form action="http://matrix.csis.pace.edu/~badii/Exercise12.php" method="post">
<label for="lname">Last name: </label>
<input type="text" name="LastName" id="ALastName" size="20"><br><br>
<label for="fname">First name: </label>
<input type="text" name="FirstName" id="AFirstName" size="20"><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
<input type="submit" name="submit" id="submit" value=" Submit "
onclick = "check('LastName'); check('FirstName')">
</form>
</body>
</html>

Thanks
Tom

Beagle
04-27-2006, 02:38 PM
you're code is full of undefined variables:

ALastName
AFirstNname
LastName
FirstName

Kor
04-27-2006, 04:07 PM
Yes and it looks rather a simple validate code, so that the main error is that the function is called onclick, not on submit, as it should. Let's simplify the code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var names=['LastName','FirstName']
function check(e){
for(var i=0;i<names.length;i++){
var re=e[names[i]].value.match(/^\s*$/);//check if the field has any characters
if(re){
alert('Please enter your '+names[i]);
e[names[i]].focus();return false;
}
}
}
</script>
</head>
<body>
<form onsubmit="return check(this.elements)">
<label for="lname">Last name: </label>
<input type="text" name="LastName" id="ALastName" size="20"><br><br>
<label for="fname">First name: </label>
<input type="text" name="FirstName" id="AFirstName" size="20"><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
<input type="submit" name="submit" id="submit" value=" Submit ">
</form>
</body>
</html>

digic25
04-27-2006, 04:43 PM
The script needs to be able to contact that matrix server to pick up a .php file to return a message after the click on submit. I still have to add the file to the server, but it needs to have that form string in the code.

Kor
04-27-2006, 04:50 PM
The script needs to be able to contact that matrix server to pick up a .php file to return a message after the click on submit. I still have to add the file to the server, but it needs to have that form string in the code.

? why? I don't get it... My code will force user to input a string (but not one full only with empty spaces - some users do this trick). Unless the condition is not fulfiled, the submit is stoped. If condition, the submit goes... isn't that what you want?

digic25
04-27-2006, 05:42 PM
Whats supposed to happen is when you click submit a greeting will come with your name. The php consists of the greeting with the users name inputed via the javascript.