visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
316 experts, 1194 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Posting vars via javascript


By: kaller2 U.S.A.  Date: 11/04/2003 00:00:00  English  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 English  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 English  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 English  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 English  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 English  Type : Comment
hehe, how do I give myself points?
By: VGR Date: 12/04/2003 21:46:00 English  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 English  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 English  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

EContact
browser fav
page generated in 396.366120 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page