Javascript: Send posted data to a function

I have a form that has an add button to fill value into a listbox.  I also want to be able to post data to it via the URL string and fill the form (actual, send the data to the add function...

Can anyone help me?  It will basicly have the same incoming string as the same string being sent out when submitted..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Batch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
     font-family: Arial, Helvetica, sans-serif;
     font-weight: bold;
     font-size: large;
}
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function postAdd()
{
if BatchIDList

}
function disableEnterKey()
{
     if (window.event.keyCode == 13) window.event.keyCode = 0;
}

function validateNum(dNum){
var reTotal = /^\d{1,8}(\.\d\d?)?$/
dNum=parseFloat(dNum);
    // alert(dNum);
     if(!reTotal.test(dNum)){
          alert("Invalid dollar amount");
              return false;
     }
}

function addToList(listField, newText, newValue) {
   if (validateNum(newValue) == false) {
   return;
   }
   else
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add a blank batch values!");
   } else
   if (IsNumeric(newValue) == false) {
      alert("Please check - non numeric value!");
      } else {
      var len = listField.length++;
       var cntList = len + 1;
      //   var cntTotal = Currency(cntList);
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len;
      document.forms[0].Count.value = cntList;
      updateTotal(listField);
       //alert(cntList);
   }
}

function updateTotal(listField)
{
     total = 0;
     for (i=0; i<listField.length; i++)
     {
         total += parseFloat(listField.options[i].value);
     }

    document.forms[0].Total.value = total;
      document.forms[0].textfield.value = "";
      // document.forms[0].Total.value = document.forms[0].Total.value + newValue;
}

function removeFromList(listField) {
        if ( listField.length == -1) {  
            alert("There are no Batch Numbers which can be removed!");
        } else
        if (confirm("Are you sure you want to remove this Batch?\n\nOK=Yes\nCancel=No")) {
        {
            var selected = listField.selectedIndex;
            if (selected == -1) {
                alert("You must select a Batch Number to be removed!");
            } else {  
            //   var len = listField.length++;
            //  var cntList = len + 1;
                var replaceTextArray = new Array(listField.length-1);
                var replaceValueArray = new Array(listField.length-1);
                for (var i = 0; i < listField.length; i++) {
                    if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
                    if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
                    if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
                    if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
                }
                listField.length = replaceTextArray.length;  
                for (i = 0; i < replaceTextArray.length; i++) {
                    listField.options[i].value = replaceValueArray[i];
                    listField.options[i].text = replaceTextArray[i];
                   document.forms[0].Count.value = i + 1;
                }
          updateTotal(listField);
            }
        }
     } else      {
          return;
               }
}

function IsNumeric(strString)
   //  check for valid numeric strings    
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function HandleSubmit(selected_value){
      var SelectedIdList = "";
      for(var i = 0; i < listField.length; i++) {
            SelectedIdList += (listField.options[i].value + ",");
            }
            SelectedIdList = SelectedIdList.substring(0, SelectedIdList.length-1);
      document.forms[0].BatchIdList.value = SelectedIdList;
      //document.forms[0].submit();
      alert (selected_value);
      
      return true;
}

function ConcListBox(){
      var strValues = "";
      for(var i = 0; i < document.forms[0].selected_value.length; i++) {
            strValues += (document.forms[0].selected_value.options[i].value + ",");
            }
      //remove the last comma.
      strValues = strValues.substring(0, strValues.length-1);
      document.forms[0].BatchIdList.value = strValues;
      }

//-->
</script>
    <SCRIPT LANGUAGE=javascript>
    <!--
    var isAltKeyPressed = false;

    function hotkey(eventname)
    {
     
        if(eventname.keyCode == 18)
            {
                  isAltKeyPressed = true;

            }
            if(eventname.keyCode == 83 && isAltKeyPressed)
            {
                  post();
            }
              else
              if(eventname.keyCode == 43)
            {
                  addPost();
            }
             
          
    }
function post(){

 // document.forms[0].elements['submit'].click();
HandleSubmit();

}
function addPost(){

 document.forms[0].elements['addItm'].click();

}
    //-->
    </SCRIPT>
</head>

<body onkeydown='hotkey(event)'>

<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3"><span class="style1">Batch Tape </span></td>
      </tr>
      <tr>
        <td width="10%">Batch # </td>
        <td colspan="2">
          <input type="text" name="textfield1" READONLY>          </td>
      </tr>
    <!--   <form method="post" name="TheData" id="TheData" onsubmit="selectAllOpts();" onKeyPress="disableEnterKey()">  -->
               <form method="post" onKeyPress="disableEnterKey()">

      <tr>
        <td>&nbsp;</td>
        <td colspan="2"><input type="text" name="textfield">
 <input name="addItm" type="button" id="addItm" onClick="addToList(selected_value, textfield.value, textfield.value);" value="add"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2">
<select name="selected_value" id="selected_value" size="25" multiple>
</select>
 <input name="removeItm" type="button" id="removeItm" onClick="removeFromList(selected_value);" value="remove"></td>
      </tr>
      <tr>
        <td><input name="Count" id="cntList_value" type="text" READONLY></td>
        <td width="10%"><input name="Total" type="text" id="Total" READONLY></td>
        <td width="80%">Total</td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
        <td>
          <div align="right">
            <input name=submit type=submit value="submit">
          </div></td>
        <td><input type="submit" name="Submit4" value="Cancel"></td>
        <td><input type="text" name="BatchIdList" value=""><input type="button" name="test" value="test" onClick="javascript:ConcListBox();"></td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
      </tr>
</table></form>
</body>
</html>
megarryAsked:
Who is Participating?
 
dakydConnect With a Mentor Commented:
Will this work for you?  I assumed that when you load this page, the URL will have "BatchIdList=" followed by a comma-delimited list of values to add to your listBox.  Also, I noticed you said you wanted to "post" the data to the URL - if you want something to show up in the URL, you have to use the "GET" method instead of "POST".  Anyhow, the script is below, hope it helps.

Add this function to your script:
function addFromUrl()
{
  var start = document.location.href.indexOf("BatchIdList");
  var end = document.location.href.indexOf("&", start + 1);
  if (end < 0)
    end = document.location.href.length - 1;
  var valList = unescape(document.location.href.substr(start, end)).split("=")[1];
  var vals = valList.split(",");
  var theSel = document.forms[0].selected_value;
  for (var i = 0; i < vals.length; i ++)
    addToList(theSel, vals[i], vals[i]);
}

and then change your body to this:
<body onkeydown='hotkey(event)' onload="addFromUrl()">
0
 
megarryAuthor Commented:
Thank you!  
0
 
dakydCommented:
Sure thing, glad you got what you wanted.
0
All Courses

From novice to tech pro — start learning today.