I'm coding out a simple contact form, and the following code is giving me trouble:
if(!strlen($_POST['lastname']) > 0){
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!strlen($_POST['comments']) > 5){
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
Relevant form element:
<textarea name="comments" cols="60" rows="5"><?=(isset($_POST['comments']) ? $_POST['comments'] : '')?></textarea>
When I leave both fields blank, only the first error message (along with the others not shown) displays, whereas the one for the comments field does not.
The error checking even returns with an error if I submit the comments fields with less than 5 characters, as it should, but the error message does not print. In addition, I even echoed the strlen() of the comments field when I submitted with it blank and it prints out 0.
Can anyone see what the problem here is?
if (!strlen > 0) first evaluates strlen, which gives, say, 10. This is then negated by ! to false. This is then compared to > 0, which is false, since false will be cast to 0 and 0 > 0 is false. The other way around, if the string is actually empty, the condition will be true.
You either want if (!(strlen > 0)) or if (strlen <= 0).
Missing parenthesis?
if(!(strlen($_POST['lastname']) > 0)) {
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!(strlen($_POST['comments']) > 5)) {
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
If you want to validate the data whether it is entered you can use following options too
if(empty($_POST['name'])){
echo 'Enter Name';
}
or
if(trim($_POST['name'])==''){
echo 'Enter Name';
}
Just to be clear, the problem here is that the ! applies to the return value from strlen only and not the entire comparison expression. The above if statements will always return false.
It could be rewritten like this to work:
if (!(strlen($_POST['lastname']) > 0)){ /* display error */ }
This is because the ! negates the result of the expression nested in parentheses and not the number returned by strlen.
Also, I would recommend not returning the POSTed value unaltered in the HTML source, that's just asking for trouble...
Related
if(isset($_POST["submit"])){
if (isset($_POST["name"]) && isset($_POST["roll"])){ // checking whether both text boxes are filled up
echo "$_POST[name] $_POST[roll]"; // just displays some info
}
else{ //not being executed when both text boxes are empty
header("Location:interface.php");
}
}
the above is a php code where im trying to check whether two text boxes are filled in or else i will navigate to another page. The problem is though if i fill up one text box the && condition doesn't work , it gets into the if part and echoes one value.
There is error (missing ') in:
echo "$_POST[name] $_POST[roll]";
It has to be $_POST['name'] and $_POST['roll']
UPDATE
isset() only is not enough. You should also check if it is not empty, like this:
if (!empty($_POST["name"]) && !empty($_POST["roll"])){
When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.
Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}
if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}
Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';
Hi Guys i am having a very peculiar problem i have written a small code so that it is possible to sign up to the website i am creating with a few restrictions to each field however from what i see the code seems ok but each time i try to use the php code with html code the browser is always printing part of the code like this:
"11){ echo 'Username needs at least 6 and maximum 11 characters'; } else if (strpos($Username, '#') != 0) { echo ' Username cant be your email'; } ?>"
<?php
$Username = 'aasd';
if (strlen($Username) < 6 || strlen($Username) > 11){
echo 'Username needs at least 6 and maximum 11 characters';
}
else if (strpos($Username, '#') != 0) {
echo ' Username cant be your email';
}
?>
From what i can see the code is correct but i can't seem to find the reason as to why this is happening am i missing something in the PHP code? do i have some condition or operator that is not set in the right way?
Thank you first hand to all those who reply
Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value
This seems like it should be easy but I can't for the life of me get this to work, I'm checking a variable in a query string, if it returns a Yes it redirects to a new page. If it returns a no I want it to display an error message above the form.
My issue is with displaying the error message, it redirects when var=Yes but when it gives me var=no I can't get it to display the error message. If I replace $errormsg with header(..); it works so I know it's not my if statement.
if (isset($_GET['var'])) {
$var = $_GET['var'];
$errormsg = '';
if ($var == 'Yes') {
header( 'Location: http://www.google.com' ); exit;
}
else if ($var == 'no') {
$errormsg = 'This is an error message.';
}
};
And this is the error message above the form:
<div class="errormsg"><?php echo $errormsg; ?></div>
My PHP ability is limited and I can't figure out what I'm doing wrong, any help will be appreciated.
Thanks!
My guess is the $var is not returning what you are expecting (possibly a "No" instead of a "no" as mjayt suggested in the comments.
First step would be to debug, and determine what $var is actually returning. Try simple print statements and go from there.
Try to echo $errormsg directly under the if block without any HTML. See if your text is returned.
Think you might remove that semi colon from that last curly brace also