Grow your CSS skills. Land your dream job.

Trying to make our text fields in contact form REQUIRED! Frustrated!! PLEASE HELP!

  • # December 24, 2012 at 12:45 pm

    I’ve suggested that before, yes -

    (1) copy+paste your code to [gist.github.com](http://gist.github.com)

    (2) **** out any sensitive/secure info

    (3) click on [Create Public Gist] and share the URL

    I was only asking because your initial posts seem to have originally contained code, but do not now. Your original question seems to be missing as well – unless this thread is a continuation of your earlier [CONTACT FORM not working ... please help!!](http://css-tricks.com/forums/discussion/comment/86155#Comment_86155) thread? Either way, it would be good if you could explain again where you are with your problem.

    # December 26, 2012 at 9:06 pm

    @traq, thank you sir. Here’s the link to the GIST with the PHP and HTML code:

    https://gist.github.com/4384872

    # December 27, 2012 at 12:09 am

    https://gist.github.com/4385576

    BTW, your email **is not** “from” the user. It’s “from” your website. Putting the user’s email address here will get your email dropped or flagged as spam by some mail servers or clients.

    The `From` email address should always belong to a domain that is allowed to send mail from your mail server (e.g., *your* domain). The easiest solution here is a no-reply address. For example, if my website was `example.com`, I’d use `no-reply@example.com`.

    If you want to include the user’s email in the headers, the `Reply-to` field is a good place for it.

    # December 27, 2012 at 8:19 am

    Here is a demo I just worked up with all of it in place. The js/php/captcha http://www.visibilityinherit.com/code/how-to-html-form-with-php-js-captcha-validation-demo.php

    # December 27, 2012 at 4:29 pm

    @Eric **do not** use ereg() – it’s deprecated. Use [preg_match()](http://php.net/preg_match) instead.

    Why include the spam answer in your email?

    # December 27, 2012 at 4:38 pm

    @traq, I’ll check everything once I get home. Thank you very much for taking the time to post sir.

    # December 27, 2012 at 8:55 pm

    @traq, I copied and pasted the code, but kept getting errors. I’ll recheck in a bit when I can upload the code. Thank you for your help.

    # December 27, 2012 at 9:15 pm

    What errors are you getting? If they’re “undefined index” errors, it’s not what I added – it’s because you don’t check if the form was submitted before trying to use it.

    To explain, for example:

    $name = trim(strip_tags($_POST));

    if the form was submitted, then `$_POST` will hold the value that the user entered. However, if the form was *not* submitted, then `$_POST` won’t even *exist*, so you get an error.

    I suspect you had the HTML form and the PHP processing script on separate pages before(?), so you wouldn’t have noticed. It would have still been a problem, though (for example, if the user didn’t fill out one of those fields).

    Try a structure like this:

    < ?php

    /* if $_POST is empty,
    the form was not submitted. */
    if( !empty( $_POST ) ){

    /* form was submitted
    validation code goes here */
    /* don’t forget to set $error
    if something is invalid */

    if( empty( $error ) ){

    /* all fields valid
    code to send email goes here */

    }
    /* here, you could put an else{} block
    for error handling/messages */

    }

    Also, please note I only wrote a check for one field (as an example; you can check the other fields similarly)

    # December 28, 2012 at 12:02 am

    You must do server side validation for true security. The folks above are helping on that. But if you’re using HTML5, you can just also add the required attribute to your inputs

    # December 28, 2012 at 10:21 am

    @traq ok I’ll look into pregmatch. I like to see that the spam question was answered. If you or other don’t then remove it.

    # December 28, 2012 at 1:09 pm

    Not sure where this stands currently. Just trying to catch up but it looks like it is close to complete.

    # December 28, 2012 at 1:45 pm

    @bkbillma, I’m going to test the code that @traq submitted above tonight. @technosailor, I’m not sure if it’s HTML5 or not. I posted the code above in a GIST link.

    # December 28, 2012 at 1:46 pm

    @traq, yes currently the form HTML and the PHP are on separate pages.

    # December 28, 2012 at 1:47 pm

    it is not HTML5 but it doesn’t really matter as the HTML5 form support is severely lacking right now

    # December 28, 2012 at 1:50 pm

    Okay very good, thank you @bkmillma. Yes I will move forward with the code @traq suggested this evening. I will post results. Thank you all.

Viewing 15 posts - 16 through 30 (of 37 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".