0

I am incrementing a value of a hidden element using javascript , and posting it in the $_POST array. But it increases only once , and then remains the same. Please help.

My file is incrementing.php with the below code:

<script language="Javascript">

function NextClicked()
{  
 document.getElementById("LabelClicked").value = 
  document.getElementById("LabelClicked").value + 1 ; 

 document.forms["incrementing"].submit();

}
</script>


<?php

if(isset($_POST['LabelClicked']) && $_POST['LabelClicked']>=1)
{
  $_POST['LabelClicked'] = $_POST['LabelClicked'] +9; 

}

?>
<?php
    if(isset($_POST['clickednext']))
    {
        echo 'Value ='.$_POST['LabelClicked'];
    }
    else
    {
        echo "Not Clicked Yet";
    }   
?>
<form name = "incrementing" method="post" action="incrementing.php">
 <div class=d2 align=left><a href="#" onclick=" NextClicked(); submit();">Next</a>
<input type = "hidden"  id="LabelClicked" name="LabelClicked" />
</form>

1 Answer 1

2

Don't you need to set the value of LabelClicked id to be what the new, incremented value is?

<input type="hidden"  id="LabelClicked" name="LabelClicked" value="<?php echo $_POST['LabelClicked']; ?>" />

Otherwise it is incrementing from "" everytime. You should also make sure that you are treating

document.getElementById("LabelClicked").value

as an integer and not a string, or else you will get "111111"

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

1 Comment

sir, please help me in the code, am confused totally, how to set the new value and assign it to the LabelClicked attribute value

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.