Getting a value from a drop down list using php

Hi, I am new to PHP and need some help.

I have a small Admin Panel to be used for various options. On the fist page I have a Dashboard, the content of the Dashboard will depend on the Selected Company. The Company is a Drop down list read from MySQL Database. That part works just fine.

<?php 
echo '<form method="post">';
$rows = db_select("SELECT idcompany, company FROM company order by idcompany;"); 
if($rows === false) {
		    $error = db_error();
			echo "<ul class='error'>";
			echo "<li class='error'>Error : $error </li> ";
			echo "</ul>";			
		}
?><label>Company: &nbsp; </label><?php 
?><select id="Company" onChange=" onChange="location.reload()"> <?php 
	 foreach($rows as $row){
          echo "<option value='" . $row['idcompany'] ."'>" . $row['company'] ."</option>";
	 } 
  echo '</select>';
  
echo '</form>'; 
?> 

Open in new window


The issue is that depends on the Selected Company (company_id) the dashboard will display the content.

// SELECT * FROM notifications_vw where idcompany = $id_company

$id_company = $_GET['Company'];
$rows = db_select("SELECT * FROM notifications_vw where idcompany = $id_company"); 
if($rows === false) {
    $error = db_error();
	echo  $error;
}

Open in new window


Unfortunately this does not work for me, I have a problem reading the list value. Ideally I should read it by the Tag ID or something like this.

The ID of the Company should be available at all times. I mean at the moment of the display of dashboard, add new customer, or display any other element that where idcompany is in use.

On the other hand when I change the Company from the drop down list, the website should reload and new company_id to be used.

Any ideas please?
List.PNG
kanaMTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
you say <select id="Company"....

I think you meant: <select Name="Company"...

http://www.w3schools.com/tags/att_select_name.asp
Dave BaldwinFixer of ProblemsCommented:
I usually use both 'id' and 'name'.  JavaScript likes 'id' and the form post to PHP uses 'name'.
Moussa MokhtariEnterpreneurCommented:
Hi @KanaMT ,
You use the $_GET when you are getting some data from the url like www.mysite.com/?name=rainbow
But in your case you should use $_POST['Company'] and change the id tag to name tag name='Company' ,Or change the method of the form and see
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

kanaMTAuthor Commented:
Okay, thank you all for helping.
Partially I have managed.

<?php  
echo '<form action="" method="post" name="company_form">';
$rows = db_select("SELECT idcompany, company FROM company order by idcompany;"); 
if($rows === false) {
		    $error = db_error();
			echo "<ul class='error'>";
			echo "<li class='error'>Error : $error </li> ";
			echo "</ul>";			
		}
?><label>Company: &nbsp; </label><?php 
?><select id="id_company" onChange="this.form.submit()" name="Company"> <?php //onChange="location.reload()"
  echo "<option value='0'>Select Company</option>";
	 foreach($rows as $row){
          echo "<option value='" . $row['idcompany'] ."'>" . $row['company'] ."</option>";
	 } 
  echo '</select>';
  echo '</form>';
?> 

Open in new window


and the other part:

<?php
//include "connect.php";

$id_company = $_POST['Company'];

$rows = db_select("SELECT * FROM notifications_vw where idcompany = $id_company "); 
if($rows === false) {
    $error = db_error();
	echo  $error;
}

Open in new window


Now, It works, but not perfect yet.
First of all when the page loads there is nothing in the dashboard- as I have to first select the Company. Then the form is submitted and the page gets refresh. I would prefer upon the load automatically select the first company from the list. (default one). So I do not have to select the company after I log in. I have lost that when added the following line before the loop.
 echo "<option value='0'>Select Company</option>";

Open in new window


I had to to add it as I could not select the first row- each time I change the Company the site refresh and is back on the first row. And that is another problem,

Now I select the Company, the page refreshes, I have the correct results but the Company list returns to back to 'Select Company'- I would like to keep it in the drop down so the user knows what is the Selected Company. and have the ID handy in the memory.

Any ideas?

Thank you in advance.
nociSoftware EngineerCommented:
Choose the current selected company as default in the drop down?
(add selected to the option parameter)
Ray PaseurCommented:
No points for this please, but if you're new to PHP and want to get a good foundation this article can help you find well-vetted learning resources (and avoid the bad examples that litter the internet).
http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Moussa MokhtariEnterpreneurCommented:
Use cookies
Slick812Commented:
greetings  kanaMT, , I follow what you mean about the initial load, and the First company being chosen.  I would think that if the $_POST['Company'] is Empty (first load condition), then test for an undefined (no value) could help, this might work -

$id_company = (empty($_POST['Company'])) ? 'compID321' : $_POST['Company'];

There the string   'compID321'   would be replaced with the default selection company ID.


In select options to can get the shown option to be used with the "selected" -
       <option value="compID321" selected>Acme Company</option>

maybe something like? -
foreach($rows as $row){
      if ( $row['idcompany'] == $id_company)
         $seled = "selected"; else $seled = "";
      echo "<option value='" . $row['idcompany'] ."' " . $seled .">" . $row['company'] ."</option>";
	 }

Open in new window


I have no idea what this may be used for or what you mean - "have the ID handy in the memory" ? ?
hope this is something like what you need, will answer questions if it is not doing what you need.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kanaMTAuthor Commented:
This works for me, thanks Slick812, Your reply was very helpful.

<?php  
echo '<form action="" method="post" name="company_form">';

?><label>Company: &nbsp; </label><?php  
?><select id="id_company" onChange="this.form.submit()" name="Company"> <?php 

if (! empty($_POST['Company'])) {
 	 
  $id_company = $_POST['Company'];
  $rows = db_select("SELECT idcompany, company FROM company where idcompany = $id_company;"); 
      echo "<option value='" . $row['idcompany'] ."' selected >" . $row['company'] ."</option>"; 
}
 	
$rows = db_select("SELECT idcompany, company FROM company order by idcompany;"); 
if($rows === false) {
		    $error = db_error();
			echo "<ul class='error'>";
			echo "<li class='error'>Error : $error </li> ";
			echo "</ul>";			
		}

	 foreach($rows as $row){
      if ( $row['idcompany'] == $id_company) 
         $seled = "selected"; else $seled = ""; 
      echo "<option value='" . $row['idcompany'] ."' " . $seled .">" . $row['company'] ."</option>";
	 }

  echo '</select>';
  echo '</form>';
?> 

Open in new window

kanaMTAuthor Commented:
Thank you all for help, with your help managed to sort it out.

Regards
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.