CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Easier Way?  (Read 440 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Easier Way?
« on: 18 Dec 2020, 03:27:49 am »
Code: [Select]
<section>

<h2>Contact Us - Form</h2>

<form method="post" id="form_Contact">

<input type="hidden" name="MODE" value="01">
<input type="hidden" name="hash_Contact" value="', hash_Create('Contact'), '">

<div>
    <div>
        <labeL for="firstname">First name</labeL>
    </div>
    <div>
        <input id="firstname" type="text" name="firstname" value="', ( ! empty($_POST['firstname']) ) ? $_POST['firstname'] : "", '" placeholder="John">
    </div>
</div>

<div>
    <div>
        <label for="surname">Surname</label>
    </div>
    <div>
        <input id="surname" type="text" name="surname" value="', ( ! empty($_POST['surname']) ) ? $_POST['surname'] : "", '" placeholder="Doe">
    </div>
</div>

<div>
    <div>
        <label for="email">Email</label>
    </div>
    <div>
        <input id="email" type="email" name="email" value="', ( ! empty($_POST['email']) ) ? $_POST['email'] : "", '" placeholder="john@example.co.uk">
    </div>
</div>

<div>
    <div>
        <labeL for="email1">Confirm email</labeL>
    </div>
    <div>
        <input id="email1" type="email" name="email1" value="', ( ! empty($_POST['email1']) ) ? $_POST['email1'] : "", '" placeholder="john@example.co.uk">
    </div>
</div>

<div>     
    <div>
        <label for="message">Message</label>
    </div>
    <div>
        <textarea id="message" name="message" placeholder="My Message...">', ( ! empty($_POST['message']) ) ? $_POST['message'] : "", '</textarea>
    </div>
</div>
 
<div>
    <label for="privacy">Privacy</label>
        <input type="checkbox" name="privacy" value="1">
</div>
 
<div>
    <button type="submit">Send Message</button>
</div>
 
</form>

</section>

I am currently flexing the first DIV and then on each child, I am using flex: 1 0 50%; - but could I just flex the label the input? as would massively simplify and reduce the code which is what we want?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Easier Way?
« Reply #1 on: 18 Dec 2020, 02:09:01 pm »
I'd have to see how you're styling this, but as a guess I'd say 90%+ of your DIV belong in the trash, and since structurally form and section are the same "depth", there's no reason for SECTION either.

Much less where's your FIELDSET?  What's with the pointless placeholder redundant to the LABEL? (you used it right on the e-mails, wrong on the textarea)

Also don't put hidden as direct children of the FORM, some UA's have been known to choke on that in the past.

Guessing wildly, if you were to use the LABEL as the wrappers and SPAN for the label formatting, you could ditch the ID and FOR relationships simplifying and reducing the overall code. Likewise use fieldset not DIV for the major styling groups that have input the user can modify the values of, put the submit and hidden in the same DIV at the bottom, and move the H2 inside the form to ditch the "section for nothing" since FORM is a type of section and H2 marks the start of a major subsection of the page...

Code: [Select]

<form method="post" id="contact">

<h2>Contact Us - Form</h2>

<fieldset class="labelWrapSpanInput">

<label>
<span>First Name</span><br>
<input type="text" name="firstname"', (
empty($_POST['firstname']) ?
'' :
' value="' . htmlspecialchars($_POST['firstname']) . '"'
), '><br>
</label><label>
<span>Last Name</span><br>
<input type="text" name="surname"', (
empty($_POST['surname']) ?
'' :
' value="' . htmlspecialchars($_POST['lastname']) . '"'
), '><br>
</label><label>
<span>E-Mail</span><br>
<input type="email" name="email" placeholder="john@example.co.uk"', (
empty($_POST['email']) ?
'' :
' value="' . htmlspecialchars($_POST['email']) . '"'
), '><br>
</label><label>
<span>Confirm email</span><br>
<input type="email" name="email1" placeholder="john@example.co.uk"', (
empty($_POST['email1']) ?
'' :
' value="' . htmlspecialchars($_POST['email1']) . '"'
), '><br>
</label><label>
<span>Message</span><br>
<textarea name="message">', (
empty($_POST['message']) ?
'' :
htmlspecialchars($_POST['message'])
), '</textarea><br>
</label>
</fieldset>

<fieldset class="labelWrapCheckOrRadio">
<label>
<input type="checkbox" name="privacy" value="1">
Privacy
</label>
</fieldset>

<div class="submitsAndHiddens">
<button type="submit">Send Message</button>
<input type="hidden" name="MODE" value="01">
<input type="hidden" name="hash_Contact" value="', hash_Create('Contact'), '">
<!-- .submitsAndHiddens --></div>

</form>

That should be MORE than enough hooks to style this however you plan to, whilst still providing the proper semantics.

NOTE, since it's blindly plugging in $_POST values, htmlspecialchars those bad boys. Sadly that brings us back up to almost your original size, but it's a lot safer this way.

Oh, and notice I moved the value="" into the condition, don't output empty values when that's the default for them! For empty form sends that will save you a good chunk of bandwidth.

To that end, leveraging a template style function for this could speed up and reduce the code size a good bit. Look how uniform your tags are!

Code: [Select]
function template_labelSpanInput(
$label,
$name,
$type = 'text',
$placeHolder = false,
$required = true
) {

$extraAttr = (
' name="' . $name . '"' .
($required ? ' required' : '') .
($placeHolder ? ' placeholder="' . $placeHolder . '"' : '')
);
$value = empty($_POST[$name]) ? '' : htmlspecialchars($_POST[$name]);

echo '
<label>
<span>', $label, '</span>';

switch ($type) {
case 'textarea':
echo '
<textarea', $extraAttr, '>', $value, '</textarea>';
break;
default:
echo '
<input type="', $type, '"', $extraAttr, (
$value ? ' value="' . $value . '"' : ''
), '>';
}

echo '<br>
</label>';

} // template_labelSpanInput

then your output would be:

Code: [Select]
echo '
<form method="post" id="contact">

<h2>Contact Us - Form</h2>

<fieldset class="labelSpanInput">';

labelSpanInput('First Name', 'firstname');
labelSpanInput('Last Name', 'surname');
labelSpanInput('E-mail', 'email', 'email', 'john@example.co.uk');
labelSpanInput('Confirm E-mail', 'email1', 'email', 'john@example.co.uk');
labelSpanInput('Message', 'message', 'textarea');

echo '
</fieldset>

<fieldset class="labelCheckOrRadio">
<label>
<input type="checkbox" name="privacy" value="1">
Privacy
</label>
</fieldset>

<div class="submitsAndHiddens">
<button type="submit">Send Message</button>
<input type="hidden" name="MODE" value="01">
<input type="hidden" name="hash_Contact" value="', hash_Create('Contact'), '">
<!-- .submitsAndHiddens --></div>

</form>';

Generally when I have sections like that, I like to make template_ functions to automate this stuff. For example you could just have something like:

Code: [Select]
$contactForm = [
'action' => '/contact',
'method' => 'get',
'submitText' => 'submit',
'fieldsets' => [
[
'class' => 'labelSpanInput',
'fields' => [
'firstname' => [
'label' => 'First Name'
],
'lastname' => [
'label' => 'Last Name'
],
'email' => [
'label' => 'E-Mail',
'placeholder' => 'john@example.co.uk',
'type' => 'email'
],
'email1' => [
'compareTo' => 'email',
'label' => 'Confirm E-Mail',
'placeholder' => 'john@example.co.uk',
'type' => 'email'
],
'message' => [
'label' => 'Message',
'type' => 'textarea'
]
]
], [
'class' => 'labelCheckOrRadio',
'fields' => [
'privacy' => [
'label' => 'Privacy',
'value' => '1'
]
]
]
],
'hiddens' => [
'MODE' => '01',
'hash_contact' => hash_Create('contact')
]
];

then have the template plug those values from the array into the form automagically. The nice part about this approach is the above array of information could also store validation information -- like that "compareTo" I put on "email1", so that it too can just loop through the fields checking things. That way you don't have to hard code each and every blasted check. (which sucks).

We have programming languages for a reason, and one of those reasons is DRY.
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Easier Way?
« Reply #2 on: 19 Dec 2020, 09:45:56 am »
I will look at that property when I get near the PC again - CCTV failed at (the) work(shop) and I have had to take my desk apart to get to the CCTV safe and power supply - don’t ask, but it seemed a good idea at the time!!

I wasn’t sure how to “fieldset” that form up... 

Thank you for advice I appreciate it - are you starting to hate my posts/topics yet?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement