借助表单借助ajax和jquery从数据库中过滤日期?

时间:2018-08-12 11:29:42

标签: javascript php html ajax

我在php中有四个文件,第一个是包含数据表的反馈,我尝试在其中显示数据反馈。php在下面

<?php
error_reporting(E_ALL);
ini_set('display_error',1);
include_once('header.php');
//include_once('sidemenu.php');


require_once('Class_Library/class_Feedback.php');
?>

<?php

$obj = new Feedback();
//$clientid = $_SESSION['clientId'];
$feedbackdata = $obj->GetAllFeedback();
if($feedbackdata['success']==1)
{   
    $data=$feedbackdata['data'];
   //print_r($data);               
}
 else {
    $data=[];
}


//print_r($fedbackdata);
//die;
?>
<style>
   .dataTables_filter {
    float: right;
    }
    .pagination{
    float: right;
    }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript' src="js/home_page.js"></script>
<script>
    $(document).ready(function () {
        var startdate = document.getElementById("feedbackstartdate").value;
        var enddate = document.getElementById("feedbackenddate").value;
                var site = document.getElementById("site").value;


       // alert("end "+enddate);
       // alert("start "+startdate);
    $feedbackdata=showfeedbacklist(startdate, enddate,site);
    });
</script>
<script>
function feedbackindex()
    {
        //alert("hello, your code is here");
        var startdate= document.getElementById("fromDate").value;
        var enddate= document.getElementById("toDate").value;
                var site= document.getElementById("site").value;

        //alert('start '+startdate);
        //alert('enddate '+enddate);
        if (startdate == "")
        {
        window.alert("Please select From date.");
        document.getElementById("fromDate").focus();
        return false;
        }
        if (enddate == "")
        {
            window.alert("Please select To date.");
            document.getElementById("toDate").focus();
            return false;
        }

        if (startdate > enddate )
            {
                window.alert("From date can't be greater then To date.");
                document.getElementById("toDate").focus();
                return false;   
            }

        $feedbackdata = showfeedbacklist(startdate, enddate,site);
                /**************************************************************************/

    }
</script>

<!--page content -->
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default" style="margin-top: 25px;">
                    <div class="panel-heading">
                        <h4>Feedback</h4>  
                    </div>
                    <form id="formdata" method="post">
                        <div class="col-md-4">
            <div class="form-group">
            <label for="pwd">From:&nbsp;&nbsp;</label>
            <input type="date" id="fromDate" name="FSDate" size="20" class="form-control input" placeholder="mm/dd/yyyy"/>
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="pwd">To:&nbsp;&nbsp;</label>
            <input type="date" id="toDate" class="form-control" name="FSDate" size="20" placeholder="mm/dd/yyyy"/>
            </div>
                </div>
                        <div class="col-md-4" style="margin-top:20px;">
                       <div class="form-group">
                        <input type="hidden" name="feedbackstartdate" id="feedbackstartdate" value="<?php echo date('Y-m-d', strtotime("-7 days")); ?>">
                        <input type="hidden" id="feedbackenddate" name="feedbackenddate" value="<?php echo date('Y-m-d', strtotime("-1 days")); ?>">
                        <input type="" id="site" name="site" value="<?php echo SITE; ?>"

                        <center><button type="button" class="btn btn-info" onclick="return feedbackindex();">Submit</button></center>
                        </div>
                       </div>
                    </form>

              <div class="panel-body">
                    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">                           <thead>
                                <tr>
                                    <th>Cutomer Name</th>
                                    <th>Location</th>
                                    <th>Visit Type</th>
                                    <th>Visit By</th>
                                    <th>Visit Dtae</th>
                                    <th>Feedback</th>                                    
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                    <?php 
                        for ($i = 0; $i < count($data); $i++) {
                    ?>
                      <tr>
                                    <td><?php echo $data[$i]['CustomerName'];?></td> 
                                    <td><?php echo $data[$i]['clientCity']; ?></td>
                                    <td><?php echo $data[$i]['TaskType']; ?></td>
                                    <td><?php echo $data[$i]['visitByName']; ?></td>
                                    <td><?php echo $data[$i]['visitDate'];?></td>
                                    <td><?php 
                                   $datafeedback = $data[$i]['feedbackValue'];
                                    if($datafeedback==null)
                                    {
                                        echo 'pending';
                                    }
                                    else{

                                        if($datafeedback==5)
                                        {
                                      echo '<img src="images/feedback/greenSmile.png" alt="send feedback by data"style="width:50px;height:50px;" />';

                                    }
                                    elseif ($datafeedback==4) {
                                        echo '<img src="images/feedback/smile.png" alt="send feedback by data"style="width:50px;height:50px;" />';
                                    }
                                    elseif ($datafeedback==3) {
                                        echo '<img src="images/feedback/redsmile.png" alt="send feedback by data"style="width:50px;height:50px;" />';

                                }

                                }
                                    ?></td> 
                                    <td>  
                                        <a  href="#" class="fa fa-eye"></a>
                                    </td>   

                                </tr>
                    <?php
                        }
                        ?>
                            </tbody>
                        </table>
                    </div>   
                </div>
            </div>
        </div>
    </div>
<!--/page content -->
<?php include 'footer.php'; ?>

当我们尝试显示数据时,我的代码是正确的工作,但是当我们尝试过滤时,我不知道如何工作,并且我的js链接文件是

function showfeedbacklist(startdate, enddate, site)
{

    var postData =
            {
                "startdate":startdate ,
                "enddate": enddate,
                "site" :site
            };


    var dataString = JSON.stringify(postData);
   // alert(dataString);

    $.ajax({
        type: "post",
       // dataType: "json",
        //contentType: "application/json; charset=utf-8",        
        url:site+"Link_Library/link_getfeedbacklist.php",
        data: {"mydata": dataString},
        success: function (response) {
            var respdata = response;
           // alert(respdata);
            console.log(respdata);   
        },
        error: function (e) {
            alert(e);
            console.log(e.message);
        }
    });
}

和我发送数据的ajax页面是

<?php
require_once('../Class_Library/class_Feedback.php');
$obj = new Feedback();

if (!empty($_POST["mydata"])) {
$jsonArr = $_POST["mydata"];
//  echo $jsonArr;
$data = json_decode($jsonArr, true);
 //print_r($data);

 //die;
if (!empty($data)) {

$fromdt1 = $data['startdate'];
$fromdt = date("Y-m-d H:i:s", strtotime($fromdt1));
//    echo "statrt date-".$fromdt;

$enddte1 = $data['enddate'];
$enddte2 = date("Y-m-d", strtotime($enddte1));
$date123 = date_create($enddte2 ." 23:59:59", timezone_open("Asia/Kolkata"));
$enddate = date_format($date123, "Y-m-d H:i:s");
//      echo "endd date-".$enddt;

$result = $obj->graphGetFeedbacklist($enddate, $fromdt);




//         echo "<pre>";
//         print_r($res);
}
}
?>

我的sql类文件是最后一个

<?php

if (!class_exists('Connection_Communication')) {
    include_once('class_connect_db_Communication.php');
}

class Feedback {

    public $DB;

    public function __construct() {
        $db = new Connection_Communication();
        $this->DB = $db->getConnection_Communication();
    }
function GetAllFeedback() {
        try {
$query = "select site.*,list.Service_Type,list.Id,(select Task_Type from tbl_task_type where Auto_Id = list.Service_Type) as TaskType,DATE_FORMAT(start_date,'%d-%b-%Y') as visitDate,CONCAT(usr.First_Name,' ',usr.Last_Name) as visitByName,(select SUM(parameter_value)/(select count(parameterId) from tbl_visit_feedback_parameter where parameterId < (select MAX(parameterId) from tbl_visit_feedback_parameter)) from tbl_visit_feedback_data where parameter_id < (select MAX(parameterId) from tbl_visit_feedback_parameter) and task_id = site.task_id) as feedbackValue from tbl_site_visit_detail as site JOIN tbl_master_user as usr ON site.employee_id = usr.ID JOIN tbl_tasks_list as list ON site.task_id = list.Task_Id where visit_status = '1'";
            $stmt = $this->DB->prepare($query);
//            $stmt->bindParam(':sd', $startdate, PDO::PARAM_STR);
//            $stmt->bindParam(':ed', $enddate, PDO::PARAM_STR);


            $stmt->execute();
            $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if ($row) {
                require_once 'class_master.php';
                $masObj = new Master();
                $farray = array();
                for ($i = 0; $i < count($row); $i++) {
                    $refid = $row[$i]['Id'];
                    $Service_Type = $row[$i]['Service_Type'];
                    $result = $masObj->getTaskDetail($refid, $Service_Type);
                    if ($result['success'] == 1) {
                        $data = $result['data'];
                        $temp = $data + $row[$i];
                        array_push($farray, $temp);
                    }
                }
                if (!empty($farray)) {
                    $response['success'] = 1;
                    $response['data'] = $farray;
                } else {
                    $response['success'] = 0;
                    $response['message'] = 'No data found.';
                }
            } else {
                $response['success'] = 0;
                $response['data'] = 'error while fetching data ';
            }
        } catch (Exception $ex) {
            $response['success'] = 0;
            $response['data'] = $ex->getMessage();
        }
        return $response;
    }



}

?>

我是编码领域的新手,所以我不知道该如何处理类型问题,因此播下了很多参考文献,但我不知道在那里会发生什么。

0 个答案:

没有答案