I am using a PHP file to output data to an HTML form, and to make the PHP file easier to maintain I am trying to use ‘or’ statements (e.g if $test==“1” or “2” or “3” echo…) but every time I do the file outputs data before you actually submit the form. The strange thing is I can use multiple ‘or’ statements in the first ‘if’ string but any after that messes it up.
The whole file is pretty big but here’s the beginning part where the problem starts…
<?php
if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];
if ($car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="Unopened"){
echo "Your iPhone is worth: $875 - $900";
}
}
if ($car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
echo "Your iPhone is worth: $175 - $190";
}
The first part works just fine, but in the second part if I have a third ‘or’ statement ($sof==“Jailbroken” or “Unlocked” or “Factory”) then it outputs the echo before someone actually submits the form. However, if you only have $sof==“Jailbroken” or “Unlocked” then its OK.
<?php
if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];
if ($car=="ATT"
and $col=="Black"
and ($sze=="16GB" or $sze=="8GB")
and ($sof=="Jailbroken" or $sof=="Unlocked" or $sof=="Factory")
and $mod=="iPhone 1st Generation" )
{
if ($con=="Unopened")
{
echo "Your iPhone is worth: $875 - $900";
}else if ($con=="New"){
echo "Your iPhone is worth: $175 - $190";
}
}
OK first off I fixed the isset at the top, second I changed all ands to && but when changing the ors to II the form wouldn’t even load. However, doing what craqgerbill said and using
if $test=="value1" or $test=="value"
so far has fixed the problem.
I’ll report back if I find anything else wrong.
Thanks
I was originally going to use a MySQL database instead of housing all the values in a single PHP file but I don’t know practically anything when it comes to SQL =P
I’m on a Mac so it doesn’t have that (or maybe it does and I don’t know the command). I am using && instead of ‘and’ now but do you think I’ll run into problems because I’m still using ‘or’?
The weird thing is now that I’m using && and || everything in the entire PHP file gets outputted now matter what is selected. But when I use ‘and’ + || it works properly? Here’s a little test file below…
<?php
if(isset($_POST['carrier']) and isset($_POST['color']) and isset($_POST['size']) and isset($_POST['software']) and isset($_POST['model']) and isset($_POST['condition'])) {
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];
if ($car=="ATT" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 4S" and $con=="Unopened"){
echo "Your iPhone is worth:";
}
}
if ($car=="Sprint" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
echo "Your iPhone is worth:";
}
?>
When using ‘and’ in-place of && and || in place of ‘or’ everything works properly, but as soon as I change ‘and’ to && everything in the file will be outputted.
Ok, I must have screwed something up because nothing works now. I threw together this test file but I can’t seem to get it to work! Would anyone mind checking over it because I can’t seem to find the problem:
And everything works properly now, but am I going to run into future problems by not defining each with $sof==“” or does that not matter because of the ()?
you should have only one }
and that there should be another } at the very end, after the very last }
PS whenever you think of using more than one AND and/or OR, use ( ) to help control the logic. Replying on the rules of precedence to get things right is always risky, ( ) always works.