drop-down option value for if statement
Moderator: General Moderators
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
drop-down option value for if statement
Hi guys, I'm trying to put something together in my app where the drop-down value selected determines which if statement will run, there's no database involved, could you point me in the right direction? I have the rest of the app sorted out.
Many Thanks
G
Many Thanks
G
Re: drop-down option value for if statement
Using POST you can read the value of the dropdown box, from there you can write a switch statement or a series of IF.
Example:
HTML:
PHP:
If you do not wish to use POST, you could always use AJAX - use JavaScript's "getElementById", and read the value that way then send the request asynchronously to the web server in the background. But the switch statement would still apply. Just pass the value as a parameter rather than using POST variable.
I hope this helps.
Example:
HTML:
Code: Select all
<!-- Excerpt -->
<select name="myExample">
<option value="Rock">Rock</option>
<option value="Pop">Pop</option>
<option value="Rap">Rap</option>
<option value="Hip-Hop">Hip-Hop</option>
</select>Code: Select all
<?php
//Excerpt
$dropDownOption = $_POST['myExample'];
switch($dropDownOption)
{
Case "Rock":
echo "Led Zeppelin";
break;
Case "Pop":
echo "Michael Jackson";
break;
Case "Rap":
echo "Eminem";
break;
Case "Hip-Hop":
echo "Run DMC";
break;
}
?>I hope this helps.
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
Thank you! I'm just a little confused on the IF statement bit, I want to do something like this and separate each of the options for an IF:
Code: Select all
if ($dropDownOption = 'Rock') {
if (...)
{
...
}
else if (...)
{
...
}
}
else if ($dropDownOption =! 'Rock') {
echo "didn't work";
}
Re: drop-down option value for if statement
I usually use Switch statements for that sort of thing, but what you're doing is fine.
P.S. 'Not equals' is !=
P.S. 'Not equals' is !=
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
Thanks for all your help, much appreciated! All working now 
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
I now have something like this:
I then put $dropDownOption in a switch and retrieve the individual values from the "category" dropdown e.g. "all"
But I want to do the same thing with a list style navigation taking the value of the li or link
and once the link has been clicked use the value that has been clicked to populate the switch:
Could you give me a few tips on doing this?
Thanks!
Code: Select all
<strong>Select Category:</strong>
<select name="category" class="dropd">
<option value="all">All Categories</option>
</select>
$dropDownOption = $_POST['category'];But I want to do the same thing with a list style navigation taking the value of the li or link
and once the link has been clicked use the value that has been clicked to populate the switch:
Code: Select all
<div id="container">
<div id="menu">
<ul name="category" class="menu">
<li><a href="#">Select Category +</a>
<ul>
<li value="all"><a href="#">all</a></li>
<li value="two"><a href="#">two</a></li>
<li value="three"><a href="#">three</a></li>
</ul>
</li>
</ul>
</div>
</div>Thanks!
Re: drop-down option value for if statement
I would just pass a $_GET[]; variable from your links, which then get parsed into a switch.
For example:
HTML:
PHP:
I hope this helps.
For example:
HTML:
Code: Select all
<a href="localhost/test/option.php?o=1">Option 1</a>
<a href="localhost/test/option.php?o=2">Option 2</a>
<a href="localhost/test/option.php?o=3">Option 2</a>Code: Select all
$myVar = $_GET['o'];
//Whitelist of allowed GET values
$typeArray = array('1','2','3');
if(in_array($myVar, $typeArray))
{
switch($myVar)
{
case 1:
break;
//etc ....
}
}
else
{
//Reject
}
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
Thanks, I've tried it out and I can see the stuff OK in the querystring when I hover over/click the links but instead of running the switch cases successfully and it echoing the correct ouput I'm given the "else" echo messages.
Re: drop-down option value for if statement
If I've understood you correctly, that probably means whatever you're testing for isn't in the whitelist array. That's the only way it drops into the else rather than the switch.
-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
You're absolutely right, your logic works fine, it's when I come to add my own logic within it (which worked before) and it doesn't return any results, instead it returns the "Didn't work" message. Bit stumped!
Code: Select all
$myVar = $_GET['o'];
//Whitelist of allowed GET values
$typeArray = array('all','other','stuff');
if(in_array($myVar, $typeArray))
{
switch($myVar)
{
/* this logic for all categories */
Case "all":
if ($selling <= 49.99)
{
...
}
else if ($selling <= 599.99)
{
...
}
else if ($selling >= 600.00)
{
...
}
if ($selling && $cost != '') {
echo "Here's the answer";
}
else {
echo "Didn't work";
}
break;
}
}
else
{
echo "Please make your selection";
}-
gluttonous_pet
- Forum Newbie
- Posts: 13
- Joined: Thu Aug 19, 2010 4:06 am
Re: drop-down option value for if statement
I'm not positive but I think the problem is that my other values need to be passed over the querystring, in this case they are text inputs, can you please show me how to add these to the existing querystring? e.g. passed along with $myVar.
<input type="text" name="stuff" id="stuff">
Thanks!
G
<input type="text" name="stuff" id="stuff">
Thanks!
G
Re: drop-down option value for if statement
It still works exactly the same as a <select>:
Code: Select all
<input type="text" name="myVarTwo" value="myValue" />Code: Select all
$myVar2 = $_POST['myVarTwo'];
//$myVar2 will contain 'myValue'