0

I am trying to use multiple submit forms in javascript but it opens about:blank along with the requested form actions URLs in next tab

my code:

<form class="search_content" action="/" method="post" target='_blank' id="form1" name="form1">
 
      <input type="text" id="lname" name="lname" required>
     
      <input type="checkbox" id="myCheck">
<input type="checkbox" id="myCheck1" >
<input type="checkbox" id="myCheck2" >

   
  
</form>

<button  onclick="submitForms()">
        Go Search !
      </button>

my javascript code

as you see I use the check box to enable the different action URLs... when I run my code, it will open three new tabs and one "about:blank" tab. which should not open in my code

can anyone help me with how to do this kind of multiple action method with javascript and checkbox

<script>
function submitForms() {
  var checkBox = document.getElementById("myCheck");
  var checkBox1 = document.getElementById("myCheck1");
  var checkBox2 = document.getElementById("myCheck2");

  
  if (checkBox.checked == true){
    document.getElementById("form1").action =
            "https://www.example.com/action_page.php";
            document.getElementById("form1").target='_blank';
            document.getElementById("form1").submit();
    
  }   
  if (checkBox1.checked == true){
    document.getElementById("form1").action =
            "https://www.example1.com/action_page.php";
            document.getElementById("form1").target='_blank';
            document.getElementById("form1").submit();
  } 
  
  if (checkBox2.checked == true){
    document.getElementById("form1").action =
            "https://www.example2.com/action_page.php";
            document.getElementById("form1").target='_blank';
            document.getElementById("form1").submit();
  }else{

            
            
  }
  
  
}
</script>
MCRPL
  • 9
  • 3
  • Did you try `preventDefault()`? – Christian Vincenzo Traina Jul 13 '22 at 10:36
  • hi @CristianTraìna i am new to javascript... so i followed mostly from StackOverflow only.. and i am not familiar with preventDefault... and I am not sure how to use this method "preventDefault" inside my code.. could you guide me – MCRPL Jul 13 '22 at 10:40
  • Well, you shouldn't start from StackOverflow, you should start from tutorials, books, and so on. StackOverflow should be your last resort :) – Christian Vincenzo Traina Jul 13 '22 at 10:46
  • Understood, thanks @CristianTraìna as you said, i will start from tutorials... thank you.. – MCRPL Jul 13 '22 at 10:48
  • @CristianTraìna yes it seems the answers seem to work for my code as well, I will do some basic learning and improve myself as you said... – MCRPL Jul 13 '22 at 10:51

0 Answers0