Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a html script with a javascript variable inside it, this variable I have then passed to a php file, to increment it by 1. My problem is that I don't know how I would then pass it back to the html file, to then display it, also I don't know how the variables work on a website, if I have to save the variable in some sort of way to make sure it is the same updated variable as when I left the site.

html script:

<script type="text/javascript">

                var nrVar= 1;
                document.getElementById("nrVar").innerHTML = nrVar;
                document.getElementByID("nrVar").value = nrVar;

</script>

php script:

    <?php 

    $nrVar= $_POST['nrVar'];

    $nrVar= round((int)$nrVar+1);

?>
share|improve this question
    
I'm not sure what you are trying to do exactly, but to have your variables persist you need for example sessions or a database on the server side or something like local storage on the client side. –  jeroen Mar 27 at 15:18
    
I think you're looking for AJAX –  Daan Mar 27 at 15:20

2 Answers 2

You have to save your javascript number in a form field, send the form to a PHP script, there the number is increased, then send it back in session. Next is the code :

increase1.php

<?php
// GET THE NUMBER FROM PHP. THE FIRST TIME IT'S JUST 1. NEXT TIMES
// THE NUMBER COMES IN SESSION INCREASED FROM PHP SCRIPT.
session_start();
if ( IsSet( $_SESSION["increased"] ) )
     $increased = (int)$_SESSION["increased"];
else $increased = 1;
?>
<html>
  <head>
    <title>Jose Manuel Abarca Rodriguez</title>
    <script type="text/javascript">

// ASSIGN VALUE TO JAVASCRIPT VARIABLE.
var my_number = <?php echo $increased; ?>;

// SEND THE FORM TO INCREASE2.PHP.
function clik () {
document.getElementById("the_number").value = my_number;
document.getElementById("frm").submit();
}
    </script>
  </head>
  <body>
    My number is = <script type="text/javascript">document.write( my_number );</script>
    <br/>
    <button onclick="clik()">Increase my number</button>

<!-- FORM TO SEND NUMBER TO PHP. -->
    <form method="post" action="increase2.php" 
          id="frm" style="display:none">
      <input type="text" id="the_number" name="the_number"/>
    </form>

  </body>
</html>

increase2.php

<?php
session_start();
$num = (int)$_POST["the_number"];
$_SESSION["increased"] = ++$num; // !!! NEVER $num++ !!!
header("Location: increase1.php" ); // BACK TO FIRST PAGE.
?>

Create two textfiles with the given names (increase1.php and increase2.php), open your browser and run localhost/increase1.php.

share|improve this answer
    
Thank you very much for your answer, I already have a form where I am passing the variable to along with a couple of other variables as you can see in the scripts below, the script you have made look's like it works, I just find it a bit difficult to figure out how I would implement the functions in to my own scripts. html: pastie.org/private/4yrlpqfgpjc3ipiwluqq php: pastie.org/private/cz1bnriprqdhjyzr8jqcwa The variable is still called nrVar. –  Sm00rf9000 Mar 27 at 16:32
    
If you want, you may accept the answer. –  Jose Manuel Abarca Rodríguez Mar 27 at 16:51

Assuming that $_POST is sent by a normal submit and not a AJAX request. I would generate script along with rest of the site using PHP to define variable value:

<script>
var nrVar = parseInt('<?php echo $_POST['nrVar']; ?>');
</script>

parseInt here is to make sure we get integer value, $_POST sanitization would also be good idea to prevent xss or even javascript injection directly.

share|improve this answer
    
So I would use this code in the same javascript function, and then it will get it from the php script? –  Sm00rf9000 Mar 27 at 15:46
    
that code will present syntax errors. watch your quotes –  Félix Gagnon-Grenier Mar 27 at 16:01

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.