Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Having problems with my PHP code -Beginner-

    hey guys started learning php earlier this year but im not all that great tbh, i've started my uni assignment and it states i need to validate firstname and givenName, must use alpha characters no more then 35 characters and can use hyphens. I've tinkered with my code for hours on end today but for some reason when i put the PHP code in the top of my form i hit submit, all my javascript alerts popup but no message for my PHP code?

    Code:
    <?php
    	$errMsg = '';
    	
    	if(isset($_POST['submit']))
    {
    $firstName = $_POST['fname'];
    $givenName = $_POST ['gname'];
    
    if(!preg_match('/^[-a-zA-Z]+$/)', $firstName))
    $errMsg .= '<p>Invalid first name!</p>';
    if(!preg_match('/^[-a-zA-Z]+$/)', $givenName))
    $errMsg .= '<p>Invalid given name!</p>';
    if(strlen($firstName) > 35)
    $errMsg .= '<p>First Name name must be no longer than 35 characters</p>';
    
    else
    if(strlen($givenName) > 35)
    $errMsg .= '<p>given Name ID must be no longer than 35 digits</p>';
    }
    	
    ?>

  • #2
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oh forgot my html
    Code:
    <tr> <td> Students Family Name </td>
    		<td> <input type="text" size="30" id="fname" name="fname" /> </td></tr>
    	
    		<tr> <td> Students Given Name </td>
    		<td> <input type="text" size="30" id="gname" name="gname" /> </td> </tr>

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    As a start...
    No where in the code(s) you have provided do you echo the php variable $errMsg

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by litebearer View Post
    As a start...
    No where in the code(s) you have provided do you echo the php variable $errMsg
    This is probably the biggest problem you have. There isn't any output at all in this script, so you cannot tell either way.
    If you need to keep your messages separate (bad characters versus bad length), then I'd suggest checking the length first, and then checking the characters. PCRE is expensive to perform compared to string length checking, so check the length first, and only if its good you should then check the characters.
    If you don't need to track the difference between the problem, you can pattern the length as {x,35} where x is your minimum characters instead of using the +. The ruleset doesn't handle things like repetition of characters, but that's not explicitly a part of your ruleset.
    Also, replace this:
    PHP Code:
        if(isset($_POST['submit'])) 
    With this:
    PHP Code:
    if (isset($_POST['fname'], $_POST['gname'])) 
    Best to use isset check on everything you want to have come through. The submit is likely just a submission button which is irrelevant to retrieving data (unless you use multiple submits on single form then it may be relevant for the operation).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks heaps guys, like i said im fairly new hehe but im having a go, mind showing me how to fix my code??

  • #6
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It display an alert even if i type my name JOHNNY followed by a hyphen (-) , it's suppose to allow maximum 35 characters , alpha characters and hyphen !? . Where did i go wrong ?

  • #7
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind lol i figured it out after ................ 8 HOURS! omg lol.. ANy idea how to validate a date between 2001 and 2004 in PHP ? The value from the form is 'dob', my assignment states "Mandatory. Must be a valid date in the format dd/mm/yyyy and the year is between 2000 and 2004."

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There are a number of ways to get the year part out of a string in that format. In line with the rest of the code you have, I'd suggest either the use of an explode and pulling the last item in the array, or the use of strrpos combined with substr would work.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Any examples or can you help? I'm not quite familiar with those functions as of yet mate

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Since this is for an assignment, I don't want to just give you an answer for it. But I can show you what to do.
    PHP Code:
    $sString 'This is my string.  It has a few sentences in it.  I will split them';
    $aSentences explode('.'$sString);
    print 
    end($aSentences); // '  I will split them';

    if (false !== ($iPos strrpos($sString'.')))
    {
        print 
    substr($sString$iPos 1); // '  I will split them';

    Those are both for string handling. When you assign the year to a variable, you can then check it using standard >= and <= comparisons.

    For more date-ish type handling, you can look into using the getdate function which will return an array with all the date parts within it (and much more information). To get there, you'd need to use strtotime with a format of dd/mm/yyyy though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by Johnny87auxsm View Post
    I'm not quite familiar with those functions as of yet mate
    The best way to become familiar is to visit the php.net manual and lookup the functions and read the user notes.

    If you want to understand what you're doing and pass your assignment you must do research and put in some effort
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    New Coder
    Join Date
    Apr 2013
    Posts
    38
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Try this website for any issue regarding PHP Coding

    Website link for PHP coding issues ..

    http://www.w3schools.com/php/default.asp



  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    w3schools are not a good place to learn about php.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i have something like this, but instead of hard coding a date how would i do it based on a date the user enters and checks if between 2001 and 2004 ?

    Code:
    $paymentDate = DateTime::createFromFormat('d/m/Y', '31/12/2010');
    $contractDateBegin = DateTime::createFromFormat('d/m/Y', '01/01/2001');
    $contractDateEnd = DateTime::createFromFormat('d/m/Y', '01/01/2012');
    
    if ($paymentDate >= $contractDateBegin && $paymentDate <= $contractDateEnd)
    {
      echo "is between\n";
    }

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I actually suggest you do *not* use the datetime. So much as I like the datetime, it doesn't handle something like the year between dates very well. It is chained to the comparators, so that's a plus (object oriented wise), but to check just the year I'd use the getdate() function which will give you an array back. One of the offsets is 'year', which you can then check against <= and >= for your requirements (stick with variables, you can always configure the min and max range at the top of your script for it).

    DateTime wise, you can provide the $_GET/$_POST data to your $paymentDate when constructing the DateTime::createFromFormat. So instead of 31/12/2012, you can give it a variable (such as $_GET or $_POST offset of say, 'paymentDate').
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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