#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    4
    Rep Power
    0

    Emergency!!: how to active a javascript function and pass the value generated from th


    How to active a javascript function and pass the value generated from the function to a hidden form on the same page?

    Details: on this html page there're checkbox selections and a hidden form. I want to transfer the selected checkbox values into one variable by using some javascript function. Then I'd like to pass the variable generated from the function to the hidden form on the same page. Finally I will pass the value via form to another asp page.

    Relevant code 1: the function to transfer the selected checkbox values into one variable:

    <SCRIPT language=Javascript>
    <!--
    function checkboxselections()
    {
    var words=" ";
    for (i=0; i<=10; i++)
    {
    {if (document.pref_form.elements[i].checked)
    words = words + " + AND + " + prefArray[i];}
    }
    }

    Relevant code 2: to pass the value via form to another asp page

    <form name="myform" action="search.asp" method="post"
    onSubmit="this.alfavalue.value=word;">
    <input type="hidden" name="alfavalue" value=word>
    <input type="submit" value="Submit selections"></p>
    </form>

    I'm really lost as how to active the javascript function and then pass the value generated from the function to that hidden form. Or, is there another nifty way to do that? All I want is to pass the checkbox values to another asp page (not as array but as character string cause I want to conduct keyword search from the next asp page using the passed character string)!! Thanks so much!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    135
    Rep Power
    12
    to active a js function you can:

    use an chor
    <a href="javascript:myfunction(arg1,arg2,...);">launch the js func </a>

    use a button:
    <input type="button" onclick="myfunction(arg1,arg2,...);">

    an image:
    <img src="thing.jpg" onclick="myfunction(arg1,arg2,...);">

    onmouseover, onkeyup,onkeydown, onmouseout , onclick ... events in a html tag.

    ...



    To assign a value to a hidden form:
    function assign_val(val,loc){
    loc.value=val;
    }

    and then you'll just have to call
    assign_val('new value', document.myform.alfavalue);
    or if the value is in a var:
    assign_val(var_where_the_value_is_name, document.myform.alfavalue);
    if you have
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    4
    Rep Power
    0
    Thanks for the reply...I guess I'm really a Newbie and still feel somewhat confused.

    The problem I was haing was that I wanted to read a VARIABLE not a CONSTANT. For some reason, the only way I was able to get a VAR read was to use the FORM method. It seems (maybe my syntax is off?) that when I passed my variable to a var before the submit action I had trouble reading that var, which should be defined by the function.

    Following is the latest try-out code. I'm still having trouble in reading and passing the values...

    <br><input type="checkbox" name="question_id" value="0">0
    <br><input type="checkbox" name="question_id" value="1">1
    <br><input type="checkbox" name="question_id" value="2">2
    <br><input type="checkbox" name="question_id" value="3">3
    <br><input type="checkbox" name="question_id" value="4">4
    <br><input type="checkbox" name="question_id" value="5">5
    <script language="JavaScript">

    function select_id(){
    var a = document.getElementsByName("question_id");
    question_title.value = '';
    for (i=0;i<a.length;i++) if (a[i].checked) question_title.value += a[i].value+',';
    if (question_title.value == '') alert ('Select Please!');
    }
    </script>

    <form><input></form>
    <form method="post" action="search.asp" onSubmit="elements[1].value=forms[0].elements[0].value">
    <input><input><input type="submit">
    </form>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    135
    Rep Power
    12



    Thanks for the reply...I guess I'm really a Newbie and still feel somewhat confused.

    The problem I was haing was that I wanted to read a VARIABLE not a CONSTANT. For some reason, the only way I was able to get a VAR read was to use the FORM method. It seems (maybe my syntax is off?) that when I passed my variable to a var before the submit action I had trouble reading that var, which should be defined by the function.

    Following is the latest try-out code. I'm still having trouble in reading and passing the values...

    <br><input type="checkbox" name="question_id" value="0">0
    <br><input type="checkbox" name="question_id" value="1">1
    <br><input type="checkbox" name="question_id" value="2">2
    <br><input type="checkbox" name="question_id" value="3">3
    <br><input type="checkbox" name="question_id" value="4">4
    <br><input type="checkbox" name="question_id" value="5">5
    <script language="JavaScript">

    function select_id(){
    var a = document.getElementsByName("question_id");
    question_title.value = '';
    for (i=0;i<a.length;i++) if (a[i].checked) question_title.value += a[i].value+',';
    if (question_title.value == '') alert ('Select Please!');
    }
    dirty.
    allways give a name to your forms:
    <form name="form1">
    ....
    </form>

    what are your <input> for do you mean
    that you have some iputs inside?

    try var a=document.form.question_id.value
    and then replace any of your
    question_id by a,


    here is smething cleaner:


    <script>
    function select_id(){

    document.form1.question_title.value ='';
    for (i=0;i<document.form0.elements.length;i++) if (document.form0.elements[i].checked) document.form1.question_title.value += document.form0.elements[i].value+',';
    if (document.form1.question_title.value == '') {
    alert ('Select Please!');
    return false;
    }
    return true;
    }
    </script>

    <form name="form0">
    <br><input type="checkbox" name="question_id1" value="0">0
    <br><input type="checkbox" name="question_id2" value="1">1
    <br><input type="checkbox" name="question_id3" value="2">2
    <br><input type="checkbox" name="question_id4" value="3">3
    <br><input type="checkbox" name="question_id5" value="4">4
    <br><input type="checkbox" name="question_id6" value="5">5
    </form>
    <form name="form1" method="post" action="search.asp" onSubmit='return select_id()'>
    <input type="hidden" name="question_title" value=''>
    <input type="submit">
    </form>

IMN logo majestic logo threadwatch logo seochat tools logo