Ionică Bizău
Ionică Bizău

Reputation: 113465

Submit a form via a button that is not inside of form

Is it possible to submit a form by clicking a button that is not inside of the form?

I have tried to set an id to the form and set the target attribute, but it seems not to work:

<form id="myForm" action="">
    <input type="text">
</form>

<input type="submit" target="myForm" value="submit">

I know it's possible via JavaScript (form.submit()), but is there any HTML native way to submit the form by pressing a button without of the form?

JSFIDDLE

Upvotes: 0

Views: 119

Answers (1)

Venkata Krishna
Venkata Krishna

Reputation: 4305

Use Ajax to send parameters and do the post form action in ajax page and then reset the form with status message below the form.

For ajax call

 var message = $('#message').val();
  //alert(true);
  if(message != '') 
  {
    var data ="message="+message;
    $.ajax({      
      type: 'POST',
      url: 'ajax.php',
      data: data,
      datatype:'json',
      success: function(response) {
        $('#myForm')[0].reset();
        $('#form_status').html(response);
        $('#form_status').fadeIn('slow', function() {
            setTimeout("$('#form_status').fadeOut('slow');", 2000);
          });
      }
    });
  } 

If you are looking for only html5 solution try this below one

<form id="myform" method="get" action="something.php">
    <input type="text" name="name" />
</form>

<input type="submit" form="myform" />

Upvotes: 1

Related Questions