Reputation: 1
I am trying to write a check for Vendors and models in a form I have put together
Here is the relavent part of the form
<table width="75%">
<form action="?" method="post">
<tr>
<td colspan="6" align="center">
<font size="5"><b>Add new record</b></font>
</td>
</tr>
<tr>
<td align="right">
<b>Vendor:</b>
</td>
<td>
<select name="vendor">
<option value="ClearAccess">ClearAccess</option>
<option value="VisionNet">VisionNet</option>
<option value="Other">Other</option>
</select>
</td>
<td align="right">
<b>Model:</b>
</td>
<td>
<select name="model">
<option value="NotModel">--ClearAccess--</option>
<option value="AG10W">AG10W</option>
<option value="SR100G">SR100G</option>
<option value="SR300N">SR300N</option>
<option value="SR350N">SR350N</option>
<option value="SR500N">SR500N</option>
<option value="NotModel">--VisionNet--</option>
<option value="M404">M404</option>
<option value="M405">M405</option>
<option value="M505">M505</option>
<option value="M505n">M505N</option>
<option value="Legacy">Legacy</option>
</select>
</td>
my form is good. my issue is when I am building my if loop. Here is what I have so far
if ($_POST['vendor']='ClearAccess') && ($_POST['model']='M404') || ($_POST['model']='M405') || ($_POST['model']='M505') || (_$POST['model']='M505N') || ($_POST['model']='Legacy')
echo "You have not selected a valid vendor/model combination"
I am having an issue with where and how to place my parenthesis.
I am also going to create an if statement similar to this
if ($_POST['vendor']='VisionNet') && ($_POST['model']='AG10W') || ($_POST['model']='SR100G') || ($_POST['model']='SR300N') || (_$POST['model']='SR350N') || ($_POST['model']='SR500N')|| ($_POST['model']='Legacy')
echo "You have not selected a valid vendor/model combination"
I am completely open to doing it different ways. Eventually I will turn this into java script where you pick a vendor and get only the relavant models for that vendor. Put as I am still learning PHP and have not learned even the basics of Java yet I want to try it this way.
Upvotes: 0
Views: 3932
Reputation: 7040
Reduce your logic. Start with what you want to do in general statements.
A = "Vendor is VisionNet"
B = "Model is AG10W"
C = "Model is SR100G"
etc.
Then write up your logic as
if (A and (B or C))
OR
if ((A and B) or C)
etc.
Then replace your place-holders with the PHP equivalent.
In PHP, if you're trying to see if x is one of many values, you can use the in_array()
method:
$models = array('AG10W', 'SR100G', 'SR300N', 'SR350N', 'SR500N', 'Legacy');
// if the vendor is vision net OR the model is in the list of models defined in $models, do something
if ($_POST['vendor'] == 'VisionNet' || in_array($_POST['model'], $models)) {
// do something.
}
Take operators into consideration as well. Assignment is different than comparison.
Remember, logic is just another subset of mathematics. Parentheses are important, so if you close your if
statement early, that's the end of it. Everything after will become a syntax error.
I mention that because you wrote this:
if ($_POST['vendor'] = 'VisionNet') || ...
Here's a link to a tutorial on discrete mathematics which is a fancy way of saying "the mathematics of logic".
Also, for all those interested, MIT (the Massachusetts Institute of Technology) offers open courses online for free, one of which is the mathematics of computer science which discusses discrete math in-depth.
Upvotes: 4
Reputation: 784998
You must use ==
instead =
in your if conditions.
So instead of:
if ($_POST['vendor']='ClearAccess') && ...
use
if ($_POST['vendor'] == 'ClearAccess') && ...
Better to have your if condition like this:
if ($_POST['vendor'] == 'ClearAccess') &&
str_replace(array('M404', 'M405', 'M505', 'M505N', 'Legacy'), '', $_POST['model'])
!= $_POST['model']
)
echo "You have not selected a valid vendor/model combination"
Upvotes: 0
Reputation: 564
You need to put your ()'s right...
Just to make you aware of the syntax, here is the code with nice line breaks :)
if (
($_POST['vendor'] == 'ClearAccess' && $_POST['model'] = 'M404') ||
$_POST['model'] == 'M405' ||
$_POST['model'] == 'M505' ||
$_POST['model'] == 'M505N' ||
$_POST['model'] == 'Legacy'
) {
echo "You have not selected a valid vendor/model combination"
}
And without the linebreaks:
if (($_POST['vendor'] == 'ClearAccess' && $_POST['model'] == 'M404') || $_POST['model'] == 'M405' || $_POST['model'] == 'M505' || $_POST['model'] == 'M505N' || $_POST['model'] == 'Legacy') {
echo "You have not selected a valid vendor/model combination"
}
Your other statement goes the same way.
Upvotes: 0
Reputation: 3558
Wrap your && within its own set of parenthesis. So the example below says if POST[vendor] is Clear Acces and POST[model] is M404, or POST[model] is M405, M505, M505N, or Legacy.
if (($_POST['vendor']=='ClearAccess') && ($_POST['model']=='M404')) || ($_POST['model']=='M405') || ($_POST['model']=='M505') || (_$POST['model']=='M505N') || ($_POST['model']=='Legacy')
echo "You have not selected a valid vendor/model combination"
Upvotes: 0
Reputation: 155
You should look at the php syntax for if then statements http://php.net/manual/en/control-structures.if.php because your syntax is wrong. You are assigning your post variables to values other than using ==
if ($_POST['vendor']=='VisionNet')
Also, you should rethink your $$ and || usage and decide what that is actually saying in your program.
Upvotes: 0
Reputation: 39522
First off, you need to use ===
instead of =
. The single equals means assignment, not comparing.
Secondly, what I think you're trying to do is say "if it's this vendor AND one of these models, do something. If that's the case, try this:
if($_POST['vendor'] === 'ClearAccess' && (*various models, using ||*))
//dostuff
That way, it will equate to true
if the vendor is equal as well as at least one of the models.
A better way to do this would be to build up an array of the various models and then use PHP's in_array()
Upvotes: 1
Reputation: 1667
Here are your problems :
=
instead of ==
()
in the if
statementchange from
if ($_POST['vendor']='ClearAccess') && ($_POST['model']='M404') || ($_POST['model']='M405') || ($_POST['model']='M505') || (_$POST['model']='M505N') || ($_POST['model']='Legacy')
to :
if (($_POST['vendor']=='ClearAccess') && ($_POST['model']=='M404') || ($_POST['model']=='M405') || ($_POST['model']=='M505') || (_$POST['model']=='M505N') || ($_POST['model']=='Legacy'))
Upvotes: 1