1

I have a form:

<form class=" id="form-edit_usr" action="edit_usr.php" method="post" enctype="multipart/form-data">

Inside of that form i have another form:

<button id="usrPhoto" class="btn"> 
   <img src="" width='100' height='140'>
</button> 

  <form id="uploadForm" method="post">
    <input type="file" id="uploadPhoto" name="uploadPhoto" data-usr="" style="display: none;"/> 
  </form>

To the "main" for i have a "submit button"

  <input type="submit" class="btn" value="Update" />
</form>

With Jquery i control the file input:

$('#usrPhoto').on('click', function(){
    //Check if user is selected
    selectedUsr = $('#choosen_usr_email').val();
    if(selectedUsr){
        //If user is selected open file dialog
        $("#uploadPhoto").click();
    }
})

My problem is when i press the #userPhoto element,
#uploadPhoto shall open file dialog, but it also submits my main form.

How can i prevent the .click() from submitting all forms/Buttons?

Björn C
  • 3,860
  • 10
  • 46
  • 85

2 Answers2

3

By the HTML specification. You cannot have a form inside another form.

Check your resulting html in your browser's inspector and you will see the "inner form" is missing.

You could break them in 2 separate forms or use pure JS to upload the file. There's a question (and answers) about it here.

SparK
  • 5,181
  • 2
  • 23
  • 32
0

Maybe you can using the jQuery submit function

$('.button').on('click',function(){
  $('.formID').submit();
});
Felix Fong
  • 969
  • 1
  • 8
  • 21