繁体   English   中英

PHP | $_POST 无法从下拉列表中读取值

[英]PHP | $_POST failing to read value from a dropdown

我是 php 的新手,所以请不要介意这是否微不足道。

我正在尝试使用 php 和 mysql 构建一些 web 页面。该表单包含几个下拉列表和一个文本输入。 执行提交(POST)操作时,我试图将值插入 mysql 数据库中。 令人惊讶的是,我能够为几个变量($doctor、$docFees)获取填充值,但其中一个变量遗漏了($spec),因此创建了一个空白条目。 尝试过调试,但多一双新鲜的眼睛肯定会有所帮助。 这是我尝试使用的代码库..


if(isset($_POST['buy-submit']))
{
  $pid = $_SESSION['pid'];
  $username = $_SESSION['username'];
  $email = $_SESSION['email'];
  $fname = $_SESSION['fname'];
  $lname = $_SESSION['lname'];
  $gender = $_SESSION['gender'];
  $contact = $_SESSION['contact'];
  $doctor=$_POST['doctor'];
  $spec=$_POST['spec'];
  $email=$_SESSION['email'];
  $docFees=$_POST['docFees'];

  $appdate=$_POST['appdate'];
  $apptime=$_POST['apptime'];
  $cur_date = date("Y-m-d");
  date_default_timezone_set('Asia/Kolkata');
  $cur_time = date("H:i:s");
  $apptime1 = strtotime($apptime);
  $appdate1 = strtotime($appdate);


  if($docFees>0){
      //$check_query = mysqli_query($con,"select apptime from appointmenttb where doctor='$doctor' and appdate='$appdate' and apptime='$apptime'");
      $check_query = mysqli_query($con,"select count(*) from med_inv where med_name='$spec'  and quantity>'$docFees'");


        if(mysqli_num_rows($check_query)==1){
          $query=mysqli_query($con,"insert into med_sale(med_name,quantity) values('$med_name','$docFees')");
          if($query)
          {
            echo "<script>alert('Your order is  successfully placed');</script>";
          }
          else{
            echo "<script>alert('Unable to process your request. Please try again!');</script>";
          }
      }
      else{
          echo "<script>alert($doctor);</script>";
          echo "<script>alert($docFees);</script>";
          echo "<script>alert('Requested Quantity Not available in STOCK!! SORRY! ');</script>";
      }
  }
  else{
      echo "<script>alert('Invlaid Quantity specified!!');</script>";
  }
}
      <div class="tab-pane fade" id="list-home" role="tabpanel" aria-labelledby="list-home-list">
        <div class="container-fluid">
          <div class="card">
            <div class="card-body">
              <center><h4>Order a Medicine</h4></center><br>
              <form class="form-group" method="post" action="admin-panel.php">
                <div class="row">

                    <div class="col-md-4">
                          <label for="spec">Medicine Name :</label>
                        </div>
                        <div class="col-md-8">
                          <select name="spec" class="form-control" id="spec" required="required">
                              <option value="" </option>
                              <?php
                               display_meds();
                              ?>
                          </select>
                        </div>

                        <br><br>

                        <script>
                      document.getElementById('spec').onchange = function foo() {
                        let spec = this.value;   
                        console.log(spec)
                        document.getElementbyID('doctor').value='';     
                        let docs = [...document.getElementById('doctor').options];
                        
                        docs.forEach((el, ind, arr)=>{
                          arr[ind].setAttribute("style","");
                          if (el.getAttribute("data-spec") != spec ) {
                            arr[ind].setAttribute("style","display: none");
                          }
                        });
                      };

                  </script>

              <div class="col-md-4"><label for="doctor">Price:</label></div>
                <div class="col-md-8">
                <select name="doctor" class="form-control" id="doctor" required="required">
                      <option value="" disabled selected></option>

                      <?php display_xdocs(); ?>
                    </select>
                  </div><br/><br/>


                        <script>
              document.getElementById('doctor').onchange = function updateFees(e) {
                var selection = document.querySelector(`[value=${this.value}]`).getAttribute('data-value');
                document.getElementById('docFees').value = selection;
              };
            </script>



                  <div class="col-md-4"><label for="consultancyfees">
                                Quantity
                              </label></div>
                              <div class="col-md-8">
                              <!-- <div id="docFees">Select a doctor</div> -->
                              <input class="form-control" type="text" name="docFees" id="docFees" required="required"/>
                  </div><br><br>


                  <div class="col-md-4">
                    <input type="submit" name="buy-submit" value="Purchase Medicine" class="btn btn-primary" id="inputbtn">
                  </div>
                  <div class="col-md-8"></div>
                </div>
              </form>
            </div>
          </div>
        </div><br>
      </div>

使用的函数


function display_meds() {
  global $con;
  $query="select distinct(med_name) from med_inv";
  $result=mysqli_query($con,$query);
  while($row=mysqli_fetch_array($result))
  {
   $med_name=$row['med_name'];
   echo '<option data-value="'.$med_name.'">'.$med_name.'</option>';
  }
}

function display_xdocs()
{
 global $con;
 $query = "select * from med_inv";
 $result = mysqli_query($con,$query);
 while( $row = mysqli_fetch_array($result) )
 {
  $username = $row['mrp'];
  $price = $row['mrp'];
  $spec = $row['med_name'];
  echo '<option value="' .$username. '" data-value="'.$price.'" data-spec="'.$spec.'">'.$username.'</option>';
 }
}

值插入到表 med_sale 中。 第一个条目是出于测试目的的手动输入。 很明显 $spec 值不是从 POST 方法中读取的。

mysql> select * from med_sale;
+----------+----------+
| med_name | quantity |
+----------+----------+
| test     |        5 |
|          |        5 |
|          |        7 |
|          |      700 |
|          |        4 |
|          |       33 |
+----------+----------+
6 rows in set (0.00 sec)

两个问题。 首先无效 HTML,缺少 >:

<option value="" </option>

其次,在您的 display_meds function数据值中应该只是value ,如下所示:

echo '<option value="'.$med_name.'">'.$med_name.'</option>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM