ShootingBlanks Posted September 14, 2007 Share Posted September 14, 2007 Hello. I have a form with a bunch of fields in it for people to input information into various columns of a database. I put an error-checking mechanism in there to make sure all the fields have been filled in: if (isset($_POST['insert'])) { if (empty($_POST['title_full']) || empty($_POST['title_short']) || empty($_POST['what_is']) || empty($_POST['why_use']) || empty($_POST['not_followed']) || empty($_POST['doc_content']) || !isset($_POST['categories'])) { $error = 'You must fill in all fields and select at least one category before submitting a new document'; } } Then, if $error exists, I halt the insertion of the document, and under the <h1>heading of the document, I have the following code: <?php if (isset($error)) { ?> <p class="warning"><?php echo $error; ?></p> <?php } ?> That all works just fine, but my problem is that if there is an error, when the page refreshes itself with the error message above, all of the fields clear out. I need it so that if a user fills out some (but not all) of the fields, then the ones that they did fill out will still be filled in when the page refreshes. I tried what I THOUGHT would work, but it doesn't: (an example of one of the input fields): <input value="<?php if (isset($_POST['title_short'])) { echo $_POST['title_short'];} ?>" name="title_short" class="title" maxlength="40" /> Any ideas???... ??? Quote Link to comment https://forums.phpfreaks.com/topic/69363-solved-text-is-not-sticking-to-field-after-_post-error/ Share on other sites More sharing options...
zetahoff Posted September 14, 2007 Share Posted September 14, 2007 Looks like that should work. Might want to double check and make sure that your error-checking script is still posting all the data to the submitted page. Quote Link to comment https://forums.phpfreaks.com/topic/69363-solved-text-is-not-sticking-to-field-after-_post-error/#findComment-348513 Share on other sites More sharing options...
Psycho Posted September 14, 2007 Share Posted September 14, 2007 Looks fine to me. Have you checked the HTML output to see if the value is actually there but just not formatted in the HTML properly. Also, instead of including the isset() check inline, create a variable beforehand and just show the variable. It will make the code cleaner. You could also do something similar with the error, just display it regardless. If it is blank, the user wont see any error anyway. There is a lot of going in and out of HTML, I personally try to minimize that for easy debugging. <?php $error = ''; if (isset($_POST['insert'])) { if (empty($_POST['title_full']) || empty($_POST['title_short']) || empty($_POST['what_is']) || empty($_POST['why_use']) || empty($_POST['not_followed']) || empty($_POST['doc_content']) || !isset($_POST['categories'])) { $error = 'You must fill in all fields and select at least one category before submitting a new document'; } } ?> <?php //If empty will be blank on the page echo "<p class=\"warning\">echo $error</p>"; ?> <?php //define variables for all field values $title_short = (isset($_POST['title_short'])?$_POST['title_short']:''; ?> <input value="<?php echo $_POST['title_short']; ?>" name="title_short" class="title" maxlength="40" /> Quote Link to comment https://forums.phpfreaks.com/topic/69363-solved-text-is-not-sticking-to-field-after-_post-error/#findComment-348517 Share on other sites More sharing options...
ShootingBlanks Posted September 14, 2007 Author Share Posted September 14, 2007 Okay - I did this: (under the <h1>heading): <?php $title_short = isset($_POST['title_short'])?$_POST['title_short']:'no error set'; if (isset($error)) { ?> <p class="warning"><?php echo $error; ?></p> <?php $title_short = isset($_POST['title_short'])?$_POST['title_short']:'error set'; } ?> Then for the input tag, I did this: <input value="<?php echo $title_short; ?>" name="title_short" class="title" maxlength="40" /> Now, I kept my original code that I posted in my first post to create the $error variable. That code (as far as I can tell) creates the variable only if any one of the fields is empty. So, now what happens is that: As soon as you go on the page for the first time "no error set" is in the field (which makes sense) If you leave ANY field blank, (and even if you write over the "no error set" text), You get the $error message AND "error set" always comes back into the text field So, it is definitely understanding that some $_POST variables (like $_POST['title_short'], for example) are empty (which is causing the $error message to be displayed in the first place). HOWEVER...it also can tell when everything is filled in, because if I fill in everything, the form will properly submit to the database... ...But sometime between the code that checks for any empty fields (which i posted in my original post, and which is way above the DOCTYPE) and the code that is listed under the <h1> heading (to display the error) the page somehow clears out the $_POST array again. I don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/69363-solved-text-is-not-sticking-to-field-after-_post-error/#findComment-348535 Share on other sites More sharing options...
ShootingBlanks Posted September 14, 2007 Author Share Posted September 14, 2007 Nevermind - I figured it out. I'm a dumbass. I had: $_POST = array(); OUTSIDE of the submitting code brackets instead of within it. So, the $_POST array was getting cleared even if the form WASN'T being submitted... D'oh! Quote Link to comment https://forums.phpfreaks.com/topic/69363-solved-text-is-not-sticking-to-field-after-_post-error/#findComment-348547 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.