doubledee Posted August 31, 2011 Share Posted August 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/ Share on other sites More sharing options...
thewooleymammoth Posted August 31, 2011 Share Posted August 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263786 Share on other sites More sharing options...
doubledee Posted August 31, 2011 Author Share Posted August 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263789 Share on other sites More sharing options...
thewooleymammoth Posted August 31, 2011 Share Posted August 31, 2011 It seems ok. It kind of depends on what your doing exactly. Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263790 Share on other sites More sharing options...
doubledee Posted August 31, 2011 Author Share Posted August 31, 2011 It seems ok. It kind of depends on what your doing exactly. What do you mean? What extra details can I provide to help you help me out? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263791 Share on other sites More sharing options...
thewooleymammoth Posted August 31, 2011 Share Posted August 31, 2011 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263795 Share on other sites More sharing options...
doubledee Posted August 31, 2011 Author Share Posted August 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263796 Share on other sites More sharing options...
thewooleymammoth Posted August 31, 2011 Share Posted August 31, 2011 ok well then that seems good as far as checking if those things exist. Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263798 Share on other sites More sharing options...
doubledee Posted August 31, 2011 Author Share Posted August 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263800 Share on other sites More sharing options...
thewooleymammoth Posted August 31, 2011 Share Posted August 31, 2011 I would need to see the page/code. Quote Link to comment https://forums.phpfreaks.com/topic/246080-handling-data-not-gathered-in-form/#findComment-1263944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.