www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] multiple variables in document value

  1. #1
    Join Date
    Apr 2009
    Posts
    35

    resolved [RESOLVED] multiple variables in document value

    Title sucks but wasn't sure how else to describe this thread.
    I have multiple forms on one page for posting comments to thumbnails.

    PHP Code:
    print"<form name='piccomment".$dispic."'><textarea id='message".$dispic."' name='message".$dispic."' wrap='soft' row=1 style='width:98%'></textarea>";
    print
    "<input type='button' value='Comment' onClick=\"commented('include/commented2.php', '".$_SESSION['userid']."', '', '', '".$dispic."', '', 'changeallthis2_".$dispic."');\">"
    $dispic is the ID number of each thumbnail. Lets say that 5 thumbnails generate 5 forms so that I get

    form name='piccomment1' with textarea name=message1
    form name='piccomment2' with textarea name=message2
    form name='piccomment3' with textarea name=message3
    form name='piccomment4' with textarea name=message4
    form name='piccomment5' with textarea name=message5

    My JS function named 'commented'
    Code:
    function commented(php_file, member, userip, votedate, dispic, invis, where) {
      var request =  getXMLHTTP();
      var newtext = document.piccomment"+dispic+".message"+dispic+".value; //HERE IS THE ISSUE!!!!
      var  the_data = 'memnum='+member+'&msg='+newtext+'&ip='+userip+'&dated='+votedate+'&dispic='+dispic+'&invis='+invis+'&where='+where;
      request.open("POST", php_file, true);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send(the_data);
      request.onreadystatechange = function() {
    	if (request.readyState == 4) {
    	  document.getElementById(where).innerHTML = request.responseText;
    	}
      }
    }
    Each form is independent. You can submit for piccomment3 without the other 4 being affected. Issue is I cannot figure out how to document.piccomment"+dispic+".message"+dispic+".value so that commenting on Pic3 gets the value of message3 from piccoment3.
    There could be dozens of forms one the one page depending on how many thumbnails are in the database. The dispic number could be any from 1 to infinity.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,102
    are you looking for
    Code:
    var newtext = document["piccomment"+dispic]["message"+dispic].value;
    ?

  3. #3
    Join Date
    Apr 2009
    Posts
    35
    That works on the first form only. Any input in the others is throwing an error in firebug:

    TypeError: document[("piccomment" + dispic)] is undefined


    var newtext = document["piccomment"+dispic]["message"+dispic].value;

  4. #4
    Join Date
    Nov 2010
    Posts
    1,102
    well I guess there is something else going on because if it works on the first it should work on all of them...
    Code:
    <body>
    <form name="piccomment1">
    <textarea name="message1">ta 1</textarea>
    </form>
    <form name="piccomment2">
    <textarea name="message2">ta 2</textarea>
    </form>
    <form name="piccomment3">
    <textarea name="message3">ta 3</textarea>
    </form>
    <form name="piccomment4">
    <textarea name="message4">ta 4</textarea>
    </form>
    <script type="text/javascript">
    for (var i = 1; i < 5; i++) {
    var dispic=i;
    var newtext = document["piccomment"+dispic]["message"+dispic].value;
    alert(newtext);
    }
    </script>
    
    </body>
    the other way would be to pass this.form to the function and reference the text area via form.getElementsByTagName("textarea")[0].value but like I say, the first option works, there's obviously something else going on - if you like, post some rendered html

  5. #5
    Join Date
    Apr 2009
    Posts
    35
    After a break to watch a few hours of Netflix and drink a pot of coffee, it dawned on me that I was not closing any of the forms. after adding </form> to the loop everything works perfectly. Thanks xelowho for your script that helped me see my error.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles