-3

I am getting confused as I read various posts about the typical task I am trying to perform, no doubt countless others have done it before. So at this point in 2013 what is the best practice solution to achieve the following:

  • I have an html page which asks the user for a number of values, x1, x2, .....xn.
  • There may be 10 to 20 such inputs. As such I don't want them to be lost or to have to be re-inputted again for whatever reason.
  • perform calculations based on user inputs then output the results (eg. y1, y2, y3..y5).
  • As suggested by some of the posts here, I tried "echo" (for PHP), that works fine. However, it shows the results in a new page so the user input is no longer visible.
  • I want the user to see both - their input and the resultant. That way they can print if they want to and see all info on one page.
  • I prefer to use "basic" technologies, ie. technologies that most users can use, that they don't have to click OK on some warning, change some setting on their browser, accept some thing or other etc..

Thanks in advance!

4

2 回答 2

1
  1. 使用 html 创建输入。
  2. 在重新加载页面或 AJAX 之间进行选择
  3. 如果您选择重新加载,则使用

代码:

  <form action="nextfile.php" method="POST">
   <input type="text" value="" name="y1" />
   <input type="text" value="" name="y2" />
   <input type="submit" value="calc me" name="submit" />
  </form> 

然后在 nextfile.php 中,您需要获取值,$_POST如果您希望保存它们,请使用$_SESSION

例如

<?php 
session_start();
if(isset($_POST['y1']) && isset($_POST['y2']))
{
    $_SESSION['res'] = (int)$_POST['y1'] * (int)$_POST['y2'];
}

上面的代码将对名为 y1 和 y2 的两个输入执行计算并将它们保存在会话中。

如果您想要 AJAX,那么您需要访问此页面并查看示例

您应该考虑 JavaScript 解决方案,因为它可以满足您的需求,并且不需要服务器代码。

于 2013-07-09T10:32:32.240 回答
0

最简单的方法是将表单提交到同一页面并重新填充输入字段:

// calc.php
<?php

    if (isset($_POST['foo'])) {
        echo 'Result: ', $_POST['foo'] + 1;
    }

?>

<form action="calc.php" ...>
   <input name="foo" value="<?php if (isset($_POST['foo'])) echo htmlspecialchars($_POST['foo']); ?>">
   ...
</form>

更现代的版本是通过 AJAX 提交计算并通过 Javascript 填充结果而不重新加载页面。

于 2013-07-09T10:32:43.203 回答