I'm a bit new to working with classes in PHP. What I'm attempting to do is create some back-end processing, and when an error is returned, store the error and let it display wherever I need it to display.
The basic flow I'm shooting for:
1. edit.php?status=new sets up the empty form
2. process.php calls class newsHandler.php to do some checking and/or insertion
3. if an error occurs, store the error message, return use to edit.php?status=new and display error message
So as an example, we set up a new form on the edit.php page:
case 'new':
$addnews = new newsHandler;
$addnews->pageTitle("New Article");
$oFCKeditor->Value = '' ;
break;
....
<form action="process.php" method="post">
<input name="id" type="hidden" value="<?php echo $_POST['id']; ?>">
<?php
$oFCKeditor->Create() ;
?>
<br><br>
<input type="button" value="Return to Main" onClick="location.href='main.php'">
<input type="submit" value="Submit">
</form>
The form posts to process.php. Process.php calls class newsHandler.php addArticle method:
class newsHandler {
function addArticle($id,$text,$date='') {
if ( isset( $_POST ) ) {
$postArray = &$_POST ; // 4.1.0 or later, use $_POST
} else {
$postArray = &$HTTP_POST_VARS ; // prior to 4.1.0, use HTTP_POST_VARS
}
foreach ( $postArray as $sForm => $value )
{
if ( get_magic_quotes_gpc() ) {
$postedValue = htmlspecialchars( stripslashes( $value ) ) ;
} else {
$postedValue = htmlspecialchars( $value ) ;
}
}
//if date is empty, set it to now
if($date=='') { $date = strtotime("now"); }
$data = array(
'id' => $id,
'text' => $postedValue,
'created_dtm' => $date
);
$database = new db_class;
$database->connect();
if (empty($data['text'])) {
$message[] = "Please enter some text into the editor; we can't allow empty text.<br />";
} else {
$insert_data = $database->insert_array(TBL_NEWS, $data);
if (!$insert_data) {
$database->print_last_error(false);
$database->print_last_query();
} else {
$message[] = "Article successfully added.<br />";
}
}
foreach($message as $key => $value){
return $value . "<br />";
}
}
}
Here is process.php in its barest form.
require_once('../database/constants.php');
require_once('../database/db.class.php');
require_once("includes/newsHandler.php");
$referer = $_SERVER['HTTP_REFERER'];
if (preg_match("/status=new/i", $referer)) {
$addnews = new newsHandler;
$data = $addnews->addArticle('Null',$postedValue,strtotime("now"));
if($data == false) {
//$host = $_SERVER['HTTP_HOST'];
//$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//$extra = 'mypage.php';
//header("Location: http://$host$uri/$extra");
//exit;
print_r($_POST);
echo $message;
return;
} else {
//$addnews->pageTitle("New Article");
}
} elseif (preg_match("/status=edit/i", $referer)) {
print_r($_POST);
}