Solved

Javascript: Send posted data to a function

Posted on 2004-08-20
3
335 Views
Last Modified: 2011-10-03
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>
0
Comment
Question by:megarry
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
dakyd earned 500 total points
ID: 11852692
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
 

Author Comment

by:megarry
ID: 11853413
Thank you!  
0
 
LVL 19

Expert Comment

by:dakyd
ID: 11855903
Sure thing, glad you got what you wanted.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now