Link to home
Start Free TrialLog in
Avatar of Webboy2008
Webboy2008

asked on

javascript upload object


<input checked type=""radio"" id=""PF""  name=""rdopaymenttype""   value=""4"">Nothing
<input checked type=""radio"" id=""PF""  name=""rdopaymenttype""   value=""5"">Upload

How can I do the following in javascript?  Thanks

if the rdopaymenttype == 5 Then
<form action="abc.asp" enctype=multipart/form-data>
else
<form action="xyz.asp">
End if
Avatar of wdfdo1986
wdfdo1986
Flag of Sri Lanka image

have a name to the form and then you can call the form from javascript. Then you can change action using some code like below

NOTE: This is just an example

function chgAction( action_name )
    {
        if( action_name=="aaa" ) {
            document.forms[0].action = "/AAA";
        }
        else if( action_name=="bbb" ) {
            document.forms[0].action = "/BBB";
        }
        else if( action_name=="ccc" ) {
            document.forms[0].action = "/CCC";
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
change the form action to action="myscript(this)"

then the function inthe js would be:
function myscript(x)
{
   if (x.rdopaymenttype[0].checked)
   {
      x.action='abc.asp';
      x.enctype='multipart/form-data';
   {
   else
   {
   x.action='xyz.asp';
   x.enctype='text/plain';
   }
   x.submit();
}

Open in new window

 


Cd&
Here is the code to deal with this

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<script>
  $(document).ready(function(){
    
    update_form_attributes();
    
    $("input[name=rdopaymenttype]").click(function(){
      update_form_attributes();
    });
    
  });
  
  function update_form_attributes()
  {
     if($("input[name=rdopaymenttype]:checked").val() == "5")
     {
        $("form").attr("action","abc.asp").attr("enctype","multipart/form-data");
     }
     else
     {
       $("form").attr("action","xyz.asp");
       $("form").removeAttr("enctype");
     }
  }
</script>
<body>
  

<form>
  <input type="radio" id="PF"  name="rdopaymenttype" checked   value="4">Nothing
<input type="radio" id="PF"  name="rdopaymenttype"   value="5">Upload
  </form>
</body>
</html>

Open in new window

Avatar of Webboy2008
Webboy2008

ASKER

It is classic asp and javascript. No asp.net or jquery please.

thanks
Please refer to my post above.  pure javascript no frameworks
<form id='myform' enctype='multipart/form-data'> is not supposed to be there as default.
because if first radio is selected, it will not be any upload there. remember, if it is 4, then
it acts normal like http post, when it is 5, it show browse button to allow upload.

Thanks
ok so the html goes back to:

<form id='myform'>
    <label for='PF1'>Nothing</label>
    <input checked type="radio" id="PF1" name="rdopaymenttype" value="4" />
    <label for='PF2'>Upload</label>
    <input checked type="radio" id="PF2" name="rdopaymenttype" value="5" />
</form>

Open in new window


and the javascript is slightly changed to:

window.onload = function() {

    var pf1 = document.getElementById('PF1');
    var pf2 = document.getElementById('PF2');
    var myform = document.getElementById('myform');
    pf1.onclick = function() {
        myform.setAttribute('action','xyz.asp');
        myform.removeAttribute('enctype');
    }
    pf2.onclick = function() {
        myform.setAttribute('action','abc.asp');
        myform.setAttribute('enctype','multipart/form-data');
    }
}

Open in new window