0

I have a input text field. I need to pass the values entered inside the element through onClick of javascript.

<form>
<fieldset>     
  <label>Common Site ID: </label><span><?php echo $commonsiteid ?></span><br>
  <label>Acres: </label><input id="acre_value" name="acre_value" type="text" value="<?php echo $acre; ?>">
 </fieldset>
</form>
<input type="submit" value="submit" onclick="saveValue('<?php echo $_REQUEST['acre_value'] ?>')">

I am passing the value through submit onClick, Which is going empty. How do i pass value in this onclick function.

1

3 Answers 3

2

Try this:

<input type="submit" value="submit" onclick="saveValue(document.getElementById('acre_value').value);">
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried writing something like following.

<input type="submit" value="submit" onclick="saveValue(document.getElementById('acre_value').value)">

Comments

1

I try this, and worked:

<script>
    function saveValue(){
        alert(document.formName.hiddenField.value);
        /*do something*/
        return false;
    }
</script>
<form name="formName" method="post">
    <input type="hidden" name="hiddenField" value="<?php echo $_REQUEST['acre_value'] ?>"/>
    <input type="submit" value="submit" onclick="saveValue()">
</form>

As you can see, i pass the value by a hidden field, and on the Js function i get the value of this field.
If you need a php function instead off a js, it's the same logic.

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.