We help IT Professionals succeed at work.

javascript upload object

Webboy2008
Webboy2008 asked
on

<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
Comment
Watch Question

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";
        }
    }
Owner (Aidellio)
Most Valuable Expert 2015
Commented:
You can do that like this, see here http://jsfiddle.net/rjurd/Bqt9H/ 

javascript
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');
    }
    pf2.onclick = function() {
        myform.setAttribute('action','abc.asp');
    }
}

Open in new window


html

<form id='myform' enctype='multipart/form-data'>
    <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

Top Expert 2013

Commented:
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&
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
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

Author

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

thanks
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
Please refer to my post above.  pure javascript no frameworks

Author

Commented:
<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
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
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