4
Contributors
8
Replies
12
Views
5 Years
Discussion Span
Last Post by Biiim
0

PHP runs on the server, so you will either need to submit a form, or do an AJAX call from Javascript. So if you explain some more what you want to achieve, we can advise a little better.

0

I just wan't to add one to $i each time a button is clicked and show it.

0

Make a HTML form with a submit button. Store your variable in a session variable. When the form submits, add 1 to it.

0

Like Pritaeas says php is server side, you want a client side script to do that eg javascript:

<script type='text/javascript'>
var i = 0;//heres your $i
function incNum(eid,amt){
    i = i + amt;
    displayNum(eid);
    //alert(i);
}
function displayNum(eid){
    var obj = document.getElementById(eid);
    obj.innerHTML = i;//innerHTML for div
    obj.value = i;//value for an input
    //alert(i);
}
setInterval(function(){incNum('inputexample',1);},1000);
</script>
<a href='javascript:' onclick="incNum('numDiv',1);">Add one</a>
<a href='javascript:' onclick="incNum('numDiv',5);">Add five</a>
<div id='numDiv'></div>
<input id='inputexample'/>
<script type='text/javascript'>
displayNum('numDiv');
displayNum('inputexample');
</script>

Just save it as a php or html page.

or could keep it really simple:

<script type='text/javascript'>
function clicked(){
    i++;
    var obj = document.getElementById('numDiv').innerHTML = i;
}
</script>
<a href='javascript:' onclick="clicked();">Add one</a>
<div id='numDiv'></div>

Edited by Biiim

0

But later with php i will save the number so i can't do it with JS.

1

But I can do it with js:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript'>
var i = 0;//heres your $i
function incNum(eid,amt){
    i = i + amt;
    displayNum(eid);
    //alert(i);
}
function displayNum(eid){
    var obj = document.getElementById(eid);
    obj.innerHTML = i;//innerHTML for div
    obj.value = i;//value for an input
    //alert(i);
}
function saveNumInPhp(){
    $.post("savedata.php",{i:i,othervar:'1'},
        function(data){
            $('#saveMsg').html(data);
        }
    );
}
setInterval(function(){incNum('inputexample',1);},1000);
</script>
<a href='javascript:' onclick="incNum('numDiv',1);">Add one</a>
<a href='javascript:' onclick="incNum('numDiv',5);">Add five</a>
<a href='javascript:' onclick="saveNumInPhp();">Save</a>
<div id='numDiv'></div>
<input id='inputexample'/>
<div id='saveMsg'></div>
<script type='text/javascript'>
displayNum('numDiv');
displayNum('inputexample');
</script>

savedata.php

<?php
$i = $_POST['i'];

//do something with $i
$msg = 'Success';
echo $msg.'-> '.$i;
?>
0

if you have to use forms then follow Pritaeas then:

Make a HTML form with a submit button. Store your variable in a session variable. When the form submits, add 1 to it.

You could also store the increment as a hidden value:

<input type='hidden' name='i' value='<?php echo $_POST['i']++;?>'/>

also note ajax isn't actually any different from pushing the submit button in a form when you break it down to the tech side of it.

Ajax just sends a post request to a page and returns the result in a javascript variable
A form submit sends a post to a page and outputs it in the browser

It makes much better user interfaces without the need for page refresh - but i can understand big systems than have lots of already written code could make it awkward to implement

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.