Avatar of kamal_g
kamal_g
 asked on

Multi field selection in php

I have a product table with product code, description,selling price, cost,category etc. I have a drop down to select the product which also shows the description and the product price.
I would like to capture the product price and show/update it in a field when ever the user selects a product. on change the field should also change with the new value. How can I achieve this. I have attached my code for your kind perusal.
include('function/db_connect.php');
//session_start();
$price = Null;
//$items = Null;
if($_POST)
{
$var1 = $_POST['country'];
$var2 = $_POST['type'];
 if ($var1 != '') {        
       echo "<label>Item<span style='color:red'>*</span></label><span style='color:red'></span></label><span class='address'>";
       echo "<select id='items' name='items' style = 'width: 546px;' onChange='get_price(this.value)'>";          
       $sql = "SELECT * FROM tbl_product WHERE country_id = '$var1' AND type_id = '$var2'";  
       $db  = new DB_CONNECT();    
       $result = mysql_query($sql);
       $myarray = array();
       echo "<option value=''>Select</option>";
       while ($row = mysql_fetch_array($result)) {
            $idp = $row["product_id"];
            $iddes = $row["product_desc"];
            $selp = $row["product_sell"];
            $costp = $row["product_cost"];


       echo "<option value='" . $idp . "'>" . $iddes . "==>".$selp ."</option>";

       }
       echo "</select><label>Item</label></span><span class='address'>";

    }  
       echo "</div>";        
       echo "<br>";    



       echo "<div><label></label><span class='name'><button name = 'data'  type ='button' onclick= 'valprice('items')'>Validate value</button></span></div>";      

    }
?>
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ray Paseur

Hell, @kamal_g and welcome to E-E.

To the instant question, it might be helpful to see your table schema.  It looks like that code queries a database and creates an HTML document with a <select> control.  This would allow a client to choose one of the <option> values.  It looks like there is a JavaScript function named get_price() that will be invoked when the <select> changes.  This function would need to have some knowledge of the price information that is in the database.  So the complete HTML document, and the JavaScript are useful things, like your database schema, that we need to see.

If you're new to PHP and want to get a foundation in how the language works, this article can help you find good learning resources and more importantly, avoid badly written examples of obsolete or dangerous PHP code.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

In a related matter, the code here uses the MySQL extension.  PHP is doing away with the MySQL extension, so you want to change to MySQLi or PDO as soon as possible.  This article explains why PHP is making this change and what you must do to keep your scripts running in the future.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
ASKER CERTIFIED SOLUTION
Eddie Shipman

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Julian Hansen

THIS SOLUTION 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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck