Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I love this site! It's so nice to know that there are so many people out there who are willing to share their knowledge..."

Geography

Where in the world do Tek-Tips members come from?

Issue with Ajax and JSP form Helpful Member! 

shijingeorge (Programmer)
18 Jul 11 18:44
In the below JSP when I enter values for the text box and radio button and click Submit, the results are displayed on the page only momentarily and then it disappears. I am not sure what is the issue with code and I have spent hours on the issue. Please help.

<html>
<head>
<title>Java Solution AJAX</title>
<script type="text/javascript">
var request;
function getTemp() {
var value = document.getElementById("value").value;

for ( var i = 0; i < document.temp.Degree.length; i++) {
if (document.temp.Degree[i].checked) {
var Degree = document.temp.Degree[i].value;
break;
}
}

//var Degree = document.getElementById("Degree").value;
var url = "http://localhost:8080/mc1/ConverterServlet?value=" + value
+ "&Degree=" + Degree;
if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
}
request.onreadystatechange = showResult;
request.open("GET", url, true);
request.send();
}
function showResult() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseXML;
var temparatures = response.getElementsByTagName("Temperature");
var temparature = temparatures[0];
document.getElementById("inputTempS").innerHTML += temparature
.getElementsByTagName("inputTemp")[0].text;
document.getElementById("outputTempS").innerHTML += temparature
.getElementsByTagName("outputTemp")[0].text;
document.getElementById("conversionS").innerHTML += temparature
.getElementsByTagName("conversion")[0].text;
}
}
}
</script>
</head>
<body>

<form name="temp" action="">
<table>

<tr>
<td>
Degrees:
</td>
<td>
<input id="value" type="text" name="value" value=""
maxlength="100" />
</td>
</tr>
<tr>
<td>
Conversion:
</td>
<td>
<input id="Degree" type="radio" name="Degree" value="CelsiustoFah" />
Celsius to Fah
<input id="Degree" type="radio" name="Degree" value="FahtoCelsius" />
Fah to Celsius
</td>
</tr>

<tr>
<td>
</td>
<td>
<input type="submit" value="Submit" onclick="getTemp();" />
</td>
</tr>
</table>
</form>
<div id="new">
<p>
The Input Temperature :
<span id="inputTempS"></span>
</p>
<p>
The Output Temperature :
<span id="outputTempS"></span>
</p>
<p>
The Conversion :
<span id="conversionS"></span>
</p>
</div>

</body>
</html>
Helpful Member!  feherke (Programmer)
19 Jul 11 3:20
Hi

Quote (shijingeorge):

click Submit, the results are displayed on the page only momentarily and then it disappears.
Well, the submit button's default behavior is to submit the form. So that it does.

If you want to stop that, return false from the event handler :

CODE --> HTML ( fragment )

<input type="submit" value="Submit" onclick="getTemp(); return false" />
Note that a form can be submitted in various ways, pressing the submit button is only one of them. So the recommended practice is to handle the forms onsubmit event instead :

CODE --> HTML ( fragment )

<form name="temp" action="" onsubmit="getTemp(); return false">
( Of course, in this later case you should remove the current onclick handler. )
 

Feherke.
http://free.rootshell.be/~feherke/

shijingeorge (Programmer)
19 Jul 11 12:47
Thanks Feherke . Thank you so much for the suggestion. It worked!!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close