Languages :: PHP :: Posting vars via javascript |
|||
| By: kaller2 |
Date: 11/04/2003 00:00:00 |
Points: 100 | Status: Answered Quality : Excellent |
|
This is really a javascript question I think. What's the best way post a form variable to the server when the user selects a piece of text? Should I use a hidden input tag? I am trying this sort of thing: <script language="javascript"> function setval(id, name, val) { var obj = document.getElementById(id); obj.setAttribute(name,val); } </script> and <form method="post"> <input type="hidden" id="a" /> <div onclick="setval('a','b', '123');">text</div> <input type=submit> </form> Should this work? |
|||
| By: kaller2 | Date: 12/04/2003 05:29:00 | Type : Comment |
|
| Here's a test prog that doesn't work - how to fix? <html> <head> <script language="javascript"> function setval(id, name, val) { var obj = document.getElementById(id); obj.setAttribute(name,val); var output = obj.getAttribute(name); alert(output); } </script> </head> <body> <?php if ($_POST["myvar"]) { echo "<pre>"; print_r ($_POST); echo "</pre>"; } ?> <form method="post"> <input type="hidden" id="myid" name="myvar" value="123" /> <div onclick="setval('myid','myvar', '456');">text</div> <input type="submit" /> </form> </body> </html> |
|||
| By: kaller2 | Date: 12/04/2003 18:46:00 | Type : Comment |
|
| Maybe the form doesn't know about the change? Maybe I need to reference the input tag via the form? |
|||
| By: kaller2 | Date: 12/04/2003 19:26:00 | Type : Comment |
|
| Hmm. This works though. <html> <head> <script language="javascript"> var somevar; </script> </head> <body> <?php if (isset($_POST)) { echo "<pre>"; print_r ($_POST); echo "</pre>"; } ?> <form method="post" onsubmit="myvar.value=somevar;" > <input type="hidden" id="myid" name="myvar" value="123" /> <div onclick="somevar='456';">Click this</div> <div onclick="somevar='789';">Or this</div> <input type="submit" /> </form> </body> </html> |
|||
| By: kaller2 | Date: 12/04/2003 21:08:00 | Type : Comment |
|
| Slight improvement: <html> <body> <?php if (isset($_POST)) { echo "<pre>"; print_r ($_POST); echo "</pre>"; } ?> <form name="myform" method="post" onsubmit="myvar.value=formvar;" > <input type="hidden" name="myvar" /> <div onclick="document.myform.formvar='123';">Click this</div> <div onclick="document.myform.formvar='456';">or this</div> <input type="submit" /> </form> </body> </html> |
|||
| By: kaller2 | Date: 12/04/2003 21:12:00 | Type : Comment |
|
| hehe, how do I give myself points? |
|||
| By: VGR | Date: 12/04/2003 21:46:00 | Type : Answer |
|
| it's Saturn's day and the Sun shines :D do you still need any help ? :D |
|||
| By: kaller2 | Date: 12/04/2003 22:36:00 | Type : Comment |
|
| The pragmatist in me (like lost programmers) says I can just use what I dreamed up, so no thanks, I don't need help, but the theorist in me still needs some kind of enlightenment.. Happy to give points to anyone for a simple explanation of why the first method failed. |
|||
| By: kaller2 | Date: 12/04/2003 23:52:00 | Type : Comment |
|
| Duh, this works, myvar just needs to declared via the form in the first place! Hopefully somebody will find this useful anyway. You're right it is a nice day. Have some points VGR. Cheers. <html> <body> <?php if (isset($_POST)) { echo "<pre>"; print_r ($_POST); echo "</pre>"; } ?> <form name="myform" method="post"> <input type="hidden" name="myvar" /> <div onclick="myform.myvar.value='123';">Click this</div><br /> <div onclick="myform.myvar.value='456';">or this</div><br /> <input type="submit"> </form> </body> </html> |
|||
|
Do register to be able to answer |
|||
©2012 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








