Page 1 of 1

PHP if else simple question

Posted: Tue Jan 03, 2012 1:05 pm
by LarsUlrich
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;

?>

Re: PHP if else simple question

Posted: Tue Jan 03, 2012 1:25 pm
by php3ch0

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";
}
should work

Re: PHP if else simple question

Posted: Tue Jan 03, 2012 1:55 pm
by LarsUlrich
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";
}

Re: PHP if else simple question

Posted: Wed Jan 04, 2012 3:05 am
by social_experiment

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;

?>
From the php manual. Use { instead of :

Re: PHP if else simple question

Posted: Wed Jan 04, 2012 6:45 am
by LarsUlrich
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?

Re: PHP if else simple question

Posted: Wed Jan 04, 2012 6:52 am
by social_experiment
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)

Re: PHP if else simple question

Posted: Wed Jan 04, 2012 7:24 am
by LarsUlrich
you got it!
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)
with

Code: Select all

if ($note=="A" || $note=="a" || $note=="0")
Note the "" surrounding the "number".

Thanks a lot!
Great site.

Re: PHP if else simple question

Posted: Thu Mar 22, 2012 3:28 pm
by LarsUlrich
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:

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>
My problem is that it works OK if the name of the textboxes are unique:

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>
But fails if the name are the same:

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>

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.

Re: PHP if else simple question

Posted: Fri Mar 23, 2012 9:03 am
by LarsUlrich
In relation to the post above: I've abandoned it, and tried a completely different approach.