www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: [RESOLVED] PHP Form Validation

  1. #1
    Join Date
    Feb 2012
    Posts
    30

    resolved [RESOLVED] PHP Form Validation

    Hello,

    I am creating a form where users sign up to be a member of my website.

    Currently the validation I'm trying to use is in Javascript, although i want it in PHP.

    Could anyone please help me in creating a validation on the field named 'firstname'.

    my form:
    PHP Code:
    <form name="registerform" id="registerform" method="post" action="signupprocess.php"
    I need the validation to check if the field 'firstname' in form 'registerform' is populated, if not return an error message.

    Many Thanks

  2. #2
    Join Date
    Feb 2012
    Location
    Plymouth, IN
    Posts
    3
    I think this may be able to help. I created my own validation javascript that will check the element for a value and if not it prints a box behind it saying there was an error. It also works for validating emails.

    What you would need to do to make it work is include and id and onblur to your input element that you would like validated. For instance if you have an input named firstname you would change the input to:

    <input type="text" name="firstname" id="firstname" onblur="validate('firstname')"/>

    Link to javascript:
    http://stevenmaloney.info/scripts/validation.js

    If you want to see it in action check out my free quote form. http://stevenmaloney.info/quote.php

    PS. A safe HTML method would be to add required to the end of your element but doesn't always work.

  3. #3
    Join Date
    Feb 2012
    Posts
    30
    also my input field is
    HTML Code:
    <input id="firstname" class="formfield" name="firstname" type="text" size="30"/>

  4. #4
    Join Date
    Feb 2012
    Posts
    19
    I would suggest you look in to PHP's empty() and isset() functions, but make sure that you understand the range of empty() before you use it, to make that's what you want.

    Alternatively you can use something like
    PHP Code:
    if (($firstname == '') || ($lastname '')) { /* return error */ 

  5. #5
    Join Date
    Feb 2012
    Posts
    30
    Thank you, how would i call this from the form like onsubmit?

    Many Thanks

  6. #6
    Join Date
    Feb 2012
    Posts
    19
    Well I guess you could do it in the onsubmit and do a ajax request, or you could let it enter the server side file called in the action attribute. If the validation fails there it'd redirect you back to the form.

  7. #7
    Join Date
    Feb 2012
    Posts
    30
    If i was to use it as server side action would it not reset the form on redirect back, also my form is in a popup box not a page. Hope this makes sense + thank you for the support

  8. #8
    Join Date
    Feb 2012
    Posts
    19
    You can store the data in the session to fill out the form with the correctly entered data. But if it's in a pop up I would maybe suggest that you go with ajax.

    Is it a jQuery popup?

  9. #9
    Join Date
    Feb 2012
    Posts
    30
    Thank you, what would i use for validation could you please show me an example + the popup is all JavaScript that opens a PHP file. below i have showed the on click function, this opens popupsignup.php and requires a .js file which i also have created.

    HTML Code:
    javascript:signup.box.show({url:'signup/popupsignup.php',width:600,height:500,openjs:'opSignUpReg',opacity:30})
    Many Thanks

  10. #10
    Join Date
    Feb 2012
    Posts
    19
    Ahh so it's a new window? Well then you can use the first method I suggested

    HTML Code:
    <form name="signup" method="post" action="">
    Firstname: <input type="text" name="firstname" value="<?php print (!isset($_POST['firstname'])) ? '' : $_POST['firstname']; ?>" /><br />
    Lastname: <input type="text" name="lastname" value="<?php print (!isset($_POST['lastname'])) ? '' : $_POST['lastname']; ?>" /><br />
    <input type="submit" name="submit" value="Sign up" />
    add the following to the top of the same file
    PHP Code:
    <?php
    if (isset($_POST['submit'])) {
    $fistname trim($_POST['fistname']);
    $lastname trim($_POST['lastname']);
      if ((!isset(
    $firstname$lastname)) || ($firstname == '') || ($lastname == '')) {
        print 
    'You must fill out both firstname and username';
      } else {
        
    // Filter all user inputs here and save to the database
      
    }
    }
    ?>
    This was just a quick writeup without testing, so please expect that there might be some errors in it

  11. #11
    Join Date
    Feb 2012
    Posts
    30
    when i try adding a 'value' to the form, it populates the text box, is their another option? Many Thanks for your help

  12. #12
    Join Date
    Feb 2012
    Posts
    19
    what u mean??

  13. #13
    Join Date
    Feb 2012
    Posts
    30
    PHP Code:
    <input style="margin:0 0 5px 0; font-size:16px; color:#09F;" id="firstname" class="formfield" name="firstname" type="text" size="30" value="<?php print (!isset($_POST['firstname'])) ? '' $_POST['firstname']; ?>"/>
    PHP Code:
    <?php print (!isset($_POST['firstname'])) ? '' $_POST['firstname']; ?>
    appears in the text box
    Last edited by thewebportfolio; 02-22-2012 at 07:15 PM.

  14. #14
    Join Date
    Feb 2012
    Posts
    19
    you are sure you're working in a .php file right?

  15. #15
    Join Date
    Feb 2012
    Posts
    30
    100% sure

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles