在按钮上单击php显示新的查询结果

时间:2017-07-11 05:56:10

标签: javascript php html

我有一个页面,在加载时显示查询结果。还有一个日期范围选择器,选择此选项后必须运行相同的查询并显示更新的结果。 页面加载完美,但是当我选择日期范围并单击按钮时,没有任何反应。这是页面的代码和图像。我是php和html的新手,通过构建Web应用程序来学习。如果我的问题非常基本和愚蠢,请原谅。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>
&#13;
&#13;
&#13;

  <?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];
                ...
            }

?>

2 个答案:

答案 0 :(得分:0)

您需要<form>标记才能发送数据。像

<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>

答案 1 :(得分:0)

首先设置您的ID唯一。

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

并使用类似的Ajax

    $(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) {
}})});

您可以在此处显示您在表单中设置的所有数据

<强> 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;}