PHP if else simple question
Moderator: General Moderators
-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
PHP if else simple question
Hi. I'm new here and new to PHP.
I have a form where you load the variable "note", which can have two values, “A” and “B”.
But I want to allow “A”, “a” and “1” to be equivalent entries (and “B”, “b” and “2”).
The code below is somewhat working, since correctly transforms “a” and “1” to “A”, and “b” and “2” to “B”, and also when you load a number different from 1 or 2, correctly displays the warning message.
But when you load a letter different from A, a, B or b, no warning message is displayed, and the variable assumes the value “B”.
I cannot found the error.
Thanks in advance!
<?php
$note = trim($_POST['note']);
if ($note=="A" || $note=="a" || $note==1):
$note="A";
include ('/second.php');
elseif ($note=="B" || $note=="b" || $note==2):
$note="B";
include ('/second.php');
else :
echo "Warning!: The only allowed entries are A, a, 1 or B, b, 2";
endif;
?>
I have a form where you load the variable "note", which can have two values, “A” and “B”.
But I want to allow “A”, “a” and “1” to be equivalent entries (and “B”, “b” and “2”).
The code below is somewhat working, since correctly transforms “a” and “1” to “A”, and “b” and “2” to “B”, and also when you load a number different from 1 or 2, correctly displays the warning message.
But when you load a letter different from A, a, B or b, no warning message is displayed, and the variable assumes the value “B”.
I cannot found the error.
Thanks in advance!
<?php
$note = trim($_POST['note']);
if ($note=="A" || $note=="a" || $note==1):
$note="A";
include ('/second.php');
elseif ($note=="B" || $note=="b" || $note==2):
$note="B";
include ('/second.php');
else :
echo "Warning!: The only allowed entries are A, a, 1 or B, b, 2";
endif;
?>
- php3ch0
- Forum Contributor
- Posts: 212
- Joined: Sun Nov 13, 2005 7:35 am
- Location: Folkestone, Kent, UK
Re: PHP if else simple question
Code: Select all
$note = trim($_POST['note']);
if ($note=="A" || $note=="a" || $note==1) {
$note="A";
include ('/second.php');
} elseif ($note=="B" || $note=="b" || $note==2):
$note="B";
include ('/second.php');
} else {
echo "Warning!: The only allowed entries are A, a, 1 or B, b, 2";
}-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
Re: PHP if else simple question
Thanks for the quick reply!
I've made an involuntary mistake in my previous post.
In order to make the questions simple, I omit that I have four conditions, not only two.
That is, I want (A, a, 1), (B, b, 2), (C, c, 3) and (D, d, 4) to be equivalent.
Now, thanks to your reply, I understand that the problem in the number of conditions. For the two conditions of my first post the code works OK (either mine or yours).
But with four, the output is that of my first post.
Below is a version of the code that is not working.
Thanks again!
$note = trim($_POST['note']);
if ($note=="A" || $note=="a" || $note==1) {
$note="A";
include ('/second.php');
} elseif ($note=="B" || $note=="b" || $note==2):
$note="B";
include ('/second.php');
} elseif ($note=="C" || $note=="c" || $note==3):
$note="C";
include ('/second.php');
} elseif ($note=="D" || $note=="d" || $note==4):
$note="D";
include ('/second.php');
} else {
echo "Warning!: The only allowed entries are A, a, 1 or B, b, 2 or C, c, 3 or D, d, 4";
}
I've made an involuntary mistake in my previous post.
In order to make the questions simple, I omit that I have four conditions, not only two.
That is, I want (A, a, 1), (B, b, 2), (C, c, 3) and (D, d, 4) to be equivalent.
Now, thanks to your reply, I understand that the problem in the number of conditions. For the two conditions of my first post the code works OK (either mine or yours).
But with four, the output is that of my first post.
Below is a version of the code that is not working.
Thanks again!
$note = trim($_POST['note']);
if ($note=="A" || $note=="a" || $note==1) {
$note="A";
include ('/second.php');
} elseif ($note=="B" || $note=="b" || $note==2):
$note="B";
include ('/second.php');
} elseif ($note=="C" || $note=="c" || $note==3):
$note="C";
include ('/second.php');
} elseif ($note=="D" || $note=="d" || $note==4):
$note="D";
include ('/second.php');
} else {
echo "Warning!: The only allowed entries are A, a, 1 or B, b, 2 or C, c, 3 or D, d, 4";
}
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP if else simple question
Code: Select all
<?php
/* Incorrect Method: */
if($a > $b):
echo $a." is greater than ".$b;
else if($a == $b): // Will not compile.
echo "The above line causes a parse error.";
endif;
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
Re: PHP if else simple question
Thanks for your response.
Anyway, that's not the problem.
I got it, but don't how how to solve it.
The problem is when at least one of the conditions have a zero.
In my first post I wrote the conditions like
...
if ($note=="A" || $note=="a" || $note==1)
elseif ($note=="B" || $note=="b" || $note==2)
...
but in fact one of the conditions bears a zero:
...
elseif ($note=="B" || $note=="b" || $note==0)
...
That value of zero is doing the problem, no matter how many conditions you have (my second post was also wrong).
If you replace the zero with another value, everything is OK.
If you have a zero, entering numbers is ok, entering acceptable letters is ok, but entering not-acceptable letters is wrong (do not show the warning message).
I really can't see why,
Any idea?
Thanks!
By the way, how do I format php code in this post?
Anyway, that's not the problem.
I got it, but don't how how to solve it.
The problem is when at least one of the conditions have a zero.
In my first post I wrote the conditions like
...
if ($note=="A" || $note=="a" || $note==1)
elseif ($note=="B" || $note=="b" || $note==2)
...
but in fact one of the conditions bears a zero:
...
elseif ($note=="B" || $note=="b" || $note==0)
...
That value of zero is doing the problem, no matter how many conditions you have (my second post was also wrong).
If you replace the zero with another value, everything is OK.
If you have a zero, entering numbers is ok, entering acceptable letters is ok, but entering not-acceptable letters is wrong (do not show the warning message).
I really can't see why,
Any idea?
Thanks!
By the way, how do I format php code in this post?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP if else simple question
http://php.net/manual/en/language.opera ... arison.php
Have a look at this url; i think the problem is that you are comparing strings and integers.
You format php code by using the PHP Code button when you make a post (same goes if you click on the 'Edit' button to edit a post)
Have a look at this url; i think the problem is that you are comparing strings and integers.
You format php code by using the PHP Code button when you make a post (same goes if you click on the 'Edit' button to edit a post)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
Re: PHP if else simple question
you got it!
That was the problem.
$note is defined as a string.
I solve the problem replacing
with
Note the "" surrounding the "number".
Thanks a lot!
Great site.
That was the problem.
$note is defined as a string.
I solve the problem replacing
Code: Select all
if ($note=="A" || $note=="a" || $note==0)
Code: Select all
if ($note=="A" || $note=="a" || $note=="0")
Thanks a lot!
Great site.
-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
Re: PHP if else simple question
Hi.
I have a form with four textboxes. There are only two allowed values to be send (0 and 10), but for each of them you can enter three values at your preference: P, p and 10 are converted to 10, and M, m and 0 are conveted to 0. The script "chekform" do the trick:
My problem is that it works OK if the name of the textboxes are unique:
But fails if the name are the same:
In my real world I have 32 textboxes, and 4 allowed values for each one, so I'm looking for a smart solution, instead of typing 32 "checkforms".
And it would be a great thing to mantain all textboxes with the same name in the textboxes (only one is active according to a previous page info) for the next handler to get the data.
As you can see, I'm newbie. But I would greatly appreciate any suggestions.
I have a form with four textboxes. There are only two allowed values to be send (0 and 10), but for each of them you can enter three values at your preference: P, p and 10 are converted to 10, and M, m and 0 are conveted to 0. The script "chekform" do the trick:
Code: Select all
<script>
function checkform(){
if (document.LoadData.Data1.value =="P" || document.LoadData.Data1.value =="p" || document.LoadData.Data1.value =="10")
{
document.LoadData.Data1.value =10
return true;
}
else if (document.LoadData.Data1.value =="M" || document.LoadData.Data1.value =="m" || document.LoadData.Data1.value =="0")
{
document.LoadData.Data1.value =0
return true;
}
alert('Forbidden value. Try again');
return false;
}
</script>
Code: Select all
<form name="LoadData" form action="Second.php" method="post" onSubmit="return checkform()">
<table cellpadding="2">
<tr>
<td colspan="4"><a>Enter Data:</a></td>
</tr>
<tr>
<td><input type="text" name="Data1" value=""/></td>
<td><input type="text" name="Data2" value=""/></td>
<td><input type="text" name="Data3" value=""/></td>
<td><input type="text" name="Data4" value=""/></td>
</tr>
<tr>
<td colspan="4"><input type="submit" name="submit" value="GO!"/></td>
</tr>
</table>
</form>
Code: Select all
<td><input type="text" name="Data1" value=""/></td>
<td><input type="text" name="Data1" value=""/></td>
<td><input type="text" name="Data1" value=""/></td>
<td><input type="text" name="Data1" value=""/></td>
And it would be a great thing to mantain all textboxes with the same name in the textboxes (only one is active according to a previous page info) for the next handler to get the data.
As you can see, I'm newbie. But I would greatly appreciate any suggestions.
-
LarsUlrich
- Forum Newbie
- Posts: 11
- Joined: Tue Jan 03, 2012 12:45 pm
Re: PHP if else simple question
In relation to the post above: I've abandoned it, and tried a completely different approach.