anevins Posted April 2, 2011 Share Posted April 2, 2011 Hi there, I've got a HTML textarea with the name attribute of 'review' and I've posted this into a variable called $review. I want to say if the characters are less than 30, please enter more words etc. The problem is, I get the if statement's error even when there are more than 30 characters. Can anyone figure out why I'm getting this? if ($review < 30){ echo '<p class="red">Review is too short, please enter at least 15 words</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/ Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 Use strlen(), not a value comparison. Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195965 Share on other sites More sharing options...
jamesjmann Posted April 2, 2011 Share Posted April 2, 2011 Echo $review to see if the variable is getting the string. Also print_r the post array to make sure the text areas value is getting registered. Thats a start... Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195966 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 I've now got this: if (strlen($review) > 30){ echo '<p class="red">Review is too short, please enter at least 15 words</p>'; } but I can't get the error message to appear now; if the characters are less than 30 Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195968 Share on other sites More sharing options...
jamesjmann Posted April 2, 2011 Share Posted April 2, 2011 Echo $review to see if the variable is getting the string. Also print_r the post array to make sure the text areas value is getting registered. Thats a start... I jusy forgot. Pika's right. U wanna use the strlen function, unless of course $review is an integer. Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195969 Share on other sites More sharing options...
jamesjmann Posted April 2, 2011 Share Posted April 2, 2011 I've now got this: if (strlen($review) > 30){ echo '<p class="red">Review is too short, please enter at least 15 words</p>'; } but I can't get the error message to appear now; if the characters are less than 30 Post the rest of ur script Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195970 Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 Probably because you're using a 'greater than' comparison . . . Also, do you want characters or words counted? Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195971 Share on other sites More sharing options...
lastkarrde Posted April 2, 2011 Share Posted April 2, 2011 if (strlen($review) < 30){ echo '<p class="red">Review is too short, please enter at least 15 words</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195972 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 I've echo'd the variable and there is data in it Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195973 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 I just want the characters, I'll post the rest of the code 1 second Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195974 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Problem solved; I was using greater than instead of less than. Thanks for your keen interests everyone. Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195975 Share on other sites More sharing options...
jamesjmann Posted April 2, 2011 Share Posted April 2, 2011 Problem solved; I was using greater than instead of less than. Thanks for your keen interests everyone. Haha wow. Shuda seen that one comin Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1195976 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Hi again, I'm trying to do some form validation. I want to say, if the input in the textarea is greater than 30 and less than 255 characters, do some stuff. But I think something's wrong with my current if statement: if ((strlen($review) < 255) && strlen(($review) > 30)){ ... } As when I change the if statement to if(!empty($review)){ ... }, the if statement works. Can you see what's wrong with my strlen if statement? Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196008 Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 In what way doesn't it work? Have you echoed strlen($review) to see what the value is? [Threads merged] Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196009 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 I can echo $review before the if statement, but as soon as PHP reaches that particular if statement, it stops Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196011 Share on other sites More sharing options...
Jnerocorp Posted April 2, 2011 Share Posted April 2, 2011 change: if ((strlen($review) < 255) && strlen(($review) > 30)){ to if (strlen($review) < 255) && strlen($review) > 30)){ Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196012 Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 You have unnecessary parentheses in the if() conditional. if ( strlen($review) < 255 && strlen($review) > 30 ) { Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196013 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Problem solved, thanks Jnerocorp and Pikachu Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196014 Share on other sites More sharing options...
jamesjmann Posted April 3, 2011 Share Posted April 3, 2011 You have a second parenthesis after the second strlen ("strlen((") I think you meant to put it after the second strlen ("strlen($review))"). In any case, its rather needless to enclose both strlen's in parenthesis. I would just write it as: <?php if (strlen($review) < 255 && strlen($review) > 30) { //blahblahblahblah } else { //blahblahblahblah } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232513-form-validation/#findComment-1196125 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.