How to get the value of the row in another table?

i am new in php and i don’t get it why i am receiving an error for undefined index in price. i actually want to get the value of the price when i am selecting a product from the select option and once i select, the price of that product will automatically shown, and once i clicked the submit, i want to see the price of that product in database, but it shows the id of my product once i clicked the submit, so meaning my product_id and price is the same.

HTML

 <form method="post" enctype="multipart/form-data">
            <div class="card-body card-block">
                  <div class="form-group">
                        <label for="product_name" class=" form-control-label">Product Name</label>
                         <select class="form-control" name="product_name" id="product_name" onchange="get_price('')"required>
                <option>Select Product Name</option>

        <?php
                $res=mysqli_query($con,"select id,name from product order by name asc");
                        while($row=mysqli_fetch_assoc($res)){
                        if($row['id']==$product_name){
                echo "<option value=".$row['id']." selected>".$row['name']."</option>";
                }else{
                echo "<option value=".$row['id'].">".$row['name']."</option>";
            }
    }
?>
</select>
</div>
        <div class="form-group">
            <label for="Price" class=" form-control-label">Price of the Product</label>
                    <select class="form-control" disabled name="price" id="price">
                        <option></option>
                            <?php
                    $res=mysqli_query($con,"select id,price from product order by name asc");
                            while($row=mysqli_fetch_assoc($res)){
                            if($row['id']==$product_name){
                echo "<option value=".$row['id']." selected>".$row['price']."</option>";
                }else{
                echo "<option value=".$row['id'].">".$row['price']."</option>";
                    }
                            }
                                ?>
                        </select>

                        </div>
                <div class="form-group">
                        <label for="Quantity" class=" form-control-label">Quantity</label>
        <input type="number" name="quantity" placeholder="Enter qty" class="form-control" required value="<?php echo $quantity?>">
                            </div>
     <button id="payment-button" name="submit" type="submit" class="btn btn-lg btn-info btn-block">
                    <span id="payment-button-amount">Submit</span>
                 </button>
                 <div class="field_error"><?php echo $msg?></div>
             </div>
        </form>

PHP

$product_name = '';
$price ='';
$quantity='';

if(isset($_POST['submit'])){
    $product_name=$_POST['product_name'];
    $price=$_POST['price']; //error
    $quantity=$_POST['quantity'];

    $res=mysqli_query($con,"select * from pos where product_id='$product_name'");
    $check=mysqli_num_rows($res);
    if($check>0){
        if(isset($_GET['id']) && $_GET['id']!=''){
            $getData=mysqli_fetch_assoc($res);
            if($id==$getData['id']){
            
            }else{
                $msg="Product is already in the form";
            }
        }else{
            $msg="Product is already in the form";
        }
    }
    
    if($msg==''){
        if(isset($_GET['id']) && $_GET['id']!=''){
            mysqli_query($con,"update pos set product_id='$product_name',qty = '$quantity' where id='$id'");
        }else{
            
            mysqli_query($con,"insert into pos(product_id,qty,price) values('$product_name','$quantity', '$price')");
        }
        header('location:pos.php');
        die();
    }
}

php

Your most immediate problem is that disabled form fields are not include in the submitted form data.

However, unless you are allowing the price to be altered, there’s no good reason to use a form field for it, just display it as content on the web page and, and using a select/option menu makes no sense in any case, since you can only select from the existing prices, not enter an arbitrary price.

yes, i already remove the disabled, and the product has existing price also, once i select on the product, the price will be shown, and i dont know why the price of that product is not showing in the database, instead the id of the product is saving in the row of price. I have a php for the on-change once the product has been selected as the price will be shown. can you check it pls?

<?php
require('connection.inc.php');
require('functions.inc.php');
if(isset($_SESSION['ADMIN_LOGIN']) && $_SESSION['ADMIN_LOGIN']!=''){

}else{
	header('location:login.php');
	die();
}

$product_name = get_safe_value($con,$_POST['name']);
$get_pr=get_safe_value($con,$_POST['get_pr']);


$res=mysqli_query($con,"select * from product where id= '$product_name'");
if(mysqli_num_rows($res)>0){
	$html='';
	while($row=mysqli_fetch_assoc($res)){
		if($get_pr==$row['id']){
			$html.="<option value=".$row['id']." selected>".$row['price']."</option>";
		
		}else{
			$html.="<option value=".$row['id'].">".$row['price']."</option>";
			
		}
	}
	echo $html;
}else{
	echo "<option value=''>No price is found </option>";
}
?>

This is my script:

	 <script>
	 
		function get_price(get_pr){//get_qty
			var product_name=jQuery('#product_name').val();
			jQuery.ajax({
				url:'get_price.php',
				type:'post',
				data:'name='+product_name+'&get_pr='+get_pr,
				success:function(result){
					jQuery('#price').html(result);
				}
			});
		}
	 </script>
Sponsor our Newsletter | Privacy Policy | Terms of Service