1

I am using a tutorial for an HTML/PHP email form, it does validation and what not and showcases text fields and select fields, it however does not show checkboxes.

Here is the URL to the tutorial http://net.tutsplus.com/tutorials/html-css-techniques/build-a-neat-html5-powered-contact-form/

The text field code looks like such

<input type="text" id="phone" name="phone" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['phone'] : '' ?>" placeholder="555-555-5555" />

The select looks like such

<option value="Select a size" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['size'] == 'Select a size') ? "selected='selected'" : '' ?>>Select a size</option>

What I am trying to figure out how to get the above to be proper syntax when using checkboxes. Here is my attempt

<input type="checkbox" id="color" name="color" value="<?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['red'] : '' ?>" />

It is improper syntax. Mind you I am going to have multiple checkboxes and 1 or all might need to be shown in the recipient email. Can someone please assist that would be awesome.

4
  • 1
    What do you want to achieve? Commented Aug 3, 2013 at 23:33
  • 1
    I am looking to achieve the same results that the text and select fields do as shown in the tutorial link. Being that this form works for select and text fields, I now have a client that requires checkboxes in their form. Commented Aug 3, 2013 at 23:48
  • I mean, what do you expect the ternary operation to 'do'. Commented Aug 3, 2013 at 23:53
  • Being that it seems to be a bit irrelevant for this case, and I don't fully understand what you are talking about even with the provide URL below (I am new with PHP) I am going to leave this ternary code (TRUE/False) out Commented Aug 4, 2013 at 0:06

3 Answers 3

2

If I am correct you are trying to set the checked state. You are doing this in the value attribute, which is incorrect. Assuming this is the checkbox for the color red something like this should be the syntax:

<input type="checkbox" 
       id="color" 
       name="color" 
       value="red"
       <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['red']) 
                  ? 'checked="checked"'
                  : ''; ?> 
       />

An example of the ternary operation:

// Like asking php a question
(TRUE) ? 'this is true' : 'this is false';
--> 'this is true'

Broken down into a regular if/else statement:

if(TRUE)
{
    echo 'this is true';
}
else
{
    echo 'this is false';
}
--> 'this is true'
Sign up to request clarification or add additional context in comments.

7 Comments

This will still get the form to work? i.e. in the tutorial it shows that the form is supposed to validate and show a green check message or a red X message see tutorial above for reference. Thanks for taking a look
Amazing I don't understand what this does The ternary operation in more detail: // Like asking php a question (TRUE) ? 'this is true' : 'this is false'; --> 'this is true' Broken down into a regular if/else statement: if(TRUE) { echo 'this is true'; } else { echo 'this is false'; }
It does nothing, just an example of a ternary operation as you did not seem to fully comprehend what it is. And no, this should not influence any existing functionality.
So it doesn't need to be included?
Also if I have multiple check boxes with a name/id of color and some selects more than one option, will both options show in the recipient email?
|
0

Your ternary operator syntax is not correct for the checkboxes:

($sr && !$cf['form_ok'] && $cf['posted_form_data']['red'] : ''

You're missing the ? part and never close the parentheses. This will result in a syntax error.

Comments

0

These lines had problems, as well....

<input type="text" id="phone" name="phone" value="<?php echo ($sr && !$cf['form_ok'] ? $cf['posted_form_data']['phone'] : '');?>" placeholder="555-555-5555" />
<option value="Select a size" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['size'] == 'Select a size' ? "selected='selected'" : '');?>>Select a size</option>

1 Comment

What would the corrections be? Note in the tutorial this is to help advise the person filling the form out that the form is either valid or invalid, will your below corrections as well as the ones for these two lines still make this work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.