12

I need to use a button without using a form. How do I make it send post data to the browser?

I am using:

<button style="height: 100px; width: 300px" onClick="parent.location='form1.php'" >Fill survey</button>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user434885
  • 1,988
  • 6
  • 29
  • 51

1 Answers1

32

You'll need to create a form in JavaScript, then submit it. Look at this code

The HTML

<button type="button" onclick="proceed();">do</button> 

The JavaScript code

function proceed () {
    var form = document.createElement('form');
    form.setAttribute('method', 'post');
    form.setAttribute('action', 'http://google.com');
    form.style.display = 'hidden';
    document.body.appendChild(form)
    form.submit();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xavier Barbosa
  • 3,919
  • 1
  • 20
  • 18
  • one question more... how do i set what i want to post since im not getting anything input... – user434885 Jan 12 '11 at 08:30
  • 2
    By either appending "?key1=value1&key2=value2" to the action value. Or by appending hidden input fields to the form. – Gideon Jan 12 '11 at 08:48
  • 4
    Here's how to add a hidden input field, for anyone that is unsure: http://stackoverflow.com/questions/1000795/create-a-hidden-field-in-javascript – Mastergalen Jun 26 '13 at 19:39