1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HTML form. Using both javascript and php to validate data.

Discussion in 'HTML & Website Design' started by pictureboarduk, Jan 27, 2010.

  1. #1
    Hi,

    I am using a HTML form which uses PHP to validate the user submitted data.

    I got it from w3schools HERE.

    
    <html>
    <head>
    	[B]<script src="contactus.js"></script>[/B]
    </head>
    
    <body>
    <?php
    function spamcheck($field)
      {
      //filter_var() sanitizes the e-mail
      //address using FILTER_SANITIZE_EMAIL
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
    
      //filter_var() validates the e-mail
      //address using FILTER_VALIDATE_EMAIL
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    
    if (isset($_REQUEST['email']))
      {//if "email" is filled out, proceed
    
      //check if the email address is invalid
      $mailcheck = spamcheck($_REQUEST['email']);
      if ($mailcheck==FALSE)
        {
        echo "Invalid input";
        }
      else
        {//send email
        $email = $_REQUEST['email'] ;
        $subject = $_REQUEST['subject'] ;
        $message = $_REQUEST['message'] ;
        mail("someone@example.com", "Subject: $subject",
        $message, "From: $email" );
        echo "Thank you for using our mail form";
        }
      }
    else
      {//if "email" is not filled out, display the form
      echo "[B]<form method='post' action='mailform.php'>[/B]
      Email: <input name='email' type='text' /><br />
      Subject: <input name='subject' type='text' /><br />
      Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
      <input type='submit' />
      </form>";
      }
    ?>
    
    </body>
    </html> 
    
    Code (markup):
    As you can see, the form action is 'mailform.php'.

    I want to use Javascript validation as well though. So that at alert box pops up if an email address is not valid.

    I have the javascript validation in the seperate file "contactus.js", but I'm unsure how I should write the forms tags.

    Something like this:

    How can I first perform the javascript validation then if/when it is completed, then execute the PHP test?

    Thanks very much, I always +ve rep for help.

    Thanks!
     
    pictureboarduk, Jan 27, 2010 IP
  2. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First, use Good Form tags instead of 99% of what you see out there (invalid forms).

    And... if you want to use JS for validation, you don't want to wait until they hit submit... cause your php is kicking in there anyway.

    You know some of your users might not have JS enabled (like with many types of mobile phones) so first, make sure your PHP can handle anything.

    Second, you might want to find a JS that validates after someone has typed in a value and then has gone on to the next form field. It doesn't have to be an alert (in fact, I think you won't want that as they are super annoying) but it can be some bit of text that appears after the field stating in red letters "YER DOIN IT RONG FOOL" or whatever.

    Third, er this should have been first, make sure you have a good Doctype at the top of your page. No doctype, who knows what some particular browser will do with your JS (or CSS for that matter).

    If you go looking for JS validators, try to find ones that use onfocus and onblur to get the ones that show an error right away. Then test it yourself: can you fill in your form without hassle? Even if you use only the keyboard?

    So, the script should be running before users start filling in the form, not at submit().

    Be aware of email validators. Some are too strict and don't allow perfectly valid email addresses. Most people try to stick to the types of email their own email client will handle (so if yours can't handle, like, cyrillic letters or something, then it's prolly ok to call those errors even if it's really a valid email address). On the other hand, don't use one that does nothing more than checks for
    "some string" @ "some string". "something"

    it should be a bit more strict than that.

    Then you can have a check on the submit: if they hit submit and there's still an error message, do not allow the form to send to the PHP script. Otherwise, let the submit do what it normally does.

    Don't use JS to completely replace the submit button, because you want those without JS to be able to send.

    You could have the script check for error messages, and return the submit to "false" which disables it until there are not error messages.

    If you do this, though, put some message at the bottom of the form saying "can't get the form to work? Contact us at this phone number" or whatever. Someone, somewhere, will break your form and you need to give them another way to get ahold of you.
     
    Stomme poes, Jan 28, 2010 IP
    pictureboarduk likes this.
  3. pictureboarduk

    pictureboarduk Well-Known Member

    Messages:
    551
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thank you Stomme for such great advice.

    I'll going to try and use ajax on the validations, what you told me seems to be asking for it.

    Thanks very much! :)
     
    pictureboarduk, Jan 29, 2010 IP
  4. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, you don't need AJAX, just plain javascript... no XML involved. : )
     
    Stomme poes, Jan 31, 2010 IP
  5. mininova

    mininova Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I use Adobe Dreamweaver CS4 and absolutely love it. No need of any technical knowledge and you will able to develop nice looking websites. I even use it to design websites for small business owners who pays me between 2 and 4K per site.

    Adobe Dreamweaver CS4 is very user friendly, you can add video, audio, optin forms, rss feeds etc with a touch of a button and the cost for Adobe Dreamweaver CS4 is $1200.00,But you can download it from here fro free: tinyurl(dot)com/yctz9uf
    Enjoy website making

    It's worth checking out.
     
    mininova, Jan 31, 2010 IP