We help IT Professionals succeed at work.

Set Post form at run time in classic ASP

mmalik15
mmalik15 asked
on
In my ASP application on form submit, based on a certain selection inside the dropdownlist I want to post the input to another form. Please see the attached code. The form is posting data to "stage1end_journal_public.asp". But I want to change this form at runtime on a codition.
<FORM NAME="form" METHOD="POST" ACTION="stage1end_journal_public.asp" ONSUBMIT="return validate_form()">

<!-- #Include File="inc/journaldetails.htm" -->

    <TABLE WIDTH="640" CELLPADDING="0" CELLSPACING="0" BORDER="0">
      <TR VALIGN="TOP">
        <TD><HR SIZE="1" COLOR="#000080"> </TD>
      </TR>
    </TABLE>		  
    <TABLE WIDTH="640" CELLPADDING="0" CELLSPACING="0" BORDER="1" 
    BORDERCOLOR="#dfdfdf" BGCOLOR="#efefef">
      <TR>
        <TD>

<!-- Start of Nested Table -->
        
        <TABLE WIDTH="100%" CELLPADDING="4" CELLSPACING="0" BORDER="0">
          <TR>
            <TD width="100"><p><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="Javascript:history.go(-1);">BACK</a> | <a href="index.asp">EXIT</a></strong></font></p>
            </TD>
            <TD ALIGN="right" VALIGN="TOP">
              <div align="right">
                <INPUT TYPE="submit" VALUE="Submit to Library  &gt;&gt;">
              </div></TD>
          </TR>
        </TABLE>

<!-- Nested Table -->
        
        </TD>
      </TR>
    </TABLE>
    
  <P></P></FORM>

Open in new window

Comment
Watch Question

Top Expert 2011

Commented:
You can update the "action" of your form when the user selects the value from the drop-down using the "onchange" event.  This happens before the form is submitted, not "at run time"... that OK?

<select name="chooseAction" id="chooseAction" onchange="javascript:updateAction();" size="1">
  <option value="0"></option>
  <option value="1">Default Location</option>
  <option value="2">Alternate Location</option>
</select>
<script>
function updateAction(){
            var dropdown = document.getElementById("chooseAction");
            var index = dropdown.selectedIndex;
            var selectedAction = dropdown.options[index].value

            switch (selectedAction){
                case '0':{
                    //no value selected
                    document.form.action = 'stage1end_journal_public.asp';
                    break;
                }
                case '1':{
                    document.form.action = 'stage1end_journal_public.asp';
                    break;
                }
                case '2':{
                    document.form1.action = 'someOtherPage';
                    break;
                }
                default:{
                    document.form.action = 'stage1end_journal_public.asp';
                    break;
                }
            }

}
</script>

Open in new window


You can trim the code above depending on whether you have a blank selection in your drop-down or not... I like to make the examples explicit and let you decide where you want to put error messages or combine the options.
Top Expert 2011
Commented:
Please try attached code sample:
<script type="text/javascript" language="javascript">
function funcFormSubmit(objValue){
    document.forms[0].action = objValue;
}

</script>
<FORM NAME="form" METHOD="POST" action="stage1end_journal_public.asp" ONSUBMIT="return validate_form()">

<!--#Include File="inc/journaldetails.htm" -->

    <TABLE WIDTH="640" CELLPADDING="0" CELLSPACING="0" BORDER="0">
      <TR VALIGN="TOP">
        <TD><HR SIZE="1" COLOR="#000080"> </TD>
      </TR>
    </TABLE>		  
    <TABLE WIDTH="640" CELLPADDING="0" CELLSPACING="0" BORDER="1" 
    BORDERCOLOR="#dfdfdf" BGCOLOR="#efefef">
        <tr>
            <td>
                <select name="frmSelect" id="frmSelect" onchange="funcFormSubmit(this.value);">
                    <option value="stage1end_journal_public.asp">Option 1</option>
                    <option value="page2.asp">Option 2</option>
                    <option value="page3.asp">Option 3</option>
                </select>
            </td>
        </tr>
      <TR>
        <TD>

<!-- Start of Nested Table -->
        
        <TABLE WIDTH="100%" CELLPADDING="4" CELLSPACING="0" BORDER="0">
          <TR>
            <TD width="100"><p><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="Javascript:history.go(-1);">BACK</a> | <a href="index.asp">EXIT</a></strong></font></p>
            </TD>
            <TD ALIGN="right" VALIGN="TOP">
              <div align="right">
                <INPUT TYPE="submit" VALUE="Submit to Library  &gt;&gt;">
              </div></TD>
          </TR>
        </TABLE>

<!-- Nested Table -->
        
        </TD>
      </TR>
    </TABLE>
    
  <P></P></FORM>

Open in new window

Author

Commented:
perfect