简体   繁体   中英

Display new query results on button click php

I have a page that displays a query result on load. there is also a date range selector, which when selected has to run the same query and display the updated results. The page loads perfectly, but when i select the date range and click the button, nothing happens. Here is the code and the image of the page. I am a newbie in php and html, learning by building a web application. So please excuse if my question is very basic and dumb. Screenshot of Page

 <html> <body> <div class="container"> <h1>Welcome <?php echo $login_session; ?></h1><br></br> <div class="row"> <div class="panel panel-black"> <div class="panel-heading form-inline"> <div class="row"> <div class="col-md-8"> <label class="control-label" for="fromdate">From:</label> <input type="date" class="form-control" id="fromdate" name="fromdate"> <label class="control-label" for="todate">To:</label> <input type="date" class="form-control" id="todate" name="todate">&nbsp;&nbsp;&nbsp;&nbsp; <button type="submit" class="btn btn-success" name="todate" id="todate" style="width: 10%;" value="Date Run" >Go</button> </div> </div> </div> </div> </div> <div class="row"> <div class="col-lg-3 col-md-6"> <div class="panel panel-blue"> <div class="panel-heading"> <div class="row"> <div class="col-xs-9 text-justify"> <div class="huge"><strong><?php echo $main_total_pledges; ?></strong></div> <div>Total Pledges</div> </div> .... </div> </div> </div> </div> </div> </body> </html> 

  <?php
   include('session.php');

    if(isset($_POST["todate"])=="Date Run")
            { 
                $main_query = mysqli_query($db,"SELECT count(a.pledge_id) total_pledges, sum(case when processed_status = 'Approved' then 1 else 0 end) approved, sum(case when processed_status = 'Cancelled' then 1 else 0 end) Cancelled, sum(case when processed_status in ('Alien','Single Gift') then 1 else 0 end) Gift, sum(case when processed_status is null then 1 else 0 end) Pending from waysact_source a left join vlc_processed_item b on a.pledge_id = b.pledge_id where a.fundraiser in (select distinct concat(f_firstname,' ',f_lastname) fundraiser from fundraiser where f_company in (select contractor_name from contractor where company_name = '$_SESSION[login_user]')) and a.pledge_date = '2017-06-06'");
                $main_query_result = mysqli_fetch_row($main_query);
                $main_total_pledges = $main_query_result[0];
                ...
            } else
            {
                $main_query = mysqli_query($db,"SELECT count(a.pledge_id) total_pledges, sum(case when processed_status = 'Approved' then 1 else 0 end) approved, sum(case when processed_status = 'Cancelled' then 1 else 0 end) Cancelled, sum(case when processed_status in ('Alien','Single Gift') then 1 else 0 end) Gift, sum(case when processed_status is null then 1 else 0 end) Pending from waysact_source a left join vlc_processed_item b on a.pledge_id = b.pledge_id where a.fundraiser in (select distinct concat(f_firstname,' ',f_lastname) fundraiser from fundraiser where f_company in (select contractor_name from contractor where company_name = '$_SESSION[login_user]'))");
                $main_query_result = mysqli_fetch_row($main_query);
                $main_total_pledges = $main_query_result[0];
                ...
            }

?>

You need <form> tag to send data. Like

<form action="" method="">
    <div class="row">
        <div class="col-md-8">
            <label class="control-label" for="fromdate">From:</label>
            <input type="date" class="form-control" id="fromdate" name="fromdate">
            <label class="control-label" for="todate">To:</label>
            <input type="date" class="form-control" id="todate" name="todate">&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="submit" class="btn btn-success" name="todate" id="todate" style="width: 10%;" value="Date Run" >Go</button>
        </div>
    </div>
</form>

First set Your id unique.

<button type = "submit" class = "btn btn-success" name = "btnTodate" id = "btnTodate" style = "width: 10%;" value = "Date Run" >Go</button>

And Used Ajax like that

    $(document).on('click', 'btnTodate', function(){
var dataValue = {setQuery: 1, data: $("#form_ID").serialize()}
$.ajax({
url: 'YOUR_PHP_FILE',
 dataType: "json",
 data: dataValue, success: function (r) {
}})});

And You can get all kind of data that you set in your form display here

YOUR_PHP_FILE.php

if (isset($_REQUEST['setQuery'])) {
$dataAuto = array();
parse_str($_POST['data'], $dataAuto);
if (!empty($dataAuto["todate"])) {
    $main_query = mysqli_query($db, "SELECT count(a.pledge_id) total_pledges, sum(case when processed_status = 'Approved' then 1 else 0 end) approved, sum(case when processed_status = 'Cancelled' then 1 else 0 end) Cancelled, sum(case when processed_status in ('Alien','Single Gift') then 1 else 0 end) Gift, sum(case when processed_status is null then 1 else 0 end) Pending from waysact_source a left join vlc_processed_item b on a.pledge_id = b.pledge_id where a.fundraiser in (select distinct concat(f_firstname,' ',f_lastname) fundraiser from fundraiser where f_company in (select contractor_name from contractor where company_name = '{$_SESSION['login_user']}')) and a.pledge_date = '2017-06-06'");
    $main_query_result = mysqli_fetch_row($main_query);
    $main_total_pledges = $main_query_result[0];
} else {
    $main_query = mysqli_query($db, "SELECT count(a.pledge_id) total_pledges, sum(case when processed_status = 'Approved' then 1 else 0 end) approved, sum(case when processed_status = 'Cancelled' then 1 else 0 end) Cancelled, sum(case when processed_status in ('Alien','Single Gift') then 1 else 0 end) Gift, sum(case when processed_status is null then 1 else 0 end) Pending from waysact_source a left join vlc_processed_item b on a.pledge_id = b.pledge_id where a.fundraiser in (select distinct concat(f_firstname,' ',f_lastname) fundraiser from fundraiser where f_company in (select contractor_name from contractor where company_name = '{$_SESSION['login_user']}'))");
    $main_query_result = mysqli_fetch_row($main_query);
    $main_total_pledges = $main_query_result[0];

}
die;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM