Jump to content

character limit


spanner206

Recommended Posts

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 

 

Link to comment
https://forums.phpfreaks.com/topic/284096-character-limit/
Share on other sites

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"]);
}
Link to comment
https://forums.phpfreaks.com/topic/284096-character-limit/#findComment-1459167
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/284096-character-limit/#findComment-1459172
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.