2012-06-21 427 views
-9

我寫了一個PHP文件Calculator.php中該帖子引起對info.php的,我寫了另一個PHP文件 - Calculator2.php說的結果到同一個頁面。我怎樣才能將它們結合在一起?我試圖把一個又一個,並沒有工作。

Calculator.php中

<FORM action="info.php" method="post"> 
<P> 
<LABEL for="firstNum">First Number: </LABEL> 
<INPUT type="text" name="firstNum" id="firstNumberLabel"><BR> 
<LABEL for="secondNum">Second Number: </LABEL> 
<INPUT type="text" name="secondNum" id="secondNumberlabel"><BR> 

<INPUT type="radio" name="calc" value="1"> Add<BR> 
<INPUT type="radio" name="calc" value="2"> Subtract<BR> 
<INPUT type="radio" name="calc" value="3"> Multiply<BR> 
<INPUT type="radio" name="calc" value="4"> Divide<BR> 
<INPUT type="submit" value="Send"> 
<INPUT type="reset"> 
</P> 
</FORM> 

info.php的文件

<?php 

switch ($_POST["calc"]){ 
case "1": 
echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>"; 
break; 
case "2": 
echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>"; 
break; 
case "3": 
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>"; 
break; 
case "4": 
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>"; 
break; 
default: 
break; 
} 
?> 

Calculator2.php

<form action= <?php echo $_SERVER['PHP_SELF'] ?> method="post"> 
    <label for="text_field">First Number: </LABEL> 
    <input type="text" name="user_text" id="text_field"><BR> 
    <label for="text_field2">Second Number: </LABEL> 
    <input type="text" name="user_text2" id="text_field2"><BR> 

<input type="submit" value="Add"> 

</form> 

<?php 
    if(isset($_POST['user_text'])&&isset($_POST['user_text2'])) 
    echo $_POST['user_text']+$_POST['user_text2']; 
?> 
+2

向我們展示您的代碼...也許我們可以幫助您。 沒有看到你的代碼,我們不能幫你。 – Sena

+0

謝謝你,塞納,我剛添加了代碼,請看看你是否有機會! – HoKy22

+0

可能的重複:http://stackoverflow.com/questions/2996806/can-one-form-have-multiple-actions – Don

回答

1
<FORM action="info.php" method="post"> 
<P> 
<LABEL for="firstNum">First Number: </LABEL> 
<INPUT type="text" name="firstNum" id="firstNumberLabel"><BR> 
<LABEL for="secondNum">Second Number: </LABEL> 
<INPUT type="text" name="secondNum" id="secondNumberlabel"><BR> 

<INPUT type="radio" name="calc" value="1"> Add<BR> 
<INPUT type="radio" name="calc" value="2"> Subtract<BR> 
<INPUT type="radio" name="calc" value="3"> Multiply<BR> 
<INPUT type="radio" name="calc" value="4"> Divide<BR> 
<INPUT type="submit" value="Send"> 
<INPUT type="reset"> 
</P> 
</FORM> 


<form action= <?php echo $_SERVER['PHP_SELF'] ?> method="post"> 
<label for="text_field">First Number: </LABEL> 
<input type="text" name="firstNum" id="text_field"><BR> 
<label for="text_field2">Second Number: </LABEL> 
<input type="text" name="secondNum" id="text_field2"><BR> 

<INPUT type="radio" name="calc" value="1"> Add<BR> 
<INPUT type="radio" name="calc" value="2"> Subtract<BR> 
<INPUT type="radio" name="calc" value="3"> Multiply<BR> 
<INPUT type="radio" name="calc" value="4"> Divide<BR> 
<INPUT type="submit" value="Send"> 


</form> 


<?php 
switch ($_POST["calc"]){ 
case "1": 
echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>"; 
break; 
case "2": 
echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>"; 
break; 
case "3": 
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>"; 
break; 
case "4": 
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>"; 
break; 
default: 
break; 
} 
?> 

,並與同爲info.php像以前

<?php 
switch ($_POST["calc"]){ 
case "1": 
echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>"; 
break; 
case "2": 
echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>"; 
break; 
case "3": 
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>"; 
break; 
case "4": 
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>"; 
break; 
default: 
break; 
} 
?> 

它是我認爲有相當多的重複代碼

0

在需要的地方將數據插入下面的括號中。

<?php 
if(sizeof($_POST) == 0){ 
    // echo html here 
} 
else{ 
    // Perform php actions here 
} 
+0

感謝您的幫助,它沒有這樣工作,我只是添加了代碼,你會打開再看一下? – HoKy22

0

這裏是一個整潔的解決方案的問題。添加一個處理提交表單的通用腳本,然後將結果重定向到任何需要的位置。

+0

謝謝@Aakash我剛剛在一週前瞭解了PHP,其實我不確定你在說什麼。你可以給我一個例子嗎? – HoKy22