Button post HTML
Asked Answered
H

1

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>
Hermie answered 12/1, 2011 at 7:37 Comment(3)
So how will the server find the data if you have no form?Banzai
the closest I would think is ajax, but it is still using get, not postDehydrogenase
@C_Rance: POST can be used for AJAX-requests.Collide
T
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();
}
Thready answered 12/1, 2011 at 8:16 Comment(3)
one question more... how do i set what i want to post since im not getting anything input...Hermie
By either appending "?key1=value1&key2=value2" to the action value. Or by appending hidden input fields to the form.Teach
Here's how to add a hidden input field, for anyone that is unsure: #1001295Girder

© 2022 - 2024 — McMap. All rights reserved.