PHP动态列表检索返回未定义索引

时间:2019-04-19 23:07:45

标签: javascript php jquery dynamic-list

我一直在作为个人练习的一部分进行测试和玩耍,但是,我陷入了以下要通过JavaScript添加项目列表的问题,提交后,我想验证然后存储添加的内容清单。但是,一旦提交,我将得到一个错误的Unidentified索引,好像该值未按isset函数检查的那样提交,并且始终返回空值。在进一步查看StackOverflow之后,我注意到有些引用HTML结构。因此,我将HTML最小化为如下所示:

<!doctype html>
<html lang="en">


<head>
   <!-- Required meta tags -->
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <!-- Bootstrap CSS -->
   <link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.min.css">
   <link href="assets/vendor/fonts/circular-std/style.css" rel="stylesheet">
     <link rel="stylesheet" href="assets/libs/css/customeStyle.css">
   <link rel="stylesheet" href="assets/libs/css/style.css">
   <link rel="stylesheet" href="assets/vendor/fonts/fontawesome/css/fontawesome-all.css">
   <title>Concept - Bootstrap 4 Admin Dashboard Template</title>

</head>

<body>
   <!-- ============================================================== -->
   <!-- main wrapper -->
   <!-- ============================================================== -->
       <?php //include("navBar.php"); ?> 
       <!-- ============================================================== -->
       <!-- left sidebar -->
       <!-- ============================================================== -->
       <?php //include("SlideBar.php"); ?>
       <!-- ============================================================== -->
       <!-- end left sidebar -->
       <!-- ============================================================== -->
       <!-- ============================================================== -->
       <?php
         if(isset($_POST["submit"])){
           //$test = $_POST["patName"];
           if(isset($_POST["medAdded0"])){
              //echo $value;
             $listName= $_POST["medAdded0"];
             echo '<script language="javascript">';
             echo 'alert("'.$listName.'")';
             echo '</script>';
             echo $listName;
           }else {
              //echo $value;
             echo '<script language="javascript">';
             echo 'alert("still empty")';
             echo '</script>';
             echo $_POST["medAdded0"];
           }
         }
       ?>
    <form action="#" method="POST">
      <div class="row">
        <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-12">
                  <div id="medListContainer" class="form-group">

                  </div>
          </div>
        </div>
        <div class="col-lg-4">
            <div class="card">
              <h5 class="card-header">List of Medication</h5>
              <div class="card-body">
                    <div class="list-group" id="pillsList">  
                      <button  class="list-group-item list-group-item-action" value="noor">Dapibus ac facilisis in</button>
                      <button class="list-group-item list-group-item-action" value="btn2">Morbi leo risus</button>
                      <button  class="list-group-item list-group-item-action" value="btn3">Porta ac consectetur ac</button>

                    </div>
              </div>
            </div>
          </div>

           <input class="btn btn-success" type="submit" name="submit" value="Save">
        </form>

   <!-- Optional JavaScript -->
   <!-- jquery 3.3.1  -->
   <script src="assets/vendor/jquery/jquery-3.3.1.min.js"></script>
   <!-- bootstap bundle js -->
   <script src="assets/vendor/bootstrap/js/bootstrap.bundle.js"></script>
   <!-- slimscroll js -->
   <script src="assets/vendor/slimscroll/jquery.slimscroll.js"></script>
   <!-- main js -->
   <script src="assets/libs/js/main-js.js"></script>
  <script>
     var indexCounter=0;
     $('#pillsList').on('click', function (e) {
       e.preventDefault();
       var targetList = document.getElementById("medListContainer");
       var medValue= e.target.value;
       if (indexCounter==10) {
         //code
         alert("you've exceeded your  limit, please generate new ");
       }else{
         addMedList(medValue,targetList);  
       }
     });

     function addMedList(BtnValue,targetList) {
       //function to place the list of selected medicaition
       var btn = document.createElement("INPUT");
       btn.innerHTML= BtnValue;
       btn.className = "list-group-item list-group-item-action";
       btn.setAttribute("value", BtnValue);

       btn.setAttribute("id", 'medAdded' + indexCounter);
       btn.setAttribute("name", 'medAdded' + indexCounter);
       indexCounter++;
       btn.setAttribute("type", "button");
       targetList.appendChild(btn);
     }
  </script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

将其替换并重试

<!doctype html>
<html lang="en">


<head>
   <!-- Required meta tags -->
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <!-- Bootstrap CSS -->
   <link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.min.css">
   <link href="assets/vendor/fonts/circular-std/style.css" rel="stylesheet">
     <link rel="stylesheet" href="assets/libs/css/customeStyle.css">
   <link rel="stylesheet" href="assets/libs/css/style.css">
   <link rel="stylesheet" href="assets/vendor/fonts/fontawesome/css/fontawesome-all.css">
   <title>Concept - Bootstrap 4 Admin Dashboard Template</title>

</head>

<body>
   <!-- ============================================================== -->
   <!-- main wrapper -->
   <!-- ============================================================== -->
       <?php //include("navBar.php"); ?> 
       <!-- ============================================================== -->
       <!-- left sidebar -->
       <!-- ============================================================== -->
       <?php //include("SlideBar.php"); ?>
       <!-- ============================================================== -->
       <!-- end left sidebar -->
       <!-- ============================================================== -->
       <!-- ============================================================== -->
       <?php
         if(isset($_POST["submit"])){
           //$test = $_POST["patName"];
           if(isset($_POST["medAdded0"])){
              //echo $value;
             $listName= $_POST["medAdded0"];
             echo '<script language="javascript">';
             echo 'alert("'.$listName.'")';
             echo '</script>';
             echo $listName;
           }else {
              //echo $value;
             echo '<script language="javascript">';
             echo 'alert("still empty")';
             echo '</script>';

           }
         }
       ?>
    <form action="#" method="POST">
      <div class="row">
        <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-12">
                  <div id="medListContainer" class="form-group">

                  </div>
          </div>
        </div>
        <div class="col-lg-4">
            <div class="card">
              <h5 class="card-header">List of Medication</h5>
              <div class="card-body">
                    <div class="list-group" id="pillsList">  
                      <button  class="list-group-item list-group-item-action" value="noor">Dapibus ac facilisis in</button>
                      <button class="list-group-item list-group-item-action" value="btn2">Morbi leo risus</button>
                      <button  class="list-group-item list-group-item-action" value="btn3">Porta ac consectetur ac</button>

                    </div>
              </div>
            </div>
          </div>

           <input class="btn btn-success" type="submit" name="submit" value="Save">
        </form>

   <!-- Optional JavaScript -->
   <!-- jquery 3.3.1  -->
   <script src="assets/vendor/jquery/jquery-3.3.1.min.js"></script>
   <!-- bootstap bundle js -->
   <script src="assets/vendor/bootstrap/js/bootstrap.bundle.js"></script>
   <!-- slimscroll js -->
   <script src="assets/vendor/slimscroll/jquery.slimscroll.js"></script>
   <!-- main js -->
   <script src="assets/libs/js/main-js.js"></script>
  <script>
     var indexCounter=0;
     $('.list-group-item').on('click', function (e) {
       e.preventDefault();
       var targetList = document.getElementById("medListContainer");
       var medValue= $(this).val();
       if (indexCounter==10) {
         //code
         alert("you've exceeded your  limit, please generate new ");
       }else{
         addMedList(medValue,targetList);  
       }
     });

     function addMedList(BtnValue,targetList) {
       //function to place the list of selected medicaition
       var btn = document.createElement("INPUT");
       btn.innerHTML= BtnValue;
       btn.className = "list-group-item list-group-item-action";
       btn.setAttribute("value", BtnValue);

       btn.setAttribute("id", 'medAdded' + indexCounter);
       btn.setAttribute("name", 'medAdded' + indexCounter);
       indexCounter++;
       btn.setAttribute("type", "button");
       targetList.appendChild(btn);
     }
  </script>

</body>
</html>

答案 1 :(得分:0)

您写了:

       if(isset($_POST["medAdded0"])){
          //echo $value;
         $listName= $_POST["medAdded0"];
         echo '<script language="javascript">';
         echo 'alert("'.$listName.'")';
         echo '</script>';
         echo $listName;
       }else {
          //echo $value;
         echo '<script language="javascript">';
         echo 'alert("still empty")';
         echo '</script>';
         echo $_POST["medAdded0"];
       }

未设置else时执行$_POST["medAdded0"]块。因此尝试在此处回显是没有意义的。摆脱困境

         echo $_POST["medAdded0"];