表单php提交提供了相同的风格


form php submission gives same style

我正在用PHP表单创建一个页面,它运行良好。但问题是,当我点击checl按钮检查时,它会在白色页面中给出答案,而不是在同一个html表格中。

我希望它以相同的表格样式提交,并在表格中添加一个新按钮,在不进行练习的情况下显示答案。

这是一个例子,展示了我想要做的事情:

http://www.englisch-hilfen.de/en/exercises/questions/simple_present.htm

<?php
   $correctSolution1 = 'do';
   $correctSolution2 = 'do';
   if(isset($_POST['add']) == TRUE) {
       $solution1 = $_POST['solution1'];
       if(empty($solution1) == TRUE) {
           echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
       } else if ($solution1 == $correctSolution1) {
           echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
       } else {
           echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
       }
       $solution2 = $_POST['solution2'];
       if ( empty($solution2) == TRUE ) {
           echo '<span style="color: blue;">"_____"</span> you like lemon? <img src="http://www.pavendors.com/images/smileyface.gif" width="16px" height="16px" border=0></br>';
       } else if ( $solution2 == $correctSolution2 ) {
           echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
       } else {
           echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>';
       }
       echo '<a href="' . $_SERVER['HTTP_REFERER'] . '"><input type="button"  value="Repeat test" /></a>';
    } else {
?>
    <form  method="post" autocomplete="off">
        <table align=center width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
         <td>
          <table align=left width="100%" bgcolor="#FFCC99" border="0" cellspacing="0" cellpadding="1">
            <tr>
              <td>
              <table width="100%" bgcolor="#FFFFCC" cellspacing="4" cellpadding="1">
                <tr></tr>
        <tr><td></td></tr>
                <tr> 
                    <td>
               <table>                                                <tr><td  colspan="2">1) <input type="text"  name="solution1" value="" size="6" /> you      <input type="text" class="ex_textfield" id="1" tabindex="1" name="solution2" value="" size="6" /> mineral water? <b><i>(to drink)</i></b></td></tr>                       
                       </table>
            </td>
        </tr>
                <tr><td height="1"><hr></td></tr>
                <tr>
                  <td align="left"><input class="small_button" type="submit" name="add" value="Check" />
                <br>
                <hr> </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
   </tr>
 </table>
</form>
          <?php
            }
           ?>

您应该查看html中的embeeding php:在上面的例子中,你正在做:

<?php
   $correctSolution1 = 'do';
   $correctSolution2 = 'do';
   if(isset($_POST['add']) == TRUE) {
   }
   else{?>
<form>
<table></table>
</form>
<?php }?>

因此,当您从进行提交(发布请求)时,这里的代码不会转到表单和表部分。您可以将表格部分添加到post-if语句部分,如下所示:

<?php
       $correctSolution1 = 'do';
       $correctSolution2 = 'do';
       if(isset($_POST['add']) == TRUE) {
    //do your php stuffs here
?>
<table>
...
your html you can include values in to it like this <?php echo $solution2;?>
...
</table>
<?php
}
else{?>
<form>
  <table></table>
</form>
<?php }?>

或者你也可以这样做:

<?php 
  //php part here may be some variable assignments
  $correctSolution1 = 'do';
  correctSolution2 = 'do';
?>
<form>
<table>
....
<?php if(isset($_POST['add']) == TRUE) {?>
 your other code here for result
<?php }?>
....
</table>
</form>

因此,基本上您需要正确理解如何将php嵌入到html中。您可以在php中查找语句的替代语法,如if、while、for、switch;http://php.net/manual/en/control-structures.alternative-syntax.php你可以做这样的事情:

<?php if(true):?>
//if code here
<?php endif;?>