Dipanshu Mahla
Dipanshu Mahla

Reputation: 152

Changing form action location

I want to change the form action location Depending on what user selects:

<form action="upload.php" method="post" id="uploadForm" enctype="multipart/form-data" onsubmit="verify(event,this)">
    <input id="fileToUpload" name="fileToUpload"type="file"/>
    Select File
    <input id="filesToUpload" name="filesToUpload" type="file" webkitdirectory directory multiple/>
    Select Folder
    <input type="submit" value="Upload" name="submit" class="cfu">
</form>

Function is:

   function verify(e,t){
    if(!document.getElementById("fileToUpload").value){
        document.uploadForm.action = "zipping.php";
        t.submit();
    }
    else{
        t.submit();
    }
}

But this doesn't work.

Upvotes: 0

Views: 643

Answers (1)

David
David

Reputation: 218847

If I remember correctly, form properties on the document object are by name, not id:

<form action="upload.php" method="post" name="uploadForm" ...
                                        ^---here

Also, if you're not preventing the form from submitting, you don't need to call .submit() on anything (if that even works in the first place):

function verify(e,t){
    if(!document.getElementById("fileToUpload").value){
        document.uploadForm.action = "zipping.php";
    }
}

Demo, which will of course produce a 404 but you can examine the requested URL in your debugging tools:

<form action="upload.php" method="post" name="uploadForm" enctype="multipart/form-data" onsubmit="verify(event,this)">
  <input id="fileToUpload" name="fileToUpload"type="file"/>
    Select File
  <input id="filesToUpload" name="filesToUpload" type="file" webkitdirectory directory multiple/>
    Select Folder
  <input type="submit" value="Upload" name="submit" class="cfu">
</form>
<script>
function verify(e,t){
    if(!document.getElementById("fileToUpload").value){
        document.uploadForm.action = "zipping.php";
    }
}
</script>

Upvotes: 1

Related Questions