PDA

View Full Version : My form can't submit...kindly help



tangara
08-12-2013, 10:34 AM
Dear forum members,

I'm trying to re-work on my web pages. Alas, my old form isn't working anymore...as in it used to be able to submit but now when I press submit, it just stall there.

Here's my java scripts and the form.



<%@ page contentType="text/html; charset=UTF-8" %>
<%
String strCookieName = "serverMessage";
String strServerMessage = "";
Cookie objCookies[] = request.getCookies();
Cookie objCookie = null;
if (objCookies != null) {
for (int intIndex = 0; intIndex < objCookies.length; intIndex++) {
if (objCookies[intIndex].getName().equals(strCookieName)) {
objCookie = objCookies[intIndex];
strServerMessage = objCookie.getValue();
}//if
}//for
Cookie objKillMyCookie = new Cookie("serverMessage", "");
objKillMyCookie.setMaxAge(0);
objKillMyCookie.setPath("/");
response.addCookie(objKillMyCookie);
}//if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tutors Registration</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen,projection" />
<link rel="stylesheet" style type ="text/css" href ="style.css">
<script language="javascript">
function checkForm(){
declare an empty Object variable.
var objForm = new Object();
var objFullNameTextBox = new Object();
var objMessageBox = new Object();
var objAddress = new Object();

var strName = new String("");
objForm = document.getElementById("frmData");
objFullNameTextBox = document.getElementById("FullName");
objMessageBox = document.getElementById("divMessage");
objEmail = document.getElementById("Email");

strName = objName.value;
strEmail = objEmail.value;
strAddress = objAddress.value;


strEmail = trimData(strEmail);
strAddress = trimData(strAddress);

strName = objFullNameTextBox.value;
strName = trimData(strName);
if (strName=="" ){
strMessage += "Please indicate Name<br>";
blnIsAllOkay = false;
}
if (strAddress=="" ){
strMessage += "Please indicate Address<br>";
blnIsAllOkay = false;
}

if (blnIsAllOkay==true){
objForm.submit();
}else{
objMessageBox.innerHTML = strMessage;
}

function trimData(pstrData){
var intIndex=0;
var intRightIndex= pstrData.length -1;
while((intIndex < pstrData.length) && (pstrData.charAt(intIndex) == ' '))
{intIndex++; }
while((intRightIndex > intIndex) && (pstrData.charAt(intRightIndex) == ' '))
{intRightIndex-=1;}
return pstrData.substring(intIndex, intRightIndex+1);
}
}
function clearErr() {
objMessageBox = document.getElementById("divMessage");
objMessageBox.innerHTML = "";
}
</script>
</head>
<body>
<div id="wrapper">
<div id="header">

<form id="frmData" name="frmData" action = "process" method = "post" >
<table>
<tr>
<td>Full Name</td>
<td><input type="text" id="FullName" name="FullName"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" id="Address" name="Address" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" id="Email" name="Email" value=""></td>
</tr>


<td>
<input type="button" id="btnSubmit" name="btnSubmit" value="Submit" onclick="checkForm()"/>
<input id="reset" type="reset" name="reset" value="reset" onclick="clearErr();"/></td>
</tr>
</form>



Hope someone can point me the way to make it submit...Thanks. Or if my javascript is outdated, please let me know where I can get easy plug-in code like jquery for java - preferably one with checkbox, radio buttons, drop down option etc.

Thanks.

Beverleyh
08-12-2013, 11:46 AM
I haven't checked any JavaScript as far as validation goes but you cannot submit a form, and have it processed/do something with the data, with JavaScript alone.
You would need to use a server-side language, such as PHP, to do that.

From your markup;
<form id="frmData" name="frmData" action = "process" method = "post" >
The provided "action" looks incomplete - I would expect it so look more like action="process.php", where it would be posting to a PHP file that contained the code to perform the processing task.

As this is a re-work of a form that used to submit, it might be that you've edited the markup incorrectly, so have a look for the form processing file on your server, possibly called "process.php", and make sure your form points to that.

Also check offline backups in case you've deleted the server version.

tangara
08-12-2013, 01:11 PM
I haven't checked any JavaScript as far as validation goes but you cannot submit a form, and have it processed/do something with the data, with JavaScript alone.
You would need to use a server-side language, such as PHP, to do that.

From your markup;
<form id="frmData" name="frmData" action = "process" method = "post" >
The provided "action" looks incomplete - I would expect it so look more like action="process.php", where it would be posting to a PHP file that contained the code to perform the processing task.

As this is a re-work of a form that used to submit, it might be that you've edited the markup incorrectly, so have a look for the form processing file on your server, possibly called "process.php", and make sure your form points to that.

Also check offline backups in case you've deleted the server version.


So there's nothing wrong with de JavaScript right?

I am using java so 'process' is ok in java context.

In any case, I have done umpteen times of debugging on all de codes but just can't submit my form I am thinking of replacing de validation with Jquery for java.

Does anyone know where to get a free and easy plug in or learn de art of it?

jscheuer1
08-12-2013, 05:58 PM
That looks like asp code, the parts with the <% . . . %> brackets around it, is it? If so you can submit to an asp page. But you can submit via javascript, however the results will not be recorded anywhere unless that javascript uses AJAX to call a server side page like PHP or asp to record the results.

And here, in the script you show, there could be other problems, but the highlighted line has to be a comment (add the red comment tokens):


<script language="javascript">
function checkForm(){
//declare an empty Object variable.
var objForm = new Object();
var objFullNameTextBox = new Object();
var objMessageBox = new Object();
var objAddress = new Object();

. . .

Otherwise the code will not run at all.

For jQuery validation, there's:

http://jqueryvalidation.org/

Oh, and it's javascript, not java - java is something else, yet another server side language.

Nile
08-12-2013, 07:07 PM
That looks like asp code, the parts with the <% . . . %> brackets around it, is it?
Actually, I'm not positive, but I think it's JSP. That's where it gets confusing in understanding what the OP is talking about. He could be mentioning Java because he's running JSP; where he says he wants to replace his jQuery validation with Java, he may be saying he wants to transfer the validation responsibilities from client side to server side.

@OP For these reasons, please be extra clear in what you mean and what you're trying to do.

jscheuer1
08-12-2013, 08:35 PM
He's not currently using jQuery for anything. So I think he wants to perhaps replace his current javascript validation with jQuery validation. As to whether or not that's jsp up at the top there in the <% ... %> bracketed area (could well be), and if so, if they want jQuery for Java (which exists, but isn't really for validation, it looks like it's more for controlling jQuery from the server side, but possibly could be adapted to do form validation), you're right, that's not clear.

But the current validation is done, or is being attempted to be being done via ordinary javascript, not particularly well written at that. Replacing that with regular jQuery validation would make more sense.

Oh, and Beverley is right, the action of the form has to be something that will take the data of the form and do something with it. If "process" is something like that, fine. It could possibly be. The way to test that would be to remove the validation code and the onclick event from the submit button. If the form then submits as expected, then "process" is a valid server side script.

What do you expect to happen when the form successfully submits?

One further thing for now, generally validation is done using the on submit of the form event, not on the on click of the submit button event.

Make that another further thing - Even if the current javascript validation can be made to work, and with a fair bit of correction/editing it can (this assumes the asp/jsp part, whatever that is, works OK and that "process" is a valid action for the form):


<%@ page contentType="text/html; charset=UTF-8" %>
<%
String strCookieName = "serverMessage";
String strServerMessage = "";
Cookie objCookies[] = request.getCookies();
Cookie objCookie = null;
if (objCookies != null) {
for (int intIndex = 0; intIndex < objCookies.length; intIndex++) {
if (objCookies[intIndex].getName().equals(strCookieName)) {
objCookie = objCookies[intIndex];
strServerMessage = objCookie.getValue();
}//if
}//for
Cookie objKillMyCookie = new Cookie("serverMessage", "");
objKillMyCookie.setMaxAge(0);
objKillMyCookie.setPath("/");
response.addCookie(objKillMyCookie);
}//if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tutors Registration</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen,projection" />
<link rel="stylesheet" style type ="text/css" href ="style.css">
<script language="javascript">
function checkForm(){
//declare variables.
var objForm;
var objFullNameTextBox;
var objMessageBox;
var objAddress;
var objEmail;
var blnIsAllOkay = true;
var strName;
var strMessage = '';
var strEmail;
var strAddress;
var trimRE = /\s/;

objMessageBox = document.getElementById("divMessage");
objForm = document.getElementById("frmData");
objFullNameTextBox = document.getElementById("FullName");
objAddress = document.getElementById("Address");
objEmail = document.getElementById("Email");

strName = objFullNameTextBox.value;
strEmail = objEmail.value;
strAddress = objAddress.value;


strEmail = trimData(strEmail);
strAddress = trimData(strAddress);

strName = objFullNameTextBox.value;
strName = trimData(strName);
if (strName=="" ){
strMessage += "Please indicate Name<br>";
blnIsAllOkay = false;
}
if (strAddress=="" ){
strMessage += "Please indicate Address<br>";
blnIsAllOkay = false;
}
if (strEmail=="" ){
strMessage += "Please indicate Email<br>";
blnIsAllOkay = false;
}

function trimData(pstrData){
var intIndex=0;
var intRightIndex= pstrData.length -1;
while(intIndex < pstrData.length && trimRE.test(pstrData.charAt(intIndex)))
{++intIndex; }
while(intRightIndex > intIndex && trimRE.test(pstrData.charAt(intRightIndex)))
{--intRightIndex;}
return pstrData.substring(intIndex, intRightIndex+1);
}

if (strMessage.length){
objMessageBox.innerHTML = strMessage;
}
return blnIsAllOkay;

}
function clearErr() {
var objMessageBox = document.getElementById("divMessage");
objMessageBox.innerHTML = "";
}
</script>
</head>
<body>
<div id="wrapper">
<div id="header">

<form id="frmData" name="frmData" action="process" method="post" onsubmit="return checkForm();">
<table>
<tr>
<td>Full Name</td>
<td><input type="text" id="FullName" name="FullName" value=""></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" id="Address" name="Address" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" id="Email" name="Email" value=""></td>
</tr>


<td>
<input type="submit" id="btnSubmit" value="Submit"/>
<input id="reset" type="reset" value="Reset" onclick="clearErr();"/></td>
</tr></table>
<div id="divMessage"></div>
</form>



But it only checks to see if the fields are empty or not. I've seen jQuery validation (the one I linked to in my last post) and it can be much more robust than just that by checking the format of responses to see if they contain expected characters, expected number of characters, spaces and/or numbers in certain places, etc. So could this script, with more tweaking.