views:

49

answers:

7

Hi All,

First tentative steps into client side. I'm having trouble finishing the following. My question is how do I return the value of a function in a HTML statement ...

<script language="Javascript">
function checkjava()
{
 return 1
}
</script>

</head>
<body>
<form action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="submit" value="click me">
</form>
</body>

I'd appreciate your help Thanks in advance

G

A: 

Browser-side scripting is event driven. Unless an event is triggered, nothing happens.

You could listen to the click event on your submit button, and then manipulate the value of your hidden field:

<input type="hidden" id="answer" name="answer" value="")>
<input type="submit" value="click me" 
       onclick="document.getElementById('answer').value = checkjava();">

Note how we had to give an id to the hidden field, in order to be able to reference it with getElementById().

Also note that JavaScript is case sensitive: checkjava() and checkJava() are not the same.

Daniel Vassallo
A: 

You would do something like that:

<script language="Javascript">
  function checkjava()
  {
   return 1
  }
</script>

</head>
<body>
 <form action="enabled_catch.php" method="get" name="your_form" onsubmit="document.getElementById('answer').value=checkjava();">
  <input type="HIDDEN" id="answer" >
  <input type="submit" value="click me">
 </form>
</body>

Basically on form submit you would set the value of the answer input to the result of the function call.

EDIT: placed onsubmit on the wrong element before (embarrassing).

Strelok
And by removing the `name`-attribute from the `hidden` input you might have made it impossible to find the value in `enable_catch.php`
adamse
A: 

I would have an id on the answer tag and then do it in the form onsubmit event.

Something like:

<form onsubmit="document.getElementById("answerId").value = checkJava()" action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" id="answerId" value="RETURN checkjava() HERE")>
<input type="submit" value="click me">
</form>
Adam Butler
+1  A: 
<form action="enabled_catch.php" method="get" name="your_form" >
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="submit" onclick="document.getElementById('answer').value=checkjava();"  value="click me">
Arsheep
onclick on a hidden field?
Nick
damn apologizies , editing
Arsheep
A: 
<script>
document.your_form.answer.value = checkjava();
</script>

Unless you want to use a fancy JS library. =)

Laheab
+2  A: 
<head>
  <script language="Javascript">
    function checkjava() {
      return 1
    }
  </script>
</head>
<body>
  <form action="enabled_catch.php" 
        method="get" 
        name="your_form"
        onsubmit="this.answer.value=checkjava();">
    <input type="hidden" name="answer">
    <input type="submit" value="click me">
  </form>
</body>

No need for the id attribute.

adamse
A: 

Brilliant ! thank you all for your help

giles
Welcome at Stackoverflow! The normal way to express thanks is to post a *comment* on the answer in question and also to mark the most helpful answer accepted by ticking the checkmark on the left hand side. Do not post comments as answers! See also http://stackoverflow.com/faq
BalusC