表单php提交给出相同的样式

时间:2013-11-19 23:47:25

标签: php html forms

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

我希望它以相同的表格样式提交提交,以及在不进行练习的情况下向表单添加新按钮。

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

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
            }
           ?>

1 个答案:

答案 0 :(得分:0)

你应该考虑在html中嵌入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;?>