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…


	<!-- 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…


question01
question02
:
:
question10

Or maybe…


why_start_business
what_not_to_do
what_to_do
:
:
compete_with_corp_america

Debbie

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?! :blush:

Ah yes, sounds fair enough. Maybe call it $sunshine … you know, “array of sunshine”. :smiley:

Seriously, though, that will only apply to the name attribute. You still need unique for/ID pairs for each input.

Ralph,

Some questions on my questions!!!


	<!-- 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…


	<!-- 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

[ot]

“Array of Sunshine”???

OMG!!

For everyone’s benefit, please stick to Web Development, Ralph!! :lol: :lol:

Debbie[/ot]

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.

I have faith in you, Ralph. (Just not your jokes always!) :wink:

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!!

Oh, okay.

So, should I use “question01” or “answer01” for for and id?? :-/

Debbie

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:

<label for="[COLOR="#0000FF"]q1[/COLOR]">Why did you decide to start your own business?</label>
<textarea id="[COLOR="#0000FF"]q1[/COLOR]" name="[COLOR="#FF0000"]a1[/COLOR]"></textarea>

Here are my two solutions, with the first one being more accurate in my mind, although maybe harder for others to follow…


	<!-- 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

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. :slight_smile: )

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

Off Topic:

Well, to quote a great teacher: “Love your enemas.”

Off Topic:

Excuse me while I go wash my hands… :shifty:

Debbie

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/linux/library/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!

But what about my first example…


    <!-- 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

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.


$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.