在没有ajax的情况下,从一个页面到另一个页面的响应

时间:2015-02-17 06:56:35

标签: php

我刚开始接受Php并且有些疑惑。

我创建了2页one.php和two.php

ONE.php
<body>
  <form method="post" action="TWO.php">
    First Number<input type="text" name="txt1"><br>
    Second Number<input type="text" name="txt2"><br>
  <input type="submit">
</form>
</body>

TWO.php
<body>
  <?php
    $sum=$_POST["txt1"] + $_POST["txt2"];
    echo $sum;
  ?>
</body>

我将POST值从one.php转换为two.php。 Two.php计算总和并回显结果。 我的查询是,我可以使用php获取在one.php上回显的总和,重要的是将数据发布到另一个页面并从那里获得响应。

3 个答案:

答案 0 :(得分:1)

是的,你是。简单地说,在 one.php 中处理表单提交(POST请求)。当 one.php 的请求不是POST时,只显示表单。

典型的方式是:

// ONE.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $content = "Sum is " . $_POST["txt1"] + $_POST["txt2"];
}
else {
    $content = <<<EOC
     <form method="post" action="ONE.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
        <input type="submit">
     </form>
EOC;
}
?> 
<!DOCTYPE html>
<html>
<head>
  <title>ONE</title>
  <meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
  <?php echo $content ?>
</body>
</html>

编辑后,OP需要两个文件,但在 one.php上显示结果

为了在两个文件之间传递数据,您可以:

  • 在第一个文件中设置数据( two.php )并要求第二个文件( one.php ,您将打印该值)
  • 使用PHP sessions

使用后者你需要做一个(你不需要需要,但这意味着使用a)页面重定向,所以我推荐的方法就是使用前者。代码可能是这样的:

// TWO.php
<?php
// you should probably check if $_POST['txt1'] and $_POST['txt2'] does really exists and throw and error if not...
$sum = $_POST["txt1"] + $_POST["txt2"];

require "ONE.php" // careful if you're on a *nix file system the NameCase is extremely important!


// ONE.php
<?php
if (isset($sum)) {
    $content = "Sum is " . $sum;
}
else {
    $content = <<<EOC
     <form method="post" action="TWO.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
        <input type="submit">
     </form>
EOC;
}
?> 
<!DOCTYPE html>
<html>
<head>
  <title>ONE</title>
  <meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
  <?php echo $content ?>
</body>
</html>

答案 1 :(得分:1)

试试这段代码:

<body>
<?php
if($_POST){
    $sum = $_POST["txt1"] + $_POST["txt2"];
    echo $sum;
}else{
?>
<form method="post" action="">
        First Number<input type="text" name="txt1"><br />
        Second Number<input type="text" name="txt2"><br />
        <input type="submit">
    </form>
<?php } ?>
</body>

答案 2 :(得分:1)

您只需一页即可尝试:

ONE.php

<html>
<head></head>
<body>
    <form method="post" action="ONE.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
       <input type="submit">
    </form>
    <?php
        // Verify if $_POST["txt1"] and $_POST["txt1"] are defined
        // (when form is submit $_POST, $_GET and other $_ PHP vars 
        // are set). If form isn't submitted, set 0 on each variable 
        // to perform sum. Is necessary check values to avoid PHP
        // Warnings/Errors (In this case with isset function. There
        // are many different ways to perform it)
        $txt1 = isset($_POST["txt1"]) ? $_POST["txt1"] : 0;
        $txt2 = isset($_POST["txt2"]) ? $_POST["txt2"] : 0;
        $sum = $txt1 + $txt2;
        //Print sum result
        echo 'Sum is: '.$sum;
    ?>
</body>
</html>

对于我正在使用ternary operators的比较,这简化/最小化了传统if / else的代码。另外you can use traditional if/else (simple compare) or switch(multiple compare)