Javascript: problem passing value for warning from hidden tag or text

I have a html file that feeds values ​​to a Java Servlet for insertion into a 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 click on the button, the warning (partnumber) returns null (firefox) or object (IE). Am I passing these values ​​incorrectly for servlet processing? Any ideas why? shouldn't it return value?

early

0


a source to share


3 answers


Firefox returns null because there is no identifier for that name. Call GetElementByName

if you don't want to assign IDs to your inputs.

IE returns an object since DOM elements are objects. You need to check one of its attributes ( value

most likely) to see what it is actually set to.



IE shouldn't return an object, but it naively thinks it is the GetElementById

same as GetElementByName

.

+3


a source


Classic IE error ID and name . You will need to specify or use id attributes, or use getElementsByName (be careful, it will return a "set" of matches) or the format form.elements [nameOrIndex].

eg.



alert(document.forms[myFormNameOrIndex].elements[myElementNameOrIndex].value);

      

+1


a source


I see three things that are odd (and probably problems):

  • How do you set the id "partnumber"? IE giving you an object, it seems like you only set the name attribute of the input field, not the id attribute.
  • You must get the value of these properties to build the URL: partnumber.value

  • You are using a method GET

    for what looks like an insert. It should be POST

    .
0


a source







All Articles