<html>
<body>
<?php
$i=1;
echo"$i";
if(up)
++$i;
?>
</body>
How can i add one to $i when a button is clicked?
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.
I just wan't to add one to $i each time a button is clicked and show it.
Make a HTML form with a submit button. Store your variable in a session variable. When the form submits, add 1 to it.
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>
But later with php i will save the number so i can't do it with JS.
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;
?>
forms are the best option to do this...
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.