I have an html file which passes values into a java servlet to insert into the database. Here is my code:
<html>
<head>
<script type="text/javascript">
function masinsert(id)
{
var currentTime=new Date();
var button = document.getElementById("m"+id);
button.onclick="";
button.value="Inserting";
var partnumber = document.getElementById("partnumber"+id).value;
var itemcost = document.getElementById("itemcost"+id).value;
var itemlistprice = document.getElementById("itemlistprice"+id).value;
var sUnitMeasKey = document.getElementById("UnitMeasKey"+id);
var sPurchProdLine = document.getElementById("PurchProdLine"+id);
var sItemClassKey = document.getElementById("itemclasskey"+id);
var UMKselected = getSelected(sUnitMeasKey);
var PPLselected = getSelected(sPurchProdLine);
var ICKselected = getSelected(sItemClassKey);
function handleHttpResponse()
{
if (http.readyState == 4) {
button.value="Imported";
}
}
var http = getHTTPObject(); // We create the HTTP Object
var tempUrl = "\MASInsert2";
tempUrl += "?partnumber="+partnumber+"&"+"itemcost="+itemcost+"&"+"itemlistprice="+itemlistprice+"&"+"UnitMeasure="+UMKselected+"&"+"PurchProdLine="+PPLselected+"&"+"itemclasskey="+ICKselected;
alert(tempUrl);
http.open("POST", tempUrl, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function getSelected(ele)
{
return ele.options[ele.selectedIndex].value;
}
function getHTTPObject(){
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
</script>
</head>
<body>
<form name=bob method=get>
<input type=hidden id="partnumber321" name="partnumber321" value="884910U">
<input type=hidden id="itemcost321" name="itemcost321" value="1027.39">
<input type=hidden id="itemlistprice321" name="itemlistprice321" value="1129.0">
<input type=hidden id="itemdescription321" name="itemdescription321" value="XSERIES 306M SS P4 3.0GHZ 1MB">
<select id="UnitMeasKey321" name="UnitMeasKey321"><option value="112">Each</select>
<select id="PurchProdLine321" name="PurchProdLine321"><option value="18">IBM</select>
<select id="itemclasskey321" name="itemclasskey321"><option value="48">Hardware</select>
<input id="m321" type="button" onclick="masinsert('321')" value="Add">
</form>
</body>
</html>
When I hit the button, the alert(partnumber) returns null (firefox) or object (IE). Am I passing these values incorrectly for the servlet to process? Any ideas why? shouldn't it return a value?
thanks in advance
Firefox is returning null because there is no ID by that name. Call GetElementByName
if you don't want to assign ids to your inputs.
IE is returning an object because DOM elements are objects. You need to check one of its attributes ( value
most likely) to see what it's actually set to.
IE shouldn't return an object, but it naively assumes that GetElementById
is the same as GetElementByName
.
Classic IE bug ID vs. Name . You will either need to specify and use id attributes, or use getElementsByName (beware it will return a "set" of matches) or the form.elements[nameOrIndex] format.
eg
alert(document.forms[myFormNameOrIndex].elements[myElementNameOrIndex].value);
I see three things that are odd (and probably the issues):
partnumber.value
GET
method for what seems like an insert. This should be a POST
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.