问题描述
我已经使用FPDF在php中创建PDF.
我使用会话变量将一种形式之间的变量传递给另一种表单.当我在
中提供价值时Report.php <?php session_start(); $_SESSION['year1']=$_POST['course_year']; $_SESSION['sem1']=$_POST['semester']; $_SESSION['community1']=$_POST['community']; $_SESSION['course1']=$_POST['course']; $_SESSION['mess_type1']=$_POST['mess_type']; $_SESSION['block_name1']=$_POST['block_name']; ?> <form action='report.php' method='POST'name= 'form1'> <p><tr><td><b> Course Year:</b></td><td><input type='text' name='course_year'></td></tr><br></p> <p><tr><td> </td><td><input style='width:105;height:32' type='submit' value='Generate Report'onsubmit='yearpdf.php' onclick="year_open()" ></td></tr> </form> <h2 align='left'><b>Semester</h2></b> <form action='report.php' method='POST'name= 'form2'> <!--<form action='report.php' align='left' method='POST'>--> <p><b><tr><td>Semester:</b></td><td><input type='text' name='semester'></td></tr><br></p> <p><tr><td> </td><td><input style='width:105;height:32' type='submit' value='Generate Report' onsubmit='sempdf.php' onclick="sem_open()"></td></tr></table> </form> .... so on for community, course, messtype and blockname
wentypdf.php是我为创建PDF文件而写的文件.一旦显示出PDF生成的空白值,就会显示出表格.我必须刷新以获取PDF中显示的值.我的年份文件是:
<?php session_start(); require('fpdf/fpdf.php'); //Connect to your database $r1=$_SESSION['year1']; $con=mysql_connect('localhost','root',''); if(!$con) { die('Unable to connect'.mysql_error()); } mysql_select_db('hostel',$con); //Select the list you want to show in your PDF file $result=mysql_query("select hosteladmissionno,student_name,sex,community,semester,course,course_year,mess_type,block_name from registration where course_year='".$r1."' ORDER BY hosteladmissionno"); $number_of_products = mysql_numrows($result); //For each row, add the field to the corresponding column while($row = mysql_fetch_array($result)) { $hostad = $row['hosteladmissionno']; $name = $row['student_name']; $sex = $row['sex']; $sem=$row['semester']; $comm=$row['community']; $course=$row['course']; $courseyr=$row['course_year']; $mess= $row['mess_type']; $block=$row['block_name']; $column_no = $column_no.$hostad."\n"; $column_name = $column_name.$name."\n"; $sex_details = $sex_details.$sex."\n"; $sem_details= $sem_details.$sem."\n"; $comm_details= $comm_details.$comm."\n"; $course_details= $course_details.$course."\n"; $courseyr_details= $courseyr_details.$courseyr."\n"; $mess_details= $mess_details.$mess."\n"; $block_details= $block_details.$block."\n"; //$column_price = $column_price.$price_to_show."\n"; } mysql_close(); //Convert the Total Price to a number with (.) for thousands, and (,) for decimals. //$total = number_format($total,',','.','.'); //Create a new PDF file $pdf=new FPDF(); $pdf->AddPage(); //Fields Name position $Y_Fields_Name_position = 40; //Table position, under Fields Name $Y_Table_Position = 46; //First create each Field Name //Gray color filling each Field Name box $pdf->SetFillColor(232,232,232); //Bold Font for Field Name $pdf->SetFont('Arial','B',10); $pdf->SetY($Y_Fields_Name_position); $pdf->SetX(5); $pdf->Cell(30,6,'Admission No',1,0,'L',1); $pdf->SetX(35); $pdf->Cell(35,6,'Student Name',1,0,'L',1); $pdf->SetX(70); $pdf->Cell(20,6,'Sex',1,0,'L',1); $pdf->SetX(88); $pdf->Cell(20,6,'Category',1,0,'L',1); $pdf->SetX(108); $pdf->Cell(20,6,'Semester',1,0,'L',1); $pdf->SetX(128); $pdf->Cell(20,6,'Course',1,0,'L',1); $pdf->SetX(145); $pdf->Cell(15,6,'Year',1,0,'L',1); $pdf->SetX(160); $pdf->Cell(25,6,'Mess type',1,0,'L',1); $pdf->SetX(185); $pdf->Cell(25,6,'Block Name',1,0,'L',1); $pdf->Ln(); //Now show the 3 columns $pdf->SetFont('Arial','',10); $pdf->SetY($Y_Table_Position); $pdf->SetX(5); $pdf->MultiCell(30,6,$column_no,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(35); $pdf->MultiCell(35,6,$column_name,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(70); $pdf->MultiCell(18,6,$sex_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(88); $pdf->MultiCell(20,6,$comm_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(108); $pdf->MultiCell(20,6,$sem_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(128); $pdf->MultiCell(17,6,$course_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(145); $pdf->MultiCell(15,6,$courseyr_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(160); $pdf->MultiCell(25,6,$mess_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(185); $pdf->MultiCell(25,6,$block_details,1); $i = 0; $pdf->SetY($Y_Table_Position); while ($i < $number_of_products) { $pdf->SetX(5); $pdf->MultiCell(205,6,'',1); $i = $i +1; } $pdf->Output(); ?>
推荐答案
您的表格onsubmit似乎是错误的. onsubmit应该是javascript function而不是php file.如果您需要提交给PHP文件,则应使用form的action属性.目前,您已将其作为report.php.您可能需要将其更改为yearpdf.php或重定向report.php
编辑1:
问题是您正在调用打开URL但不提交输入值的window.open.因此,第一次什么都不会发生.但是,在下一个window.open或下一个刷新中,有来自report.php的会话变量,因此可以检索数据.您可以关注一些选项
- 将报告守则列出来处理在YearPdf.php中处理提交,然后将" Action"表单更改为YearPdf.php.并删除OnClick功能
- 或从report.php重定向到report.php末尾的年yearpdf.php.对于重定向,您可以使用header('Location: http://www.yoursite.com/yearpdf.php');.并删除OnClick功能
- 或modfiy您的javascript代码以发送类似window.open("yearpdf.php" + "?course_year=" + document.getElementsByName("course_year")[0].value));的内容,然后将您的$r1=$_SESSION['year1'];更改为$r1=$_GET['course_year']; yearpdf.php
选项1是推荐和简单的方式.选项2是开销,如果您在重定向前输出任何HTML,则无法正常工作.选项3产生一个不必要的窗口.
问题描述
i have used FPDF for creating pdf in PHP.
I used session variable to pass the variable between one form to another form. When i provide a value in the
Report.php <?php session_start(); $_SESSION['year1']=$_POST['course_year']; $_SESSION['sem1']=$_POST['semester']; $_SESSION['community1']=$_POST['community']; $_SESSION['course1']=$_POST['course']; $_SESSION['mess_type1']=$_POST['mess_type']; $_SESSION['block_name1']=$_POST['block_name']; ?> <form action='report.php' method='POST'name= 'form1'> <p><tr><td><b> Course Year:</b></td><td><input type='text' name='course_year'></td></tr><br></p> <p><tr><td> </td><td><input style='width:105;height:32' type='submit' value='Generate Report'onsubmit='yearpdf.php' onclick="year_open()" ></td></tr> </form> <h2 align='left'><b>Semester</h2></b> <form action='report.php' method='POST'name= 'form2'> <!--<form action='report.php' align='left' method='POST'>--> <p><b><tr><td>Semester:</b></td><td><input type='text' name='semester'></td></tr><br></p> <p><tr><td> </td><td><input style='width:105;height:32' type='submit' value='Generate Report' onsubmit='sempdf.php' onclick="sem_open()"></td></tr></table> </form> .... so on for community, course, messtype and blockname
where yearpdf.php is the file which i have written for the creation of pdf file. As soon as the pdf is generated Blank values with tables are displayed. I have to refresh to get the values displayed in the PDF. My yearpdf file is :
<?php session_start(); require('fpdf/fpdf.php'); //Connect to your database $r1=$_SESSION['year1']; $con=mysql_connect('localhost','root',''); if(!$con) { die('Unable to connect'.mysql_error()); } mysql_select_db('hostel',$con); //Select the list you want to show in your PDF file $result=mysql_query("select hosteladmissionno,student_name,sex,community,semester,course,course_year,mess_type,block_name from registration where course_year='".$r1."' ORDER BY hosteladmissionno"); $number_of_products = mysql_numrows($result); //For each row, add the field to the corresponding column while($row = mysql_fetch_array($result)) { $hostad = $row['hosteladmissionno']; $name = $row['student_name']; $sex = $row['sex']; $sem=$row['semester']; $comm=$row['community']; $course=$row['course']; $courseyr=$row['course_year']; $mess= $row['mess_type']; $block=$row['block_name']; $column_no = $column_no.$hostad."\n"; $column_name = $column_name.$name."\n"; $sex_details = $sex_details.$sex."\n"; $sem_details= $sem_details.$sem."\n"; $comm_details= $comm_details.$comm."\n"; $course_details= $course_details.$course."\n"; $courseyr_details= $courseyr_details.$courseyr."\n"; $mess_details= $mess_details.$mess."\n"; $block_details= $block_details.$block."\n"; //$column_price = $column_price.$price_to_show."\n"; } mysql_close(); //Convert the Total Price to a number with (.) for thousands, and (,) for decimals. //$total = number_format($total,',','.','.'); //Create a new PDF file $pdf=new FPDF(); $pdf->AddPage(); //Fields Name position $Y_Fields_Name_position = 40; //Table position, under Fields Name $Y_Table_Position = 46; //First create each Field Name //Gray color filling each Field Name box $pdf->SetFillColor(232,232,232); //Bold Font for Field Name $pdf->SetFont('Arial','B',10); $pdf->SetY($Y_Fields_Name_position); $pdf->SetX(5); $pdf->Cell(30,6,'Admission No',1,0,'L',1); $pdf->SetX(35); $pdf->Cell(35,6,'Student Name',1,0,'L',1); $pdf->SetX(70); $pdf->Cell(20,6,'Sex',1,0,'L',1); $pdf->SetX(88); $pdf->Cell(20,6,'Category',1,0,'L',1); $pdf->SetX(108); $pdf->Cell(20,6,'Semester',1,0,'L',1); $pdf->SetX(128); $pdf->Cell(20,6,'Course',1,0,'L',1); $pdf->SetX(145); $pdf->Cell(15,6,'Year',1,0,'L',1); $pdf->SetX(160); $pdf->Cell(25,6,'Mess type',1,0,'L',1); $pdf->SetX(185); $pdf->Cell(25,6,'Block Name',1,0,'L',1); $pdf->Ln(); //Now show the 3 columns $pdf->SetFont('Arial','',10); $pdf->SetY($Y_Table_Position); $pdf->SetX(5); $pdf->MultiCell(30,6,$column_no,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(35); $pdf->MultiCell(35,6,$column_name,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(70); $pdf->MultiCell(18,6,$sex_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(88); $pdf->MultiCell(20,6,$comm_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(108); $pdf->MultiCell(20,6,$sem_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(128); $pdf->MultiCell(17,6,$course_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(145); $pdf->MultiCell(15,6,$courseyr_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(160); $pdf->MultiCell(25,6,$mess_details,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(185); $pdf->MultiCell(25,6,$block_details,1); $i = 0; $pdf->SetY($Y_Table_Position); while ($i < $number_of_products) { $pdf->SetX(5); $pdf->MultiCell(205,6,'',1); $i = $i +1; } $pdf->Output(); ?>
推荐答案
Your form onsubmit seems to be wrong. onsubmit should be a javascript function not a php file. if you need to submit to php file you should use action attribute of form. currently you have given it as report.php. you may need to change it to yearpdf.php or redirect in the report.php
EDIT 1:
the problem is you are calling the window.open which opens an url but doesn't submit your input value. so first time nothing will happen. but however on the next window.open or on the next refresh, there is the session variable from report.php and thus retrieves the data. you can follow some options
- put the code of report.php that to process the submit in yearpdf.php and change the form "action" to the yearpdf.php. and remove onclick function
- OR Redirect from report.php to yearpdf.php in the end of report.php. For redirect you can use header('Location: http://www.yoursite.com/yearpdf.php');. and remove onclick function
- OR modfiy your JavaScript code to send something like this window.open("yearpdf.php" + "?course_year=" + document.getElementsByName("course_year")[0].value)); and change your $r1=$_SESSION['year1']; to $r1=$_GET['course_year']; in yearpdf.php
Option 1 is the recommended and simple way. Option 2 is an overhead and won't work if you output any html before redirection. and Option 3 produces an unnecessary window.