Thursday, 15 January 2015

JavaScript: fill HTML form using JavaScript and submit -



JavaScript: fill HTML form using JavaScript and submit -

i need fill html form this:

<form action="http://www.example.net/index.php" method="post"> <div class="poll"> <p class="poll-answer"> <label><input type='radio' name='option_id' value='12' />abc</label> </p> <p class="poll-answer"> <label><input type='radio' name='option_id' value='34' />def</label> </p> <input type="hidden" name="poll_id" value="56" /> <input type="submit" value="submit!" /> </div> </form>

i need fill using javascript , send it.

i writed:

<script> function post(path) { method = "post"; var form = document.createelement("form"); form.setattribute("method", method); form.setattribute("action", path); var hiddenfield = document.createelement("input"); hiddenfield.setattribute("type", "radio"); hiddenfield.setattribute("name", "option_id"); hiddenfield.setattribute("value", "12"); hiddenfield.setattribute("type", "hidden"); hiddenfield.setattribute("name", "poll_id"); hiddenfield.setattribute("value", "56"); form.submit(); } post('http://www.example.net/index.php'); </script>

but in response there no data. need send form slected abc = value='12'. form action not on domain. have a.com, form @ b.com.

# nc -l 192.168.1.11 -p 80 post /index.php http/1.1 host: example.net connection: keep-alive content-length: 0 cache-control: max-age=0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 origin: null user-agent: mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) chrome/38.0.2125.101 safari/537.36 content-type: application/x-www-form-urlencoded accept-encoding: gzip,deflate accept-language: cs-cz,cs;q=0.8

what bad?

you're forgetting append hidden field form , form document.

<script> function post(path) { method = "post"; var form = document.createelement("form"); form.setattribute("method", method); form.setattribute("action", path); var hiddenfield = document.createelement("input"); hiddenfield.setattribute("type", "radio"); hiddenfield.setattribute("name", "option_id"); hiddenfield.setattribute("value", "12"); form.appendchild(hiddenfield); hiddenfield = document.createelement("input"); hiddenfield.setattribute("type", "hidden"); hiddenfield.setattribute("name", "poll_id"); hiddenfield.setattribute("value", "56"); form.appendchild(hiddenfield); document.body.appendchild(form); form.submit(); } post('http://www.example.net/index.php'); </script>

javascript html forms post

No comments:

Post a Comment