spanner206 Posted November 20, 2013 Share Posted November 20, 2013 another beginners question from yours truly but i was just wondering how i would code a character limit on one of my fields iv been around the internet but i cant find what i need cause i usually come up with the same error code each time. if anyone knows what to do that would be great, hears the code iv been using. <?php $con = mysqli_connect("localhost","root","","nib"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // define variables and set to empty values $companyname = $firstname = $address1 = $address2 = $area = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typeofbusiness = $renewaldate = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $errors = array(); if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; } <input type='text' name='keywords' size='60' value='companyname' maxlength='10' /> elseif(strpos($_POST['companyname'], '/') !== false) { $errors['companyname'] = 'Cannot use /'; } elseif(strpos($_POST['companyname'], 'The') !== false) { $errors['companyname'] = 'Company Names cant start with The'; } elseif(strpos($_POST['companyname'], 'the') !== false) { $errors['companyname'] = 'Company Names cant start with the'; } elseif(strpos($_POST['companyname'], 'THE') !== false) { $errors['companyname'] = 'Company Names cant start with THE'; } else { $companyname = test_input($_POST["companyname"]); } and heres the error code ive been getting. ( ! ) Parse error: syntax error, unexpected '<' in C:\wamp\www\AddLeads\addeadstemplate.php on line 29 aswell as this if i remove this symbol i get one to do with the else if statement witch i kinda need. again if anyone has a solution please get back to me soon as Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 20, 2013 Share Posted November 20, 2013 If you are breaking in and out of html/php code, then you must use the php opening and closing tags <?php and ?> ?> <input type='text' name='keywords' size='60' value='companyname' maxlength='10' /> <?php Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 20, 2013 Solution Share Posted November 20, 2013 (edited) This is HTML <input type='text' name='keywords' size='60' value='companyname' maxlength='10' /> You cannot just dump that into your PHP code and expect it to work. i was just wondering how i would code a character limit on one of my fields I guess you want to make sure the company name is not more than 60 characters the PHP code would be if(strlen($_POST['companyname'] > 60)) { // company name is greater than 60 characters } Code for validating the companyname field would be if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; } elseif(strlen($_POST['companyname'] > 60)) { $errors['companyname'] = 'Company name must be less then 60 characters'; } elseif(stripos($_POST['companyname'], 'the')) { $errors['companyname'] = 'Company Names cant start with the'; } else { $companyname = test_input($_POST["companyname"]); } Edited November 20, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 20, 2013 Share Posted November 20, 2013 Also, is there a reason the "companyname" field is being displayed in the middle of your if/elseif tests? I would wait until all the tests are done. Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 20, 2013 Author Share Posted November 20, 2013 you've done it again ch0cu3r thanks again m8 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 20, 2013 Share Posted November 20, 2013 Note that you still need to work on the if test for "the". As it stands, an error will be flagged if "the" appears anywhere in the name. Based on the error message, you only want to flag an error if the name starts with "the". To fix the issue, you could switch to a regular expression or maybe use substr(): http://us1.php.net/substr Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.