Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How can I query Database on click of Button

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How can I query Database on click of Button

Postby stephenrodrics » 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


[PHP]PHP
<?php

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

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

?>


JAVASCRIPT

<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;
document.getElementById("getdata").submit();
}

}

</script>


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

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

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

[/PHP]
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 53
Joined: Thu Mar 14, 2013 6:10 am

Re: How can I query Database on click of Button

Postby sandeep605085 » Tue May 07, 2013 9:51 pm

Hi,

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.

OR

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
count++;
}

so here count is considered as global variable.

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


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.