Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    133
    Thanks
    2
    Thanked 0 Times in 0 Posts

    validating a form, can it have 2 values?

    Hey guys, I'm just learning PHP, and I'm trying to validate a form.
    The problem I'm having, is with value=

    Here is a snipit of the form code as an example:
    Code:
    <label>Full Name
    <span class="small">First Name Last Name</span>
    </label>
    <input onfocus="this.value=''" type="text" value="John Smith" name="name" id="name" />
    I have the value set as an example of the field, and it disappears when the user clicks the field.
    But the validation tutorial I'm following is telling me to set the value as
    Code:
    value="<?=$varname;?>
    I believe that this prevents the individual from having to re-enter their information if they forgot a field, thus triggering the validation/error message and reloading the page.

    How can I have the 'example' of the field and the var so that people don't have to re-enter their information when the form is validated?

    Please give me an example of the correct code, as I'm still learning

    Thanks!
    Last edited by njfail; 01-27-2013 at 02:08 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    How can I have the 'example' of the field and the var so that people don't have to re-enter their information when the form is validated?
    I don't understand that sentence.

    Rather than using onfocus() and relying of JavaScript - it's annoying anyway to have the value removed each time (suppose I just want to correct the spelling?) - use HTML5 placeholder:

    PHP Code:
    <input type="text" value="<?php echo $varname?>" name="name" id="name" placeholder="John Smith" />
    If you are using the HTML5 doctype then you don't need ' /' at the end either.

    It is also advisable to always use the full php opening tag <?php and echo the value.

    You'll notice that if you delete the value and click away from the input the placeholder text will show. However, placeholder text should say something useful such as "Name is required" or "Minimum 5 characters" rather than an actual name, as this is mis-leading.

    BTW I think name is a very poor name for the name. Use a little imagination; even 'fullname' is better.
    Last edited by AndrewGSW; 01-27-2013 at 03:16 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,607
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Code:
    <?php 
    $fullname = '';
    if (isset($_POST['fullname'])) $fullname = validateFullName($_POST['fullname']);
    ?>
    
    <label for="fullname">Full Name
    <span class="small">First Name Last Name</span>
    </label>
    <input type="text" value="<?php echo $fullname ?>" name="fullname" id="fullname"/>
    If you are serving the (X)HTML5 as XHTML then you need the closing / but if you are serving it as HTML then you don't.
    Last edited by felgall; 01-27-2013 at 03:53 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    133
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the advice!
    I was unaware of the placeholder attribute

    Can I ask why name is a bad name? I understand it is simple/common but is there a reason that is bad?

    I'm not very familiar with doctypes. My knowledge of html/css/php is mainly from following tutorials and taking advice from random pages online. People have told me to just use this, so its what I use
    Code:
    <!DOCTYPE html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •