2013-05-06 85 views
0

我有一個php頁面,其中包含一個假設將MySql數據導出爲ex​​cel的按鈕。在導出頁面上的用戶之前,選擇「收件人」和「發件人」日期。我已經放置了一個check()javascript函數來驗證用戶何時選擇了這兩個日期。如何在html標記中調用javascript和php函數?

問題說明是,在按鈕上單擊下載開始時不檢查日期。請幫忙。這是代碼。

<? 
include("adminlogin.php"); 

include("classes/tc_calendar.php"); 

$myCalendar = new tc_calendar("stdt", true, false); 
$myCalendar->setIcon("images/iconCalendar.gif"); 
$myCalendar->setPath("./"); 
$myCalendar->setYearInterval(2000, 2015); 
$myCalendar->dateAllow('2000-01-01', '2015-12-31'); 
$myCalendar->setDateFormat('j F Y'); 
//$myCalendar->setHeight(350);  
//$myCalendar->autoSubmit(true, "form1"); 

$myCalendar2 = new tc_calendar("enddt", true, false); 
$myCalendar2->setIcon("images/iconCalendar.gif"); 
$myCalendar2->setPath("./"); 
$myCalendar2->setYearInterval(2000, 2015); 
$myCalendar2->dateAllow('2000-01-01', '2015-12-31'); 
$myCalendar2->setDateFormat('j F Y'); 
//$myCalendar2->setHeight(350); 
//$myCalendar2->autoSubmit(true, "form1"); 

$stdt1=$_POST['stdt']; 
$enddt1=$_POST['enddt']; 
echo "dates are"; 
echo $stdt1; 
echo $enddt1; 

// function to download report 
function export_excel_csv() 
{ 
    $init_sql = "select distinct(proj_id) from timeshiftentry_master where dt>='$stdt1' and dt<='$enddt1'"; 
    $init_res=mysql_query($init_sql); 

    //header info for browser 
    $filename = "WP_List_" . date('Ymd') . ".xls"; 
    header("Content-Type: application/vnd.ms-excel; name='excel'"); 
    header("Content-Disposition: attachment; filename=\"$filename\""); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 

    /*******Start of Formatting for Excel*******/ 
    //define separator (defines columns in excel & tabs in word)  
    $sep = "\t"; //tabbed character 
    ob_end_clean(); 

    //start of printing column names as names of MySQL fields 
    /*for ($i = 0; $i < mysql_num_fields($result); $i++) 
    { 
     if($i == 0) 
     { 
      echo "S. No." . "\t"; 
     } 
     echo mysql_field_name($result,$i) . "\t"; 
    } 
    echo "Employee Name" . "\t"; 
    echo "Hours Booked" . "\t"; 
    print("\n");*/ 

    $SerialNum = 0; 
    while($init_row=mysql_fetch_array($init_res)) 
    { 
     $sql = "SELECT * FROM project_master where proj_id='$init_row[0]'"; 
     //execute query 
     $result = mysql_query($sql) or die (mysql_error()); 
     //end of printing column names 
     //start while loop to get data 
     while($row = mysql_fetch_array($result)) 
     { 
      // get the emplyoee details 
      $projid=$row[0]; 
      $projnm=$row[1]; 
      $sql_emp="Select distinct(emp_no),emp_name from timeshiftentry_master where proj_id='$projnm' and dt>='$stdt1' and dt<='$enddt1'"; 
      $res_emp=mysql_query($sql_emp); 
      while($row_emp=mysql_fetch_array($res_emp)) 
      { 
       $sql_hrs="select sum(wrkhrs) from timeshiftentry_master where proj_name='$projnm' and emp_no='$row_emp[0]' and dt>='$stdt1' and dt<='$enddt1'"; 
       $res_hrs=mysql_query($sql_hrs); 
       $row_hrs=mysql_fetch_array($res_hrs); 
       if($row_hrs[0] >= 0) 
       { 
        $SerialNum++; 
        // insert to excel 
        $schema_insert = ""; 
        for($j=0; $j<mysql_num_fields($result);$j++) 
        { 
         if($j==0) 
         { 
          $schema_insert .= "$SerialNum".$sep; 
         } 
         if(!isset($row[$j])) 
          $schema_insert .= "NULL".$sep; 
         elseif ($row[$j] != "") 
          $schema_insert .= "$row[$j]".$sep; 
         else 
          $schema_insert .= "".$sep; 
        } 
        $schema_insert .= "$row_emp[1]".$sep; 
        $schema_insert .= "$row_hrs[0]".$sep; 
        $schema_insert = str_replace($sep."$", "", $schema_insert); 
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
        $schema_insert .= "\t"; 

        print(trim($schema_insert)); 
        print "\n"; 
        // inserted one row 
       } 
      } 
     } 
    } 
    exit(); 
} 

/*if(!isset($_POST)) 
{ 
    export_excel_csv(); 
}*/ 

if($_SERVER['REQUEST_METHOD']=="POST") 
{ 
    $chk1= $_POST['download']; 

    if($chk1=='Download') 
    { 
     export_excel_csv(); 
    } 
} 
else 
{ 

} 

?> 

<script language="javascript"> 
<!-- 

function check() 
{ 
    if(show) 
    { 
     if(document.f1.stdt.value=="0000-00-00") 
     { 
      alert("Please Enter Start Date") 
      show=false; 
      return false; 
     } 
     if(document.f1.enddt.value=="0000-00-00") 
     { 
      alert("Please Enter the End Date") 
      show=false; 
      return false; 
     } 
     if(document.f1.stdt.value>document.f3.enddt.value) 
     { 
      alert("Dates not allowed.") 
      show=false; 
      return false; 
     } 
    } 
    return true; 
} 

//--> 
</script> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 

<title>Bombardier TSM System</title> 

<style type="text/css"> 
<!-- 
.style1 {font-size: small} 
.style2 {color: #FFFFFF} 
.style3 {font-size: x-small} 
--> 
</style> 

</head> 

<body> 

<form name="f1" method="POST" action="" onClick="return check()"> 

<script language="javascript" src="calendar.js"></script> 

<br> 

<table align="center" cellspacing="5"> 

<tr> 
<th colspan="2"><font color="#993300" size="4">Download Hours Booking Report</font></th> 
</tr> 

<tr> 
<td>&nbsp;</td> 
</tr> 

<tr> 
<td align="right"><font color="#993300">From : </font></td> 
<td><?$myCalendar->writeScript();?></td> 
</tr> 

<tr> 
<td align="right"><font color="#993300">To : </font></td> 
<td><?$myCalendar2->writeScript();?></td> 
</tr> 

<tr> 
<td>&nbsp;</td> 
</tr> 

<tr> 
<td colspan="2" align="center"><input type="submit" name="download" value="Download" id="download" onclick="download=true"/></td 
</tr> 

</table> 

<p>&nbsp;</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 

<?include("footer.php");?> 

</form> 
</body> 
</html> 
+1

您正在使用[**的**過時的數據庫API(http://stackoverflow.com/q/12859942/19068),並應使用[現代更換](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin 2013-05-06 12:08:22

回答

0

有兩個問題

1)您有多餘的HTML註釋標記。

2)你正在檢查 '如果' 對未聲明的變量顯示

這裏

if(show) 

因此JavaScript是失敗的條件。這就像未定義的變量顯示。

+0

我做了所述更改。仍然存在的問題是,在提交(即「下載」)時會彈出警報[請輸入日期...],並在「確定」下彈出下載頁面。還有其他需要修改的地方 – 2013-05-06 12:12:34

+0

你有任何郵件ID或者我可以粘貼推薦代碼的東西嗎? – 2013-05-06 12:17:24

+0

謝謝Chandresh,我得到了解決方案,但如果可以提供幫助,我還有其他問題。沒有閱讀頁面上設置的日期。如果你看看下面的代碼片段。評論代碼不會讀取日期,但會生成excel文件(顯然是空白),而未註釋的代碼能夠讀取日期,但excel不會生成。任何幫助? – 2013-05-06 12:26:55

0

您想要執行檢查時,表單是提交,沒有點擊。

<form name="f1" method="POST" action="" onClick="return check()"> 

應該

<form id="f1" method="POST" action="" onsubmit="return check()"> 
+0

我一直在格式化文件很長一段時間,我只是忘記了我做了這個改變。感謝您注意到這一點。 – 2013-05-06 12:20:36