drop-down option value for if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gluttonous_pet
Forum Newbie
Posts: 13
Joined: Thu Aug 19, 2010 4:06 am

drop-down option value for if statement

Post by gluttonous_pet »

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
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: drop-down option value for if statement

Post by timWebUK »

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:

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>
PHP:

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;
}
?>
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.
gluttonous_pet
Forum Newbie
Posts: 13
Joined: Thu Aug 19, 2010 4:06 am

Re: drop-down option value for if statement

Post by gluttonous_pet »

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

User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: drop-down option value for if statement

Post by timWebUK »

I usually use Switch statements for that sort of thing, but what you're doing is fine.

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

Post by gluttonous_pet »

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

Post by gluttonous_pet »

I now have something like this:

Code: Select all

<strong>Select Category:</strong>
<select name="category" class="dropd">
<option value="all">All Categories</option>
</select>

$dropDownOption = $_POST['category'];
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:

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>
Could you give me a few tips on doing this?

Thanks!
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: drop-down option value for if statement

Post by timWebUK »

I would just pass a $_GET[]; variable from your links, which then get parsed into a switch.

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>
PHP:

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
}
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

Post by gluttonous_pet »

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.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: drop-down option value for if statement

Post by timWebUK »

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

Post by gluttonous_pet »

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

Post by gluttonous_pet »

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
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: drop-down option value for if statement

Post by timWebUK »

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'
Post Reply