SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    What to call Form Fields?

    I have a page on my website where Users can answer a series of open-ended questions and share their thoughts on things.

    Silly question, but what do I call each Field on my Form?!

    Questions would be things like...
    1.) Why did you decide to start your own business?
    2.) What advice would you share with others on what NOT to do?
    3.) What advice would you share with others on what TO do?
    :
    :
    10.) How do you compete against Corporate America?

    Normally my controls would look like this...
    HTML Code:
    	<!-- First Name -->
    	<label for="firstName"><b>*</b>First Name:</label>
    	<input id="firstName" name="firstName" type="text" maxlength="30"
    			value="<?php if(isset($firstName)){echo htmlentities($firstName, ENT_QUOTES);} ?>" /><!-- Sticky Field -->
    	<?php
    		if (!empty($errors['firstName'])){
    			echo '<span class="error">' . $errors['firstName'] . '</span>';
    		}
    	?>

    But what in the world do I call my 10 Questions?!

    Something generic like this...
    Code:
    question01
    question02
    :
    :
    question10

    Or maybe...
    Code:
    why_start_business
    what_not_to_do
    what_to_do
    :
    :
    compete_with_corp_america

    Debbie

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    This is for your reference only, so call them whatever you like. Me, I'd go for "question1", "question2" etc.

    (I presume you're asking about the for and name attributes and not the labels.)
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    This is for your reference only, so call them whatever you like. Me, I'd go for "question1", "question2" etc.

    (I presume you're asking about the for and name attributes and not the labels.)
    I am referring to anywhere that I had "first_name" in the example above...

    for, id, name, $errors[],...


    Debbie

    P.S. I just realized that in another thread someone recommended using an array, so that may answer my question right there?!

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    someone recommended using an array
    Ah yes, sounds fair enough. Maybe call it $sunshine[] ... you know, "array of sunshine".

    Seriously, though, that will only apply to the name attribute. You still need unique for/ID pairs for each input.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ralph,

    Some questions on my questions!!!

    HTML Code:
    	<!-- Question 01 -->
    	<label for="question01">1.) Why did you decide to start your own business?</label>
    	<textarea id="question01" name="question01" cols="40" rows="8"><?php if (isset($question01)){echo htmlentities($question01, ENT_QUOTES);} ?></textarea>
    	<?php
    		if (!empty($errors['question01'])){
    			echo '<span class="error">' . $errors['question01'] . '</span>';
    		}
    	?>
    	<br />

    Questions:

    1.) Can I change my naming convention to more accurately reflect what is what like this...

    HTML Code:
    	<!-- Question 01 -->
    	<label for="question01">1.) Why did you decide to start your own business?</label>
    	<textarea id="answer01" name="answer01" cols="40" rows="8"><?php if (isset($answer01)){echo htmlentities($answer01, ENT_QUOTES);} ?></textarea>
    	<?php
    		if (!empty($errors['answer01'])){
    			echo '<span class="error">' . $errors['answer01'] . '</span>';
    		}
    	?>
    	<br />

    2.) What is the code in my <?php if (isset($answer01)){ "keying" off of? Is it the "for" in the label, or the "id" in the textarea??


    Debbie

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by ralph.m View Post
    Ah yes, sounds fair enough. Maybe call it $sunshine[] ... you know, "array of sunshine".
    "Array of Sunshine"????

    OMG!!

    For everyone's benefit, please stick to Web Development, Ralph!!


    Debbie

  7. #7
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    This thread will quickly go over my head, I suspect, but I'll do my best. Firstly: the for="" value in the label must equal the id="" value in the input/textarea, as that's how they know which relates to which. So it must be

    for="question01" / id="question01"

    For example, this pair helps a blind user to know which label goes with which input.

    What is the code in my <?php if (isset($answer01)){ "keying" off of? Is it the "for" in the label, or the "id" in the textarea??
    It relies on the name="" attribute. The for and id are only for the HTML side of things ... to help users associate a label with the input it applies to.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    This thread will quickly go over my head, I suspect, but I'll do my best.
    I have faith in you, Ralph. (Just not your jokes always!)


    Firstly: the for="" value in the label must equal the id="" value in the input/textarea, as that's how they know which relates to which. So it must be

    for="question01" / id="question01"

    For example, this pair helps a blind user to know which label goes with which input.
    Oh, right, I knew that!!


    Quote Originally Posted by doubledee
    What is the code in my <?php if (isset($answer01)){ "keying" off of? Is it the "for" in the label, or the "id" in the textarea?

    It relies on the name="" attribute. The for and id are only for the HTML side of things ... to help users associate a label with the input it applies to.
    Oh, okay.

    So, should I use "question01" or "answer01" for for and id??


    Debbie

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    So, should I use "question01" or "answer01" for for and id??
    It doesn't matter. But as far as the HTML is concerned, I guess it's a question rather than an answer. The HTML presents the question, which is contained in the label, and it's up to the user to provide an answer. So perhaps:

    Code:
    <label for="q1">Why did you decide to start your own business?</label>
    <textarea id="q1" name="a1"></textarea>
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  10. #10
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here are my two solutions, with the first one being more accurate in my mind, although maybe harder for others to follow...

    PHP Code:
        <!-- Question 1 -->
        <label for="question1">1.) Why did you decide to start your own business?</label>
        <textarea id="question1" name="answer[1]" cols="40" rows="8"><?php if (isset($answer[1])){echo htmlentities($answer[1], ENT_QUOTES);} ?></textarea>
        <?php
            
    if (!empty($errors['answer1'])){
                echo 
    '<span class="error">' $errors['answer1'] . '</span>';
            }
        
    ?>
        <br />

        <!-- Thought 1 -->
        <label for="thought1">1.) Why did you decide to start your own business?</label>
        <textarea id="thought1" name="thought[1]" cols="40" rows="8"><?php if (isset($thought[1])){echo htmlentities($thought[1], ENT_QUOTES);} ?></textarea>
        <?php
            
    if (!empty($errors['thought1'])){
                echo 
    '<span class="error">' $errors['thought1'] . '</span>';
            }
        
    ?>
        <br />


    Debbie

  11. #11
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    I think this is an issue that will only be important to you. You can call it whatever you like. (Not a single one of your site visitors is going to check your HTML to see what for/ID values you used. )

    Remember that, whether you use answer[] or thought[], the first item will be answer[0] etc., not answer[1].
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  12. #12
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    I think this is an issue that will only be important to you. You can call it whatever you like. (Not a single one of your site visitors is going to check your HTML to see what for/ID values you used. )

    Remember that, whether you use answer[] or thought[], the first item will be answer[0] etc., not answer[1].
    I'm anal-retentive!! What else can I say?!

    Thanks for helping again, Ralph.


    Debbie

  13. #13
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Off Topic:

    Quote Originally Posted by DoubleDee View Post
    I'm anal-retentive!! What else can I say?!
    Well, to quote a great teacher: "Love your enemas."
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  14. #14
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by ralph.m View Post
    Well, to quote a great teacher: "Love your enemas."
    Excuse me while I go wash my hands...


    Debbie


  15. #15
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    I'm a fan of verbose meaningful names - I've been known to rip people new holes for idiotic nonsense like "q01, q02" etc... It's like when you open up something made by a WYSIWYG retard and all their classes are gibberish like "style03, style43" etc...

    I see nothing wrong with your second example -- it says what they are. That's the most important thing in making clear code, is clearly saying what things are.

    Though I prefer camelBacks to underscore_separation... either is acceptable, clean, easy to read and clear as crystal.

    When it comes to naming variables, classes, ID's, etc, I always point people at this article on the IBM Linux developer site:
    http://www.ibm.com/developerworks/li.../l-clear-code/

    While it's focus is on C, the lessons and ideas presented are useful to programmers of all skill levels and languages.

    From writing meaningful comments to useful naming conventions, it's all about code clarity. Programming is already difficult, don't make it harder for yourself and others by making things uselessly vague and cryptic!

  16. #16
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,761
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    I see nothing wrong with your second example -- it says what they are. That's the most important thing in making clear code, is clearly saying what things are.
    But what about my first example...
    Code:
        <!-- Question 1 -->
        <label for="question1">1.) Why did you decide to start your own business?</label>
        <textarea id="question1" name="answer[1]" cols="40" rows="8"><?php if (isset($answer[1])){echo htmlentities($answer[1], ENT_QUOTES);} ?></textarea>
        <?php
            if (!empty($errors['answer1'])){
                echo '<span class="error">' . $errors['answer1'] . '</span>';
            }
        ?>
        <br />

    I like that one better because it more accurately names what is the "Question" and what is the "Answer".


    Debbie

  17. #17
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    I would call that bad just for using the id "question1" on the answer. The ID should say what it is. the LABEL is the question, not the input.

    If you have a whole bunch of them automated, you may have to resort to numbers just to keep the back-end code simple; in that case it's not entirely about clearly naming for the purpose of maintaining the HTML, it's about building sane PHP.

    though at that point, you'd be building the ID with PHP, which you are not.

    Oh, and I'd suggest htmlspecialchars instead of htmlentities there -- no sense wasting bandwidth on entities you don't need to escape.

    Besides, if you're going to have a bunch of them in a row, all taking the same error checks and using the same markup, they'd be in an array letting you quite easily set a meaningful key.

    Code:
    $questionnaire=array(
    	'whyStartBusiness' => '1) Why did you decide to start your own business?',
    	'whatNotToDo'      => '2) What do you think should not be done when starting a business?',
    	'whatToDo'         => '3) What advice do you have for starting a business?'
    );
    
    foreach ($questionnaire as $key => $question) {
    	echo '
    		<label for="',$key,'">',$question,'</label>
    		<textarea
    			id="',$key,'"
    			name="answer[',$key,']"
    			cols="40" rows="8"
    		>',(
    			isset($_POST['answer'][$key]) ? htmlspecialchars($_POST['answer'][$key]) : ''
    		),'</textarea>
    		',(
    			empty($errors[$key]) ? '' : '<span class="error">'.$errors[$key].'</span>'
    		),'<br />';
    }
    Your validation checks could work against them the same way. It also would mean having a list of keys to check against, so people can't just willy-nilly fake their own keys to trick your form into doing something it shouldn't. (a danger using array-type NAME attributes).

    Oh, I assumed your $answer array was pulled from $_POST for resending the form if they enter it wrong -- at which point why the extra array for the same values? Also, notice I'd swing an axe at the extra <?php ?> for nothing... that's just sloppy coding IMHO.


Bookmarks

Posting Permissions

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