如何在提交php表单后显示结果

时间:2013-04-09 07:07:26

标签: php mysql forms

提交表单后如何显示结果

我想在提交打印表格后显示结果

示例第一次填写表单提交结果提交后我想要一个屏幕显示相同的结果 http://www.tizag.com/phpT/examples/formexample.php

我该怎么办? 请帮我解决这个问题。

php表单代码

<?php
     function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
     {
     ?>
     <?php 
     if ($error != '')
     {
     echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
     }
     ?> 
     <form action="" method="post">
     <div>
       <p><span class="style9"><strong>G.R.N No:</strong></span><strong>  *</strong>
         <input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Name:</strong></span><strong>  *</strong>
         <input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
       </p>

       <p><span class="style9"><strong>Roll No :</strong></span><strong>  *</strong>
         <input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Class:</strong></span><strong>  *</strong>
         <input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
       </p>
        <p><span class="style9"><strong>Fees Date  :</strong></span><strong>  *</strong>
          <input id="fullDate"  name="date" type="text" value="<?php echo $date; ?>" size="50" />
       </p>
        <p><span class="style9"><strong>Fees :</strong></span><strong>  *</strong>
          <input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
       </p>
        <span class="style9"><strong>Reference</strong></span><strong> *</strong>
        <input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
        <br/>
       <p class="style1">* required</p>
     <input type="submit" name="submit" value="Submit">
     </div>
     </form> 
     <?php 
     }
     include('connect-db.php');

     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
     $grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
     $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
     $rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
     $class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
     $fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
     $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
     $reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));


     // check to make sure both fields are entered
     if ($grn == '' || $name == '' || $rollno == '')
     {
     // generate error message
     $error = 'ERROR: Please fill in all required fields!';

     // if either field is blank, display the form again
     renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
     }
     else
     {
     // save the data to the database
      mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
     or die(mysql_error()); 
     echo "<center>KeyWord Submitted!</center>";
     // once saved, redirect back to the view page

     }
     }
     else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','','','','','');
     }

    ?>

3 个答案:

答案 0 :(得分:0)

你所要求的并不是那么舒服。

显示提交的表单数据需要帮助吗?或更多特定显示用于打印?

要显示它,您只需要创建一个显示它的html页面。 像:

echo '<table><tr>';
  echo '<td>';
    echo '<Strong>Name:</strong><br/>';
    echo $name;
  echo '</td>';
echo '</tr></table>';

要显示结果而不显示表单,当您发布到同一页面时,需要使用if语句封装for代码。

if(isset($_POST['submit'])) {
  //code for the php form
}else {
  //code to display form
}

答案 1 :(得分:0)

在表单中为“操作”部分使用新页面。在该新页面上,只需回显要显示的$ _POST值。如果您打算制作某种类型的页面以便人们可以检查他们的条目,您可以将这些$ _POST数据存储在会话中。

答案 2 :(得分:0)

只需调用底部的功能即可显示发布的值:

    function showFormValues()
         {
         ?>


         <div>
           <p><span class="style9"><strong>G.R.N No:</strong></span><strong>  *</strong>
              <?php echo $_POST['grn']; ?>
           </p>
           <p><span class="style9"><strong>Name:</strong></span><strong>  *</strong>
              <?php echo $_POST['name'];
           </p>
and so on.
    .
    .
    .
    .
         </div>

         <?php 
         }
?>