Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a html form, a variable and a .php script. I would like to pass the value of this variable to the .php script using a html form, it has to be the variables value, so lets say it is 1 then it passes the value 1, but then if the value of the variable changes to 2 then the value the form passes should be 2. Currently when passing the variable, it just passes the name of it.

html:

<form id="get-form" action="getAmount.php" method="POST" name="get-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="testVar" name="testVar" value="testVar"/>
<input type="image"  src="butImg.png" id="Button" value="pay" alt="but"/>
</form>

<script type="text/javascript">
var testVar= 1;
document.getElementById("testVar").value = testVar;
</script>

php:

$testVar= $_POST['testVar'];
share|improve this question
    
Where/when does the variable change? – chris85 Apr 2 '15 at 19:36
    
where is the problem? you don't get that 1 in post after submitting this form? – n-dru Apr 2 '15 at 19:39
    
Have you tried the input like <input type="hidden" id="testVar" name="testVar"/> – mzografski Apr 2 '15 at 19:40
    
It is because the .php script passes the amount of the current variable to a .txt script and then next time the variable it is submited, it is the current+1 so that everytime the .php is called, the variable is raised by one. It is a bit complicated but it is to make sure every user on the website is able to see the updated variable at once. – Fulgut98 Apr 2 '15 at 21:28
    
@n-dru yes the issue is that the 1 isnt getting passed, so whenever I check for the 1 it has only passed the name of the variable. – Fulgut98 Apr 2 '15 at 21:29

I think the problem is that javascript in your browser is turned off, so the javascript code will not be executed and the value of testVar will stay the same.

You can easily check that by opening the web inspector of your browser and check if the value of the hidden field is your given value or testVar.

share|improve this answer
    
I have checked if javascript is on in my browser and it is, but thank you for the answer. – Fulgut98 Apr 2 '15 at 21:35

Instead of trying passing the Javascript variable do pass the PHP one

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

If you need the JavaScript variable for something else you can still have the JS assignment part:

<script type="text/javascript">
  var testVar= <?php echo $_POST["testVar"];?>;
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.