I have multiple buttons on a single page. I need when I click a button that the same page reloads, but a counter and name of button clicked be updated. And as many times I do that, the list of clicked buttons and name of buttons should be maintained. Something like a visitor of site concept and counter.
e.g.
Button1 (value "Milk") Button2 (value "Cereals") etc...
Now pressing any of the buttons above will be like:
List: Item1: Button 1 - value of button item2: .... etc
I will have then a submit button that once am done with adding the items I need to list, I click and go to review what I selected...
Any idea how this can be done via php only without JS?
Thanks, and sorry if not so clear but I tried my best
You can create a form that submits to itself, in conjunction with sessions that retain the data for as long as you want it.
<?php
session_start();
if(isset($_POST)):
foreach ($_POST as $key => $value) {
$_SESSION[$key] = (isset($_SESSION[$key]) ? $_SESSION[$key]+= 1 : 1);
}
endif;
?>
<!doctype>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type='submit' name='milk' value='Milk'>
<input type='submit' name='cereals' value='Cereals'>
</form>
Milk: <?php echo $_SESSION['milk']; ?>
Cereals: <?php echo $_SESSION['cereals']; ?>
</body>
</html>
< ?php
if($_POST['mil'])
{
$_SESSION['count']++;
.... // milk button only
}
if($_POST['cre'])
{
$_SESSION['count']++;
.... // cereals button only
}
if($_POST['mil'] or $_POST['cre'])
{
$_SESSION['count']++;
.... // Any (milk or cereals) button
cked this block executed
}
echo $_SESSION['count']; // get total
?>
<form method='post' action=''>
<input type='submit' name='mil' value='Milk'>
<input type='submit' name='cre' value='Cereals'>
</form>
Which submit button clicked,that block only executed. The form submitted the same page.
using SESSIONS
< ?php
session_start();
if($_POST['mil'])
{
$count = ($_SESSION['count'] = $count++);
// $count would hold the incremented value for each time it is clicked
}
if($_POST['cre'])
{
$count1 = ($_SESSION['count1'] = $count1++);
}
if($_POST['mil'] or $_POST['cre'])
{
.... // Any (milk or cereals) button
cked this block executed
}
?>
<form method='post' action=''>
<input type='submit' name='mil' value='Milk'>
<input type='submit' name='cre' value='Cereals'>
</form>
This is the basic idea.