Fetching Form Values with ASP

Discussion in 'C#' started by cancer10, Mar 15, 2007.

  1. #1
    Hey!

    I came up with the new problem. I want to fetch all form values by using request.form("FormName") which is generated by javascript on runtime.

    The attached zip code contains the working code of javascript creating dynamic form elements. So after generating a couple of textboxes when I click on the "Submit" button it should display all the values entered in the textboxes on the next page.

    Herez a working example of that javascript: http://www.quirksmode.org/dom/domform.html

    Is this Possible?


    Thanx
     

    Attached Files:

    cancer10, Mar 15, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    just iterate over request.form - it's a little tricky to have dynamic fieldnames but it's fun :) you will have to split your formnames for extra info like <input type="text" name="ProdID-3_ProdName-SuperDuper_ProdCatID-104" value="Hello World" />

    Then on post, you can detect - did this form element start with "ProdID" ok - I know i have to interpret this field. Then split on character _. Then split them on character -. So essentially, you have 4 items in 1 form field + the inputted value. (Hope this helps)
     
    ccoonen, Mar 15, 2007 IP
  3. DaDMan

    DaDMan Guest

    Messages:
    566
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not too sure to understand ccoonen explanation but might be working! :)

    You could loop through the Form Collection using either the form count or the request.form collection.
    I personnally prefer and have always used the second method ( request.form collection, but guess that would be a matter of personnal choice more than any other thing.. )


    Here's your file ( I've renamed the html for asp.. )
    So you have the fields name with they're values listed in either way you used.. without actually knowing the name of the fields...

    
    <%
    'The form as been submitted then loop trhough the values and display them
    If request.Form("sendform") <> "" Then
       response.write "Displaying the form fields with the form count method<br>"
       For x = 1 to Request.Form.Count
           Response.Write Request.Form.Key(x) & " = "     'that's the field name
           Response.Write Request.Form.Item(x) & "<br>"   'that's the field value
       Next
       response.write "<br>"
       response.write "Displaying the form fields with the field object method<br>"
       
      'the "fldName" could be anything.. it's a variable.. so it could be for each "f" in request.form 
      'then you use "f" in request.form(f)
       For Each fldName in Request.Form
          Response.Write fldName & " = "      'thats the field name
          Response.write Request.Form(fldName) & "<br />"   'thats the field value
       Next
    End if
    %>
    <script type="text/javascript" src="quirksmode.js"></script>
    <script type="text/javascript">
    <!--
    
    var counter = 0;
    
    function init() {
    	document.getElementById('moreFields').onclick = moreFields;
    	moreFields();
    }
    
    function moreFields() {
    	counter++;
    	var newFields = document.getElementById('readroot').cloneNode(true);
    	newFields.id = '';
    	newFields.style.display = 'block';
    	var newField = newFields.childNodes;
    	for (var i=0;i<newField.length;i++) {
    		var theName = newField[i].name
    		if (theName)
    			newField[i].name = theName + counter;
    	}
    	var insertHere = document.getElementById('writeroot');
    	insertHere.parentNode.insertBefore(newFields,insertHere);
    }
    
    // -->
    </script>
    
    
    <div id="readroot" style="display: none">
    <hr />	
    <select name="rankingsel">
    <option>Rating</option>
    <option value="excellent">Excellent</option>
    <option value="good">Good</option>
    <option value="ok">OK</option>
    <option value="poor">Poor</option>
    <option value="bad">Bad</option>
    </select>
    &nbsp;&nbsp;
    <select name="rankingsew">
    <option>Rating</option>
    <option value="excellent">Excellent</option>
    <option value="good">Good</option>
    <option value="ok">OK</option>
    <option value="poor">Poor</option>
    <option value="bad">Bad</option>
    </select>		
    &nbsp;&nbsp;
    <input name="cd" value="title" />
    &nbsp;&nbsp;
    <input type="button" value="Remove review" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
    
    <BR /><BR />
    </div>
    
    <!-- <form method="post" action="/cgi-bin/show_params.cgi"> -->
    <form method="post" action="index.asp">
    
    	<span id="writeroot"></span>
    
    	<input type="button" id="moreFields" value="Give me more fields!" />
    	<input type="submit" name="sendform" value="Send form" />
    
    </form>
    
    Code (markup):
     
    DaDMan, Mar 15, 2007 IP