请参阅下面的我的代码。
在关闭表格标签之后,如何将控件返回到index.php
到可执行语句以在index.php
页面上显示查询结果,而不是processData.php
末尾显示的代码?
我已经通过Google搜索了这个论坛,但没有看到解决方案?那些熟悉FORTRAN的人,Visual Basic会喜欢return
语句,该语句可用于返回呼叫例程。是否有类似于return
的语句将控件移交给PHP中的调用网页?
index.php
的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sales Report Summary</title>
</head>
<body>
<h1>Online Sales Report Demo
<br>
<br>
<form method="post" action="processData.php">
Enter Your Full Name:<input type="text" name= "author_name" />
<br>
Enter Your eMail Address:<input type="text" name= "author_email" />
<br>
<input type="submit" value="Submit">
</form>
**//Question - How to return the control to a statement after </form> tag to print values instead of processData.php**
</body>
</html>
processData.php
的代码:
<?php
// define variables and set to empty values
$author = $email = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//Next extract the data for further processing
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$author = test_input($_POST['author_name']);
$email = test_input($_POST['author_email']);
//Next echo the Values Stored
echo "<br>";
echo "<br>Your Name:".$author;
echo "<br>Your Email Address:".$email;
}
?>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydatabase');
$sql = "SELECT Sales_Author, Sales_Date, Sales_Channel, Sales_Title, Sales_Unit, Sales_Royalty, Sales_Currency
FROM Sales_tbl
WHERE Sales_Email= '" . $email . "' ORDER BY Sales_Date ASC";
$result = mysql_query( $sql, $conn );
if(!$result )
{
die('Could not fetch: ' . mysql_error());
}
?>
<table border="2" style="background-color: #84ed86; color: #761a9b; margin: 0 auto;">
<thead> <tr> <th>Date</th><th>Channel</th><th>Title</th><th>Units</th><th>Royalty</th><th>Currency</th></tr></thead><tbody>
<?php
while($row = mysql_fetch_assoc($result)){
echo "<tr>
<td>{$row['Sales_Date']}</td>
<td>{$row['Sales_Channel']}</td>
<td>{$row['Sales_Title']}</td>
<td>{$row['Sales_Unit']}</td>
<td>{$row['Sales_Royalty']}</td>
<td>{$row['Sales_Currency']}</td>
</tr>n"; }
?>
</tbody></table>
<?php
mysql_close($conn);
echo "Fetched data successfullyn";
?>
</body>
</html>
如果我接受你的问题,这是一种可能性:
在 processData.php
中,将该链接放在您想要的位置:
<a href="index.php?state=fromProcess">back to index.php</a>
然后您可以在index.php中对该参数做出反应:
....
<input type="submit" value="Submit">
</form>
//Question - How to return the control to a statement after </form> tag to print values instead of processData.php**
<?php
if(isset($_GET['state'])) {
switch($_GET['state']) {
case "fromProcess":
echo "I come from process!";
// do whatever you want to do
break;
default:
echo "ERROR: undefined state submitted";
}
}
?>
</body>
</html>
当然,也有可能无需通过
单击链接而重定向的可能性header("location: index.php?state=fromProcess");
// NOTE: This has to be before ANY output, and no output after that will be seen.
但是我有一种感觉,您实际上想显示从index.html中生成的数据。
然后您至少有两种可能性:
在index.php
中包括processData.php
并将其包装在
<?php
if(isset($_POST['author_name'])) {
include('processData.php');
// process the data and make output
}
?>
也可以反过来(但这对您想做的事情有点棘手)。
您必须牢记的一件事:
PHP脚本是脚本,基本上都是脚本。一旦完成执行,它们就会不在记忆中,因此除非将其包含在当前脚本中(没有自动加载 - 但不是您想要的)。