|
Posted by David Haynes on 05/29/06 15:22
pepito3@gmail.com wrote:
>> 2. Have the page call another php file that processes the form data and
>> then redirects to the calling form page again.
>
> How can I make the called file to acknowledge the calling file?
>
> i.e.:
>
> *** product_list.php:
> ...
> <a href="add_product.php?code=...> Buy! </a>
> ...
>
> *** add_product.php:
>
> <?php
> add_product($_GET['code']);
> // How to go back to product_list.php (or whatever file previous
> to add_product.php)?
>
> ?php>
>
You would use the header() function of php.
I use this function I use. The original code was posted here a while
back by someone else.
<?php
/**
* Module: redirect.inc
*
* Manages a redirection when there may be a session active.
* Also correctly re-addresses URLs that are not absolute.
*
* @param string $url
*/
function redirect($url) {
session_write_close();
if( substr($url, 0, 4) != 'http' ) {
if( isset($_SERVER['HTTP_HOST']) ) {
$url = 'http://'.$_SERVER['HTTP_HOST'].$url;
} else {
$url = 'http://localhost'.$url;
}
}
header("location: $url");
exit;
}
?>
So, your code would become:
<?php
add_product($_GET['code']);
redirect('product_list.php');
?>
If you are going to use the two file approach, then you really need to
start understanding sessions.
add_product.php
<?php
include('redirect.inc');
session_start();
// process the GET values
if( isset($_GET['code') ) {
$SESSION['product_codes'][] = $_GET['code'];
}
redirect('list_product.php');
?>
<?php
session_start();
// read back the product codes
$product_codes = $_SESSION['product_codes'];
echo "You have ordered: ".implode(', ', $product_codes); // for example
....
?>
Also, it would not hurt to start using a naming convention to help
associate the view with the controller. I use the following:
foo.html.php - this is the HTML format view for the code.
foo.xml.php - this is the XML format view for XSLT processes.
foo.ctrl.php - this is the controller for all the foo views.
I also pass the controller URL from the controller to the view via the
SESSION. That way I always know that <form action="<?php echo
controller;?>" method="xxx"> will always call the correct controller
(i.e. the controller that called the view in the first place.)
-david-
[Back to original message]
|