How can I query Database on click of Button

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 64
Joined: Thu Mar 14, 2013 6:10 am

Tue May 07, 2013 6:29 am

I am trying to pass date as parameter to PHP code basically I want to pass every time -7 date

Suppose on 1st click date is 2013-02-10

on 2nd click it will be -7

means 2013-02-3

but problem what is happening is as this is all happening on click of Button, and once the users click on Button the form is posted and again page is REFRESHED and again & again COUNT becomes 0

how can I avoid this is there any global or Static variable concept in Javascript or PHP

Basically I want to Query MySQL when user clicks Previous Previous, as I want to get Weekly Data from Database thats why I am passing Date as Hidden Variable to Query Week by Week Data


$day2 = date('Y-m-d');

$day2 = date("Y-m-d",strtotime($day."last Sunday"));



<script type="text/javascript">
var dateis = '<?php= $day2;?>';
var count =0;
function add()

count ++;

if(count >0)
var prev = new Date();
var x=7*count;

var newdate = new Date(prev);
newdate.setDate(newdate.getDate()- x);

document.getElementById("datesent").value = newdate;



<form name = "getdata" method ="post" id = "getdata" action= "getprevdata.php">

<input type = "hidden" id "datesent" name "datesent">

<button onclick="add()"> Previous </button>


User avatar
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Tue May 07, 2013 9:51 pm


In PHP you can can do like:
create a hidden input tag to store the count value and pass with submitted data so that when page is refreshed then you can get the count value from submitted value of count.

<input id='count' name='count' type='hidden' value='<?php $count; ?>' />

then in php code you can get the count value as
$count = $_POST['count'];
and then do the operation.


you can query that database each time after submitting the form and refreshing the page to get the date stored in database and based on that value you can do the operation then.

IN JavaScript you can do like:

count = 0;
function add() {
//your code

so here count is considered as global variable.

Sandeep Agrawal
Mindfire Solutions

Post Reply