SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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,200
    Mentioned
    456 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.)

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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,200
    Mentioned
    456 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.

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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

  7. #7
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 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.

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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
    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!

  10. #10
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    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

  11. #11
    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
  •