0

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

6 Answers 6

2
<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.

Sign up to request clarification or add additional context in comments.

Comments

1
<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">

Comments

0

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).

1 Comment

And by removing the name-attribute from the hidden input you might have made it impossible to find the value in enable_catch.php
0

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>

Comments

0

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.

Comments

0
<script>
document.your_form.answer.value = checkjava();
</script>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.