Jump to content

[SOLVED] Text is not "sticking" to field after $_POST error...


Recommended Posts

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???... ???

 

 

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" />

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.

 

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! :P

 

 

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.