Jump to content

Handling Data not gathered in Form


doubledee

Recommended Posts

This is a tough one to summarize - especially in the Subject?!

 

I need to insert a Comment which consists of:

 

* article_id

* member_id

* body

* status

* created_on

* approved_on

* updated_on

 

Only the "body" field is gathered from my "Add a Comment" form, and everything else either comes from the $_SESSION or from NOW()

 

I am wondering if it would be easier to do Error-Handling and what-not if I created an Input for at least the first 4 fields above and populated the "Value" of those fields that already have values (e.g. "member_id")??

 

Then when the user clicks "Add a Comment", I can check all of the values in $_POST and handle everything in a uniform way, if you follow my new thinking?!

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Im not sure i understand your question, but if you already have the values you want in a variable and don't want the user to change them, then there is no reason to populate a form with them. Just use the values you have.

 

Having a hard time formulating.  (Just so confused on this whole damn "Add a Comment" project...)  :-\

 

I don't think my original post/idea made sense because the user never knows the ArticleID and MemberID exist, and *if* they were blank, it's not like the user could do anything anyways.

 

So here is a second attempt to clean up my code.

 

Does this make sense?

 

// *************************************************************
// HANDLE FORM.											 *
// *************************************************************
if ($_SERVER['REQUEST_METHOD']=='POST'){
// Form was Submitted (Post).

// Connect to the database.
require_once('private/mysqli_connect.php');

// Initialize Errors Array.
$errors = array();

// Trim all form data.
$trimmed = array_map('trim', $_POST);

// Set local variables.
$status = 'Pending';


// ******************************
// CHECK COMMENT INFORMATION.		*
// ******************************

// Check Article ID.
if (empty($_SESSION['articleID'])){
	$errors['articleID'] = '';
}else{
	$articleID = $_SESSION['articleID'];
}

// Check Member ID.
if (empty($_SESSION['memberID'])){
	$errors['memberID'] = '';
}else{
	$memberID = $_SESSION['memberID'];
}

// Check Comment.
if (empty($trimmed['body'])){
	$errors['body'] = 'Please enter a Comment.';
}else{
	$body = $trimmed['body'];
}


// Check for Data-Entry Errors.
if (empty($errors)){
	// Form data clean.

	// Add a new Comment.
	// Build query.
	$q = "INSERT INTO comment(article_id, member_id, body, status, created_on)
			VALUES(?, ?, ?, ?, NOW())";

 

 

Is that sufficient error-checking in case the Session values were lost and so my query doesn't blow up?

 

 

Debbie

 

 

Link to comment
Share on other sites

what are you doing with the errors array if there is an error? If you just want to declare that there was an error the array may not be necessary.

 

<?php
$error = "";
if(!isset($_POST['comment']))
     $error = "No comment was entered. <br />";

if (!isset($_SESSION['articleID']))
     $error .= "There was no article ID. <br />"

if (!isset($_SESSION['memberID']))
     $error .= "There was no member ID. <br />"

if(strlen(error) > 0){
   echo $error;
  //handle the error here.
}else
  //add the comment.
?>

Link to comment
Share on other sites

what are you doing with the errors array if there is an error?

 

If there is a form field that requires data-entry and the user leaves it blank or enters an invalid value, then I echo back the form error (e.g. $errors['body'] = 'Please enter a Comment.';) to the right of the field when the form is reloaded.

 

Follow me?

 

 

Debbie

 

 

Link to comment
Share on other sites

ok well then that seems good as far as checking if those things exist.

 

Say, another issue with this form...

 

I added two buttons at the end of "add_comment.php"...

 

* Return to Article

* Go to Home Page

 

After the user submits a comment, they see a message like this (if successful)...

 

Comment Submitted

 

All submissions must be reviewed by an Administrator.

 

Once approved, your comment will appear below the article:

"Postage Meters can save you money!"

 

Thanks for sharing your thoughts!

 

If I click on either button, each works, but if I then click the browser "Back" button I get an "Undefined Index" error?!

 

How do I prevent things like that from breaking my form?  :-\

 

 

Debbie

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.