Reputation: 2752
I've probably not explained what I"m trying to do in the title very well, so here goes:
I've got a HTML entry form, in a .php
file. That entry form when submitted files a POST
operation. That POST operation has code to check field input, similar to this:
<?php ... if ($_POST["submitted"] == 1) { //"submitted" is a hidden field with value '1' in the form $isvalid = 1; // Inits to 1, changes to 0 if something fails checks. $field1 = $_POST["field1"]; $field2 = $_POST["field2"]; ... /* Checks for validation on each field go here, but are not relevant to this question here. */ } if ($isvalid == 1) { // Send email } else { ?> <!-- Print form, and error msg. --> ... <input name="field1" type="text" id="field1" size="32" class="stylingclass"> ...
So the above is example code, but here's the real question: How can I get any of the text input fields in my form, when submitted and an error occurred, to automatically contain the previous values that were entered, so that the user can correct the entries accordingly, based on whatever constraints I've set?
Upvotes: 0
Views: 169
Reputation: 18843
You can set the value parameter using a basic ternary operator:
<input name="field1" type="text" id="field1" value="<?php echo isset($_POST["field1"]) ? $_POST["field1"] : ''; ?>" size="32" class="stylingclass">
Note that the ternary operator is used here to prevent an error if there is no index of the specified field in the $_POST array
Upvotes: 2
Reputation: 324750
Just add value="<?=$_POST['field1']?>"
(assuming PHP 5.4, use <?php echo $_POST['field1'] ?>
otherwise)
Upvotes: 1