Avatar of kanaMT
kanaMT

asked on 

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
PHPHTMLWeb DevelopmentJavaScript

Avatar of undefined
Last Comment
kanaMT
SOLUTION
Avatar of noci
noci

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I usually use both 'id' and 'name'.  JavaScript likes 'id' and the form post to PHP uses 'name'.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of kanaMT
kanaMT

ASKER

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.
SOLUTION
Avatar of noci
noci

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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).
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Use cookies
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of kanaMT
kanaMT

ASKER

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

Avatar of kanaMT
kanaMT

ASKER

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

Regards
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo