简体   繁体   中英

How can I return the control to the web page from which another php webpage was called

Please see my code below.

How can I return the control to the index.php to an executable statement after the closing form tag to display query results on index.php page rather than code shown toward the end of processData.php ?

I have searched through Google, this forum and have not seen a solution? Those familiar with Fortran, Visual Basic would appreciate the return statement that can be used to go back to the calling routine. Is there a statement similar to return to hand over the control to the calling web page in PHP?

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

Code for 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 successfully\n";
  ?>
  </body>
  </html>

If I take your question literaly this is one possibility:

In processData.php put that link where ever you want it to be:

<a href="index.php?state=fromProcess">back to index.php</a>

then you can react on that parameter in 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>

There's of course also the possibility to redirect without clicking a link via

header("location: index.php?state=fromProcess");
// NOTE: This has to be before ANY output, and no output after that will be seen.

But I have the feeling, that you actually want to display data generated from processData in index.html.

Then you got at least two possibilities:

Include processData.php in index.php and wrap it in a

<?php
if(isset($_POST['author_name'])) {
       include('processData.php');
       // process the data and make output
}
?>

Same is possible the other way round (but that's kinda tricky for what you want to do).

One general thing you have to keep in mind:
php scripts are scripts, basicly all on their own. They are out of memory once they finished executing, so you cannot call a function of another script unless you include it into the current script (without autoload - but that not what you want).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM