如何在PHP中"$ _POST"返回相同页面或不同结果页面? [英] How to '$_POST' back to the same page or to a different result page in php?

查看:91
本文介绍了如何在PHP中"$ _POST"返回相同页面或不同结果页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个PHP文件 Calculator.php ,将结果发布到 info.php ,然后我写了另一个PHP文件- Calculator2.php 将结果发布到同一页面.如何将它们结合在一起?我试图一个接一个地放,但不起作用.

I wrote a php file Calculator.php that posts result to info.php, and I wrote another php file - Calculator2.php that posts results to the same page. How can I combine them together? I tried to put one after another and it doesnt work.

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'];
 ?>

推荐答案

<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

and with the same info.php as before

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

问题在于我认为有很多重复的代码

the problem with it is i think there is quite a lot of repeated code

这篇关于如何在PHP中"$ _POST"返回相同页面或不同结果页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆