• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

See if variable from a list box is IN a list?

I have a listbox in the follwing format (HTML):

<select class="select" name="passport" onchange="">
                        <option value="" selected>Select a country</option>
                        <option>-----------------------</option>
                        <option>Afghanistan</option>
                        <option>Albania</option>
                        <option>Algeria</option>
                        <option>American Samoa</option>
                                etc.

Now I have 5 categories of countries, example of category one below:

| Aruba | Australia | Austria | Belgium | Bermuda | Canada | Cayman Islands Channel Islands | Denmark | Finland | France | French Polynesia | Germany | Gibraltar Guam | Hong Kong | Iceland | Ireland | Italy | Japan | Kuwait | Liechtenstein | Luxembourg Monaco | Netherlands | Norway | Qatar | San Marino | Singapore | Sweden | Switzerland UAE | United Kingdom | United States | Vatican

What i need to do is see if what the user selected in the drop down box is in one of the categories? Is there an easier way to do it other than 100's of If statements, if so how?
0
AndriesKeun
Asked:
AndriesKeun
  • 5
  • 5
  • 3
  • +1
1 Solution
 
dr_dedoCommented:
first, you sould add value to ur options e.g.
<option value="Afghanistan">Afghanistan</option>
this way, $_REQUEST['passport'] would return country name

then u can use a simple regex
if(preg_match ('/\b'.$_REQUEST['passport'].'\b/',$country_list_1)

you can loop through country lists with a for loop
you can have county lists as arrays and check using in_array() function
0
 
TeRReFCommented:
A regex for such a simple task is a bit overkill. Use in_array(), it's faster.
if(in_array($_REQUEST['passport'], $country_list_1))
0
 
TeRReFCommented:
Sorry dr_dedo, I hadn't seen your reference to in_array!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TeRReFCommented:
If it's a string, you should use strpos()...
if(strpos($country_list_1, $_REQUEST['passport']))
0
 
Richard QuadlingSenior Software DeverloperCommented:
I would use $_GET or $_POST for finer control over the data coming in.

From the PHP Manual ...

Note:
Introduced in 4.1.0. There is no equivalent array in earlier versions.

Note:
Prior to PHP 4.3.0, $_FILES information was also included in $_REQUEST.

An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_REQUEST; to access it within functions or methods.




So, if you save a value in the cookie and have it on the form? Which one will you get? You can only have 1!!!!

By using the right superglobal, you are in control and not hoping the user (or a hacker) doesn't mess things up for you!

0
 
AndriesKeunAuthor Commented:
Its not working.

My Array:

$categorya=array("Andorra","Aruba","Australia","Austria","Belgium","Bermuda","Canada","Cayman Islands Channel Islands","Denmark","Finland","France","French Polynesia","Germany","Gibraltar Guam","Hong Kong","Iceland","Ireland","Italy","Japan","Kuwait","Liechtenstein","Luxembourg Monaco","Netherlands","Norway","Qatar","San Marino","Singapore","Sweden","Switzerland UAE","United Kingdom","United States","Vatican");

php:

$points = 50;
if(in_array($_GET['passport'], $categorya)) $points += 1000;

if(in_array($_POST['passport'], $categorya)) $points += 1000;

if(in_array($_REQUEST['passport'], $categorya)) $points += 1000;

if(strpos($categorya, $_GET['passport'])) $points += 1000;

if(strpos($categorya, $_POST['passport'])) $points += 1000;

if(strpos($categorya, $_REQUEST['passport'])) $points += 1000;

I email the result to me and only get 50 points.


0
 
Richard QuadlingSenior Software DeverloperCommented:
Try this as the form handler.
<?php
error_reporting(E_ALL);

$categorya=array("Andorra","Aruba","Australia","Austria","Belgium","Bermuda","Canada","Cayman Islands Channel Islands","Denmark","Finland","France","French Polynesia","Germany","Gibraltar Guam","Hong Kong","Iceland","Ireland","Italy","Japan","Kuwait","Liechtenstein","Luxembourg Monaco","Netherlands","Norway","Qatar","San Marino","Singapore","Sweden","Switzerland UAE","United Kingdom","United States","Vatican");

echo '<pre>' . var_export($_GET, True) . var_export($_POST, True) . var_export($_COOKIE, True) . '</pre>';

$points = 50;
if (isset($_GET['passport']) && in_array($_GET['passport'], $categorya))
 {
 $points + 1000;
 }
if (isset($_POST['passport']) && in_array($_POST['passport'], $categorya))
 {
 $points + 1000;
 }

echo $points;
?>

What EXACTLY comes out?
0
 
AndriesKeunAuthor Commented:
Ah, think i spotted it This comes out:

array (
)array (
  'qualification' => 'None',
  'work' => 'None',
  'salary' => '',
  'passport' => '“United',
  'spouse' => 'No Degree / No Spouse Apply',
)array (
)

50

Passport should be "United Kingdom", its cut the last part off. But i set the value properly

                    <select class="select" name="passport" onchange="">
                              <option value="" selected>Select a country</option>
                              <option value="">-----------------------</option>
                              <option value=“United Kingdom“>United Kingdom</option>
                              <option value=“United States“>United States</option>
                              <option value=“South Africa“>South Africa</option>
                              <option value=“Australia“>Australia</option>
                              <option value=“New Zealand”>New Zealand</option>

know how to sort it?
0
 
Richard QuadlingSenior Software DeverloperCommented:
It looks like the " you are using is different sometimes.

If you do a view source on this page you will get ...

      Ah, think i spotted it This comes out:<br /><br />array (<br />)array (<br />&nbsp; 'qualification' =&gt; 'None',<br />&nbsp; 'work' =&gt; 'None',<br />&nbsp; 'salary' =&gt; '',<br />&nbsp; 'passport' =&gt; '“United',<br />&nbsp; 'spouse' =&gt; 'No Degree / No Spouse Apply',<br />)array (<br />)<br /><br />50<br /><br />Passport should be &quot;United Kingdom&quot;, its cut the last part off. But i set the value properly<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;select class=&quot;select&quot; name=&quot;passport&quot; onchange=&quot;&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;&quot; selected&gt;Select a country&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;&quot;&gt;-----------------------&lt;/option<wbr/>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“United Kingdom“&gt;United Kingdom&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“United States“&gt;United States&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“South Africa“&gt;South Africa&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“Australia“&gt;Australia&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“New Zealand”&gt;New Zealand&lt;/option&gt;<br /><br />know how to sort it?</div>


You will see that the " are shown as &quot; (which is correct) but you also have “ which is a different symbol.

Are you using something like Word or Frontpage?
0
 
AndriesKeunAuthor Commented:
using editplus, but copied it from word. will change them all and see if it works. I notice now that they are looking different.
0
 
Richard QuadlingSenior Software DeverloperCommented:
Word uses an auto formatter to make quotes look nice (opening and closing quotes). Word has no place in writing PHP code. Completely the wrong tool.
0
 
AndriesKeunAuthor Commented:
Yes I know, the the list of 200+ countries and areas were already in a word document, thought it would be easier to just do the necesaries there and then just copy over, but prob should just have done it the other way, just me being lazy :)
0
 
Richard QuadlingSenior Software DeverloperCommented:
Ha!

Did that all work then?
0
 
AndriesKeunAuthor Commented:
nice one bro, cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now