2014-09-29 1 views
0

안녕하세요 행 및 열 생성에 대한 질문이 있습니다. 한 페이지에만 어떻게 만들 수 있는지 묻고 싶습니다. 이것이 제가 시도한 것입니다.테이블 생성기

HTML :

<html> 
<head> 
<title>Table Generator</title> 

<body> 
<center><h1>Generate Your Table</h1></center> 

<div id="div1"> 
<center><h4>Enter number of Row and Column</h4> 
    <form action="get_table/execute_table.php" method="POST"> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

</div> 



</body> 

PHP : 네, 완전히 실행하지만 난 거기에 단 1 페이지를 원하는

<?php 

$row = $_POST['title1']; 
$column = $_POST['title2']; 

echo "<table border='1'>"; 

for($tr=1;$tr<=$row;$tr++){ 

echo "<tr>"; 
    for($td=1;$td<=$column;$td++){ 
      echo "<td>row: ".$tr." column: ".$td."</td>"; 
    } 
echo "</tr>"; 
} 

echo "</table>"; 
?> 

. 감사.

답변

3

일반적으로 : 자기 게시물에

if($_POST['title']){ 
    //do whatever get_table/execute_table.php did 
}else{ 
    //echo the form here or, if you're allowed, use an include() 
} 

상세 정보 : 어딘가에 페이지에 유사한 조건을 추가합니다.

그런 다음 물론, 형태와 같은 페이지에 PHP 프로세스를 넣어 :

<div id="div1"> 
<center><h4>Enter number of Row and Column</h4> 
    <form action="" method="POST"> 
    <!--  ^^ no more value, well you could just put the same filename --> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

</div> 

<?php 

$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings 
if(isset($_POST['submit'])) { // catch submission button 

    $row = $_POST['title1']; 
    $column = $_POST['title2']; 

    $out .= "<table border='1'>"; 

    for($tr=1;$tr<=$row;$tr++){ 

    $out .= "<tr>"; 
     for($td=1;$td<=$column;$td++){ 
       $out .= "<td>row: ".$tr." column: ".$td."</td>"; 
     } 
    $out .= "</tr>"; 
    } 

    $out .= "</table>"; 

} 

echo $out; // finally echo it 
+0

@Ghost라는 아이디어에 감사드립니다. :) –

+1

@DontStopLearning 확실하게 도움이 됨 – Ghost

1

동일한 페이지에 게시하고 PhP Post 배열에 아직 데이터가 있는지 확인해야합니다.

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST"> 

참고 :

과 함께 양식 조치를 교체 대부분의 (? 모두) 브라우저 빈 조치를 자기 게시물을 지원하지만, 이것은 기술적으로 W3C 호환되지 않습니다.

그런 다음 페이지가 제출되면 동일한 페이지가 다시로드되고 POST 배열이 채워집니다. 같은 페이지에 원하는 경우에, 당신은 단지 그 값으로 action=""를 생략 How do I make a PHP form that submits to self?

+0

같이 모든 HTML의 상단에 PHP 코드 블록을 배치 추가 이'if ($ _ POST [title]) {'는 따옴표 붙일 필요가 있습니다 =>'if ($ _ POST [ 'title']) {'그렇지 않으면 정의되지 않은 상수 오류가 발생합니다. –

+0

고침. 고마워. – CyberEd

+0

반갑습니다. –

1

그냥 빈 속성 조치를 떠나 하나의 페이지를 사용하여이를 달성 할 수 있습니다. 및 top.and에 당신의 PHP 블록 .PHP로 저장해야합니다 및


그래서 결국은이

<?php 

    $row = $_POST['title1']; 
    $column = $_POST['title2']; 

    echo "<table border='1'>"; 

    for($tr=1;$tr<=$row;$tr++){ 

    echo "<tr>"; 
     for($td=1;$td<=$column;$td++){ 
       echo "<td>row: ".$tr." column: ".$td."</td>"; 
     } 
    echo "</tr>"; 
    } 

    echo "</table>"; 
    ?> 

    <html> 
    <head> 
    <title>Table Generator</title> 

    <body> 
    <center><h1>Generate Your Table</h1></center> 

    <div id="div1"> 
    <center><h4>Enter number of Row and Column</h4> 
    <form action="" method="POST"> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

    </div> 



    </body>