adelphik Posted August 29, 2008 Share Posted August 29, 2008 This seems like it should be very simple . . . but it's driving me nuts. In a contact form, if the form data has not been filled out correctly, I'm using PHP to populate the value attribute of the text inputs so the user doesn't have to re-enter. All I'm doing is checking to see if the POST array variable exists: <form method="post" action="send.php"> <label for="yourname">Name:</label><input type="text" name="yourname" value="<?php if ($_POST['yourname']) { echo $_POST['yourname']; } ?>" />*<br /> <label for="email">Email:</label><input type="text" name="email" value="<?php if ($_POST['email']) { echo $_POST['email']; } ?>" />*<br /> <label for="phone">Phone:</label><input type="text" name="phone" value="<?php if ($_POST['phone']) { echo $_POST['phone']; } ?>" /><br /> <label for="message">Message:</label><textarea name="message"></textarea><br /> <input type="submit" value="Submit" name="submit" id="submit"> </form> Everything's fine, except that the first input ("yourname") always comes back with the number 1 as its value attribute (if they entered other form data incorrectly and an error was tripped), no matter what they entered for their name. For example, if they fill out the form with "John" for their name and "john" for their email address and hit submit, it comes back with "1" in the name field and "john" in the email field. Why is that field always coming back as "1"? I'm using PHP version 4.4.8, and idologic.com is my host. Link to comment https://forums.phpfreaks.com/topic/121899-input-field-value-attribute/ Share on other sites More sharing options...
webref.eu Posted August 29, 2008 Share Posted August 29, 2008 Why bother with the if statement at all? I never do. Rgds Link to comment https://forums.phpfreaks.com/topic/121899-input-field-value-attribute/#findComment-628982 Share on other sites More sharing options...
adelphik Posted August 29, 2008 Author Share Posted August 29, 2008 I'm too nice of a guy I guess. Link to comment https://forums.phpfreaks.com/topic/121899-input-field-value-attribute/#findComment-628985 Share on other sites More sharing options...
obsidian Posted August 29, 2008 Share Posted August 29, 2008 I'm not sure why it would be returning 1, but a word of suggestion: why not preset all your variables and then just echo the variables into the value field instead of having to do your if statement for each? Try something like this: <?php $post_fields = array('yourname', 'email', 'phone'); foreach ($post_fields as $f) { // Don't forget to sanitize for output $$f = isset($_POST[$f]) ? htmlentities($_POST[$f], ENT_QUOTES) : ''; } ?> <input type="text" name="yourname" value="<?php echo $yourname; ?>" /> <input type="text" name="email" value="<?php echo $email; ?>" /> <input type="text" name="phone" value="<?php echo $phone; ?>" /> Link to comment https://forums.phpfreaks.com/topic/121899-input-field-value-attribute/#findComment-628992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.