【问题标题】:Response from one page to another page in php without ajax在没有ajax的情况下从一个页面到另一个页面的php响应
【发布时间】:2015-02-17 15:34:55
【问题描述】:

我刚开始使用 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>

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

【问题讨论】:

  • 使用 sum 参数重定向到 one.php 页面并在 one.php 中获取总和。
  • 如果没有 AJAX,您将需要重新加载页面。你同意吗?
  • @jQuery:很好,这是一个解决方案,但是先生,如果数据不仅仅是总和和一些不应该被陶醉的大数据怎么办。
  • @Alejandro: 可以重新加载。
  • @Sumeet 。那么你需要使用php会话。在重定向到 one.php 页面之前在会话中存储日期。如果设置了会话,则打印该数据。

标签: php


【解决方案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>

【讨论】:

  • 当然,先生,我非常感谢您的努力,但将数据发布到另一个页面并从那里获得响应很重要。
  • @Sumeet,现在怎么样?
【解决方案2】:

试试这个代码:

<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>

【讨论】:

    【解决方案3】:

    你可以试试这个,只用一页:

    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)

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2019-08-30
      • 2019-01-14
      • 2014-09-24
      • 2013-01-28
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多