Getting a value from a drop down list using php

kanaMT
kanaMT used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018
Commented:
you say <select id="Company"....

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

http://www.w3schools.com/tags/att_select_name.asp
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I usually use both 'id' and 'name'.  JavaScript likes 'id' and the form post to PHP uses 'name'.
Moussa MokhtariEnterpreneur
Top Expert 2016
Commented:
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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 Engineer
Distinguished Expert 2018
Commented:
Choose the current selected company as default in the drop down?
(add selected to the option parameter)
Most Valuable Expert 2011
Top Expert 2016

Commented:
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 MokhtariEnterpreneur
Top Expert 2016

Commented:
Use cookies
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.

Author

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

Author

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

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial