在没有ajax的php中从一个页面到另一个页面的响应
我刚开始学习Php,有一些疑问 我创建了两个页面,分别是one.php和two.php在没有ajax的php中从一个页面到另一个页面的响应,php,Php,我刚开始学习Php,有一些疑问 我创建了两个页面,分别是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> <inpu
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
第一个号码
第二个号码
2.php
我将值从one.php发布到two.php。php计算总和,echo计算结果。
我的问题是,我是否能够仅使用php在one.php上获得回音,将数据发布到另一个页面并从那里获得响应很重要。是的。简单地说,在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>
//ONE.php
一个
编辑后,OP需要两个文件,但将结果打印在一个.php上
为了在两个文件之间传递数据,您可以:
- 在第一个文件(two.php)中设置数据,并需要第二个文件(one.php,您将在其中打印值)
- 使用
// 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>
//TWO.php
是的,你是。简单地说,在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>
//ONE.php
一个
编辑后,OP需要两个文件,但将结果打印在一个.php上
为了在两个文件之间传递数据,您可以:
- 在第一个文件(two.php)中设置数据,并需要第二个文件(one.php,您将在其中打印值)
- 使用
对于后者,您需要执行页面重定向(当然,您不需要这样做,但使用页面重定向是很重要的),因此我推荐使用前者。代码可能类似于:
// 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>
//TWO.php
是的,你是。简单地说,在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>
//ONE.php
一个
编辑后,OP需要两个文件,但将结果打印在一个.php上
为了在两个文件之间传递数据,您可以:
- 在第一个文件(two.php)中设置数据,并需要第二个文件(one.php,您将在其中打印值)
- 使用
对于后者,您需要执行页面重定向(当然,您不需要这样做,但使用页面重定向是很重要的),因此我推荐使用前者。代码可能类似于:
// 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>
//TWO.php
是的,你是。简单地说,在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>
//ONE.php
一个
编辑后,OP需要两个文件,但将结果打印在一个.php上
为了在两个文件之间传递数据,您可以:
- 在第一个文件(two.php)中设置数据,并需要第二个文件(one.php,您将在其中打印值)
- 使用
对于后者,您需要执行页面重定向(当然,您不需要这样做,但使用页面重定向是很重要的),因此我推荐使用前者。代码可能类似于:
// 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>
//TWO.php
请尝试以下代码:
<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>
第一个号码
第二个号码
尝试以下代码:
<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>
第一个号码
第二个号码
尝试以下代码:
<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>
第一个号码
第二个号码
尝试以下代码:
<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>
第一个号码
第二个号码
您可以在一个页面中尝试:
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>
第一个号码
第二个号码
您可以在一个页面中尝试:
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>
第一个号码
第二个号码
您可以在一个页面中尝试:
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>
第一个号码
第二个号码
您可以在一个页面中尝试:
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>
第一个号码
第二个号码
使用sum参数重定向到one.php页面,并在one.php中获取sum。如果没有AJAX,您将重新加载页面。你同意吗?@jQuery:很好,这是一个解决方案,但是先生,如果数据不仅仅是总和和一些不应该被狂欢的大数据,那该怎么办呢。@亚历杭德罗:可以重新加载。@Sumeet。然后您需要使用php会话。在重定向到one.php页面之前,在会话中存储日期。如果设置了session,则使用sum参数将data.redirect打印到one.php页面,并在one.php中获取sum。如果不使用AJAX,则将重新加载页面。你同意吗?@jQuery:很好,这是一个解决方案,但是先生,如果数据不仅仅是总和和一些不应该被狂欢的大数据,那该怎么办呢。@亚历杭德罗:可以重新加载。@Sumeet。然后您需要使用php会话。在重定向到one.php页面之前,在会话中存储日期。如果设置了session,则使用sum参数将data.redirect打印到one.php页面,并在one.php中获取sum。如果不使用AJAX,则将重新加载页面。你同意吗?@jQuery:很好,这是一个解决方案,但是先生,如果数据不仅仅是总和和一些不应该被狂欢的大数据,那该怎么办呢。@亚历杭德罗:可以重新加载。@Sumeet。然后您需要使用php会话。在重定向到one.php页面之前,在会话中存储日期。如果设置了session,则使用sum参数将data.redirect打印到one.php页面,并在one.php中获取sum。如果不使用AJAX,则将重新加载页面。你同意吗?@jQuery:很好,这是一个解决方案,但是先生,如果数据不仅仅是总和和一些不应该被狂欢的大数据,那该怎么办呢。@亚历杭德罗:可以重新加载。@Sumeet。然后您需要使用php会话。在重定向到one.php页面之前,在会话中存储日期。如果设置了会话,则打印该会话