javascript - Submit a form from a link and get $_Post value -
i have following problem. have form containing several links. links should assign hidden field , submit form using javascript.
i'm using php 5.4.
when submit form using button, $_post containts data. when form submitter javascript, $_post empty.
any clue?
<head> <script language="text/javascript"> function affect(nummet) { document.forms['frmdemo'].num_met_choose.value = nummet; document.forms['frmdemo'].submit(); } </script> </head> <body> <?php var_dump($_post); ?> <form name="frmdemo" enctype="multipart/form-data" method="post"> <input name="num_met_choose" id="num_met_choose"> <a href="" onclick="javascript:affect('22');" >test 1</a> <a href="" onclick="javascript:affect('12');" >test 2</a> <button id="submit" type="submit">ok</button> </form>
you have 2 problems.
the first this:
document.forms['frmdemo'].submit();
combined this:
<button id="submit" type="submit">ok</button>
by having form control id="submit"
inside form, overwrite submit
property of the form object. instead of being function can call, becomes reference element id submit
.
you need use different id (or none @ since don't seem using anything).
you should have got error in browser's javascript console. keep , eye on when js isn't behaving expect.
even if fix that, have problem:
<a href="" onclick="javascript:affect('22');" >test 1</a>
- the javascript runs
- the form submit
- the link followed form isn't submitted after all
don't use link thing run js (unless want function link, or setting use link fallback).
use button (with type="button"
) instead.
that said. don't use javascript @ this. can same effect plain html.
<form enctype="multipart/form-data" method="post"> <button type="submit" name="num_met_choose" value="22">test 1</button> <button type="submit" name="num_met_choose" value="12">test 2</button> </form>
Comments
Post a Comment