2013-12-16 161 views
-1

我正在製作一個表單,我需要爲X和Y插入2個值來創建X行和Y行的表。但我無法讓表格出現。該函數被調用,但HTML被忽略,出於某種原因,我只得到一行內容「Line 1 Line 2 Line3 ... Row 1,Row 2,...」。將PHP與HTML混合使用

這裏是我的代碼:

<body> 
<form method="post" action="<?php $_SERVER['REQUEST_URI'] ?>" > 

    X= <input type="text" name="x" /> <br /> 
    Y= <input type="text" name="y" /> <br /> 
    Diferença= <select name="expressao"> 
        <option value=">"> > </option> 
        <option value="<"> < </option> 
        <option value="="> = </option> 
       </select> 
    <input type="submit" value="enviar" /> 
    <input type="hidden" name="enviar" value="1" /> 

</form> 
<hr /> 

<?php 

function tabela() { 
    tabelashow(); 
} 

if (!empty($_POST['enviar'])) { 

    $x=$_POST['x']; 
    $y=$_POST['y']; 
    $expressao=$_POST['expressao']; 

    echo "O valor da expressão $x $expressao $y é "; 

    if ($expressao == ">") { 
     if ($x > $y) { 
      echo "Verdadeiro"; 
      tabela(); 
      } 
     else { 
      echo "Falso"; } 
    } 

    elseif ($expressao == "<") { 
     if ($x < $y) { 
      echo "Verdadeiro"; } 
     else { 
      echo "Falso"; } 
     } 

    else { 
     if ($x == $y) { 
      echo "Verdadeiro"; 
     } 
     else { 
      echo "Falso"; } 
     } 
} 

?> 
<hr /> 
<table border="1"> 
<?php 
function tabelashow() { 
    for ($t=1; $t<=3; $t++) { 
     echo "<tr>"; 
     for ($u=1; $u<=3; $u++) { 
      echo("<td>Coluna $u </td>"); 
     } 
     echo "</tr>"; 
    } 
} 
?> 
</table> 
</body> 
+0

這是爲什麼遷移? O.O – bjb568

回答

2

你錯過了一個分號,並在第一行

<?php print $_SERVER['REQUEST_URI']; ?> 

echoprint,做jessh太說。似乎是合法的。

1

實際的問題是表格的HTML代碼在之前輸出標籤。

這裏是你的PHP輸出:

O valor da express�o 3 > 2 � Verdadeiro<tr><td>Coluna 1 </td><td>Coluna 2 </td><td>Coluna 3 </td></tr><tr><td>Coluna 1 </td><td>Coluna 2 </td><td>Coluna 3 </td></tr><tr><td>Coluna 1 </td><td>Coluna 2 </td><td>Coluna 3 </td></tr><hr /> 
<table border="1"> 

</table> 

一種方法來解決,這將是把<table border="1">標籤上述這樣調用tabela():

</form> 
<hr /> 
<table border="1"> 
<?php 

function tabela() { 
    tabelashow(); 
} 

if (!empty($_POST['enviar'])) { 
... 

OR您可以在tabelashow()函數中回顯表格標籤,如下所示:

function tabelashow() { 
    echo '<table border="1">'; 
    for ($t=1; $t<=3; $t++) { 
     echo "<tr>"; 
     for ($u=1; $u<=3; $u++) { 
      echo("<td>Coluna $u </td>"); 
     } 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 

我只是在我自己的服務器上嘗試過它,它可以工作。

1

使用echo短標籤形式的行動屬性..

<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>" > 
0

什麼舊文章。以防萬一它能幫助某人。

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" > 

    X= <input type="text" name="x" /> <br /> 
    Y= <input type="text" name="y" /> <br /> 
    Diferença= <select name="expressao"> 
        <option value=">"> > </option> 
        <option value="<"> < </option> 
        <option value="="> = </option> 
       </select> 
    <input type="submit" value="enviar" /> 
    <input type="hidden" name="enviar" value="1" /> 

</form> 
<hr /> 
<?php 
$tabela_poner = false; 

if(isset($_POST['enviar'])){ 
    $x = $_POST['x']; 
    $y = $_POST['y']; 
    $expressao = $_POST['expressao']; 

    function tabelashow(){ 
     global $tabela_poner; 
     if($tabela_poner): 
      global $x, $y; 
      ?> 
      <table border="1"> 
       <?php for($i = 1; $i <= $x; $i++): ?> 
       <tr> 
        <?php for($j = 1; $j <= $y; $j++): ?> 
        <td>Fila #<?php echo $i; ?>/Coluna #<?php echo $j; ?></td> 
        <?php endfor; ?> 
       </tr> 
       <?php endfor; ?> 
      </table> 
     <?php endif; // ($tabela_poner) 
    } 

    function tabela(){ 
     global $tabela_poner; 
     $tabela_poner = true; 
    } 

    echo "O valor da expressão $x $expressao $y é "; 

    // verdad o no 
    switch($expressao){ 
     case '>': 
      if($x > $y){ 
       echo 'Verdadeiro'; 
       tabela(); 
      } 
      else 
       echo 'Falso'; 
      break; 
     case '<': 
      echo $x < $y ? 'Verdadeiro' : 'Falso'; 
      break; 
     case '=': 
      echo $x === $y ? 'Verdadeiro' : 'Falso'; 
    } 
    ?><hr /><?php 
} 
?> 
<?php tabelashow(); ?>