如何从PHP的select下拉列表中选择的项目的数据库中从文本框中获取价格?

时间:2019-05-24 17:12:44

标签: php jquery ajax

如何从PHP的select下拉列表中选择的项目的数据库中获取文本框中的价格。

这是我可以运行的代码,但是它无法从数据库中获取价格,基本上我想制作this website的副本,那么如何从数据库到文本框获取特定商品的价格? / p>

    <?php
 //load_data_select.php
 $connect = mysqli_connect("localhost", "root", "", "zzz");
 function fill_brand($connect)
 {
      $output = '';
      $sql = "SELECT * FROM brand";
      $result = mysqli_query($connect, $sql);
      while($row = mysqli_fetch_array($result))
      {
           $output .= '<option value="'.$row["brand_id"].'">'.$row["brand_name"].'</option>';
      }
      return $output;
 }
 ?>

<!DOCTYPE HTML>
<html>
<!-- Copied from http://www.saifinfosystem.in/quot.php by Cyotek WebCopy 1.7.0.600, Friday, May 24, 2019, 12:17:38 PM -->
<head>
<title>Online Quotation</title>
<meta charset="utf-8">

<link href="css/main.css" rel="stylesheet" type="text/css">
<!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
    <link href="css/desktop.css" rel="stylesheet" type="text/css">
<![endif]-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>

  <script type='text/javascript' src='jsss/jquery.min.js'></script>


  <!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> -->

    <script src='order.js'></script>


</head>

<body>
<div id="backgroundStarBurst">
  <section id="wrap">
    <header class="inside">

 <!DOCTYPE HTML>
<html>
<head>
<title>Saif Infosystem</title>
<meta charset="utf-8">
<meta name="google-site-verification" content="OFt9LIenQcB2EAYM2bONz6Xcgt7567iu9BVPTdPdKh0">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="css/desktop.css">
<link rel="stylesheet" type="text/css" href="css/slit-slider.css">
<noscript>
<link rel="stylesheet" type="text/css" href="css/slider-slit-noscript.css">
</noscript>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<header>
</script>
<div class="header-form">
            <div class="suggestionsBox" id="suggestions" style="display: none;">
        <div class="suggestionList" style="line-height:2;" id="suggestionsList">  </div>
      </div>
      </div><!--header-form-->

      </article>

      </div><!--top-bar-->

       <article class="logo"><a href="index.php.html">

  <img src="logo.png"></a></article>

      </header>
</aside>
    </html></header>
    <section id="insideContainer">
      <section id="insideContent">




<style type="text/css">
<!--
A:link { COLOR: black; TEXT-DECORATION: none; font-weight: normal }
A:visited { COLOR: black; TEXT-DECORATION: none; font-weight: normal }
A:active { COLOR: black; TEXT-DECORATION: none }
A:hover { COLOR: blue; TEXT-DECORATION: none; text-decoration:underline;  }
</style>
<script type="text/javascript" language="javascript">
function validateForm()
{

   var mobile = document.getElementById("mobile").value;
        var pattern = /^\d{10}$/;
        if (pattern.test(mobile)) {

            return true;
        }
            alert("It is not valid mobile number.input 10 digits number!");
            return false;

            }
  </script>

  <script language="Javascript">

      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

   </script>
   <div class="container">
        <h3>
             <select name="brand" id="brand">
                  <option>Show All Product</option>
                  <?php echo fill_brand($connect); ?>
             </select>
             <br /><br />
             <div class="row" id="show_product">

                  <input type="hidden" name="productprice1" id="show_price" value="">
             </div>
        </h3>
   </div>
 <form name="form1" id="form" method="post" onsubmit="return validateForm()">
 <table>


 <tr>
 <th height="33">Name:</th>
 <td><input type="text" value="" name="name" required="" placeholder="Enter Name"></td>

 </tr>
 <tr>
 <th height="33">Mobile:</th>
 <td><input type="text" name="mobile" id="mobile" required="" onkeypress="return isNumberKey(event)" placeholder="Enter 10 Digit Phone No"></td>
 </tr>
 <tr>
 <th height="33">Email:</th>
 <td><input type="text" placeholder="Enter Email" required="" name="email"></td>
 </tr>
 </table>

             <table width="100%" border="1" cellpadding="2" cellspacing="0">
             <tr style="line-height:20px; font-weight:bold; background:#9EE2F1; color:#0000FF;">
                 <td width="">Name</td>
                 <td width="">Select Item</td>
                 <td width="">Quantity</td>
                 <td width="">&nbsp;&nbsp;&nbsp;Total (Rs.)</td>
            </tr>
                          <tr>
                 <td width=""><span style="font-size:14px">Processor</span></td>
                 <td width="">
                 <select name="brand" id="brand" style="width:260px">
                    <option value="">Select Processor</option>
                          <?php echo fill_brand($connect); ?>
                                         </select>
                 <input type="hidden" name="productname1" id="show_price" value="">
                  <input type="hidden" name="productprice1" id="show_price" value="">
                 </td>
                 <td width="">
                <input type="text" value="" name="qty1" id="qty1" style="width:40px;" onkeyup="checkqty(1,this.value);">
                </td>
                 <td width=""><input type="text" class="read" readonly="readonly" name="producttotalprice1" id="producttotalprice1"></td>
            </tr>
             </table>

            </form>
 <br>
</section>
      <div class="clear"></div>
    </section>
  </section>
    </div>
</body>
</html>
<script>
$(document).ready(function(){
     $('#brand').change(function(){
          var brand_id = $(this).val();
          $.ajax({
               url:"load_data.php",
               method:"POST",
               data:{brand_id:brand_id},
               success:function(data){
                    $('#show_price').php(data);
               }
          });
     });
});
</script>

load_data.php

<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "", "zzz");
$output = '';
if(isset($_POST["brand_id"]))
{
     if($_POST["brand_id"] != '')
     {
          $sql = "SELECT * FROM product WHERE brand_id = '".$_POST["brand_id"]."'";
          // $sql = "SELECT * FROM product WHERE product_id    = '".$_POST["brand_id"]."'";
     }
     else
     {
          $sql = "SELECT * FROM product";
     }
     $result = mysqli_query($connect, $sql);
     while($row = mysqli_fetch_array($result))
     {
          $output .= '<input type="hidden" name="productprice1" id="show_price" value=".$row["price"].">';
          // $output .= '<div class="col-md-3"><div style="border:1px solid #ccc; padding:20px; margin-bottom:20px;">'.$row["price"].'</div></div>';
     }
     echo $output;
}
?>

1 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点,在这种情况下,Ajax和数据属性是一种快速的方法,即数据属性方法,因此您必须这样修改函数 fill_brand

   function fill_brand($connect)
     {
          $output = '';
//first call only the fields you need
          $sql = "SELECT brand_id,brand_name,brand_price FROM brand";
          $result = mysqli_query($connect, $sql);
          while($row = mysqli_fetch_array($result))
          {
///second append a data attribute to your option element lets call it data-price
               $output .= '<option value="'.$row["brand_id"].'" data-price="'.$row["brand_price"].'">'.$row["brand_name"].'</option>';
          }
          return $output;
     }

现在要填写您的输入,创建一个更改事件:

 $('#brand').change(function(event){
  let price = $(this).data("price");
  $("#yourInputId").val(price);
});

仅此而已,希望对您有所帮助