【问题标题】:How to '$_POST' back to the same page or to a different result page in php? [closed]如何在 php 中将“$_POST”返回到同一页面或不同的结果页面? [关闭]
【发布时间】:2012-06-24 00:54:12
【问题描述】:

我写了一个 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'];
 ?>

【问题讨论】:

  • 向我们展示您的代码...也许我们可以帮助您。如果看不到您的代码,我们将无能为力。
  • 谢谢Sena,我刚刚加了代码,有机会请看一下!

标签: php post


【解决方案1】:

这是一个巧妙的解决方案。添加一个处理表单提交的通用脚本,然后将其与结果重定向到任何需要的地方。

【讨论】:

  • 谢谢@Aakash 我一周前刚刚学习了 php,实际上我不确定你在说什么。可以举个例子吗?
【解决方案2】:
<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;
}
?>

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

【讨论】:

    【解决方案3】:

    根据需要将您的数据插入以下括号。

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

    【讨论】:

    • 感谢您的帮助,它没有那样工作,我只是添加了代码,您可以再看一下吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 2018-08-09
    • 2014-03-10
    • 1970-01-01
    • 2011-11-10
    • 2011-08-06
    • 2011-06-26
    • 2014-03-11
    相关资源
    最近更新 更多