简体   繁体   中英

php javascript echo javascript variable to php loop condition dynamically using onclick or how can I use ajax to solve it?

How can I store only the value of f() into $ab ? How can I also change $ab dynamically in the loop condition when the button is pressed so that the integers are printed out dynamically?

<script type="text/javascript">
var i = 0;
function f(){
i = i + 10;
document.getElementById("more").innerHTML= i;
}
</script>

php

$ab = "<p id='more'> </p>";
echo $ab;
for($x=0;$x<$ab;$x++) echo $x . "<br>";
echo "<button type='button' onclick='f()'> > </button>";

Try this,

Hope this work for you.

    <script type="text/javascript">
    var i = 10;
    function f(){
        i = i + 10;
        var html = "";
        for(var j=0;j<i;j++){
            html += j+"<br/>";
        }
        document.getElementById("more").innerHTML= html;
    }
    </script>

You need to send the variable $x to your function f

echo "<button type='button' onclick='f($x)'> > </button>";

Then in the function you can count how many times the buttons all together are clicked, i++ :

var i = 0;
function f(x) {
  x += i * 10;
  i++;
  document.getElementById("more").innerHTML= i;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM