1

I want to upload a text file but I can select which php process will it be based on selection. I tried to combine some codes but it does not work. Here is my html code:

<h3>Upload Time Logs</h3>
<fieldset>
<legend><strong>Select type of machine model</strong></legend>
<select id="cmbmachine" name="machine" class="field" onChange="this.form.action=this.option[this.selectedIndex].value;">
<option value="machine1.php">Machine 1</option>
<option value="machine2.php">Machine 2</option>
</select>
</fieldset>
<fieldset>
    <legend><strong>Select a file to upload</strong></legend>
    <form id="form1" action="" method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" size="40" multiple="multiple" />
     <br />
      <p></p>
       <input type="submit" value="Upload File" />
       <br />
        <br />
    </form>
    <div id="information"></div>
 </fieldset>
 <fieldset>
 <legend><strong>Uploaded Files</strong></legend>
    <div id="uploaded"></div>
 </fieldset>

I tried this suggestions Change Form "action" on selection, javascript - change form action based on selection? but it seems it does not help.. What am I doing wrong?..

Community
  • 1
  • 1
SilverRay
  • 331
  • 2
  • 7
  • 23

2 Answers2

0

I finally did it. I just have text every code I found and gradually apply it to my question. Here is my revised code:

<fieldset>
<legend><strong>Select type of machine model</strong></legend>
<form id="form1" name="form1" method="post" action="/" enctype="multipart/form-data">
<select id="machine" name="machine" class="field" onChange='chgAction()'>
<option value="" selected="selected">Choose..</option>
<option value="machine1">Machine 1</option>
<option value="machine2">Machine 2</option>
</select>
</fieldset>
<fieldset>
    <legend><strong>Select a file to upload</strong></legend>

    <input type="file" id="files" name="files[]" size="40" multiple="multiple" />
     <br />
      <p></p>
       <input type="submit" value="Upload File" id="upload" />
       <br />
        <br />
    </form>
    <div id="information"></div>
 </fieldset>
 <fieldset>
<legend><strong>Uploaded Files</strong></legend>
    <div id="uploaded"></div>
</fieldset>
<script typ="text/javascript">
    function chgAction(){
var form = document.form1;

console.log('chgAction()');
console.log(form.machine.selectedIndex);

switch(form.machine.selectedIndex){
case 1:
  form.action = "/hrtms/machine1.php";
  break;

case 2:
  form.action = "/hrtms/machine2.php";
  break;
}
}
</script>
SilverRay
  • 331
  • 2
  • 7
  • 23
0
<select id="cmbmachine" name="machine" class="field" onChange="addaction(this.value)">
<option value="machine1.php">Machine 1</option>
<option value="machine2.php">Machine 2</option>
</select>
<script>
function addaction(actionvalue)
 {

  $("#form1").attr("action", actionvalue);
};
</script>