根据选定的值打开一个带有数据库数据的模态

时间:2016-12-19 21:04:02

标签: javascript php html ajax

我是网络开发的新手,我正在尝试根据所选值从数据库中检索数据 我使用java脚本来获取所选值并发出请求

 <script>
function getselectedc () {
  var sel = document.getElementById('coursesids');
  var sv = sel.options[sel.selectedIndex].value;
  var xhttp;    
  xhttp = new XMLHttpRequest();
  xhttp.open("GET", "managerview.php?q="+sv, true);
  xhttp.send();
}
</script>

当点击按钮时,模态应该出现这是我的按钮和模态的html代码

 <button type="submit" onclick="getselectedc()" data-toggle="modal" data-target="#updateModal" class="btn btn-info btn-lg card" id="update" style="width:50%;" <?php echo $buttonenable;?> > <span class="glyphicon glyphicon-pencil"></span> Update</button>
    <br> <br>

  <!-- Modal -->
  <div class="modal fade" id="updateModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content" style="background-image:url(images/mb2.jpg);">
        <div class="modal-header" style="padding:35px 50px; ">
          <button type="button" class="close" data-dismiss="modal" >&times;</button>
          <h1 style="color:#0e5c6d;"><span class="glyphicon glyphicon-pencil bold"></span> <strong >Update Course</strong></h1>
        </div>
        <div class="modal-body" style="padding:40px 50px; ">
          <form role="form">
            <div class="form-group" style="text-align:left;"> 
              <label for="courseid">Course ID</label>
              <input type="text" class="form-control" id="corseid" disabled placeholder=<?php echo $uid;?> >
            </div>
            <div class="form-group" style="text-align:left;"> 
              <label for="noclasses">Number Of Classes</label>
              <input type="number" class="form-control" id="noclasses" placeholder=<?php echo $nclasses;?> >
            </div>
            <button type="submit" class="btn btn-info btn-block"><span class="glyphicon glyphicon-ok"></span> Update</button>
          </form>
        </div>
      </div>

    </div>
  </div>

我的PHP代码

     <?php
       $buttonenable="";
       $nhrs=0;$nclasses=0;$price=0;$tcid=0;$sname="";$uid="";
      $conn = mysqli_connect('localhost','root','','project');
      if (mysqli_connect_error()) { die("Connection to database failed: " . mysqli_connect_error());}

       $scid=$_GET["q"];
    function populatecdata()
      {
         if($GLOBALS['scid']!="")
       { $sql="SELECT * FROM COURSES WHERE ID=".$GLOBALS['scid'];
        echo $sql;
         $result=mysqli_query($GLOBALS['conn'], $sql);
       if (!$result) { die(mysqli_error($GLOBALS['conn'])); }
       else {
              $row = mysqli_fetch_assoc($result);
              $GLOBALS['uid']='"'.$GLOBALS['scid'].'"';
              $GLOBALS['nclasses']='"'.$row["NoOfClasses"].'"';
              $GLOBALS['nhrs']='"'.$row["NoOfHours"].'"';
              $GLOBALS['price']='"'.$row["Price"].'"';
              $GLOBALS['tcid']='"'.$row["TrainerID"].'"';
              $GLOBALS['sname']='"'.$row["Sportname"].'"';
            }
       }
      }


populatecdata();
function options()
{

  $x=0;
  $sql ="SELECT ID FROM COURSES ";
  $GLOBALS['result'] = mysqli_query($GLOBALS['conn'], $sql);
  $r1="block";
  if (!$GLOBALS['result']) { die(mysqli_error($GLOBALS['conn'])); }
  $a=array();
  $i=0;
  if(mysqli_num_rows($GLOBALS['result']) > 0)
  {
    while($row = mysqli_fetch_assoc($GLOBALS['result']))
      {
        echo '<option value="'.$row["ID"].'">'.$row["ID"].'</option> '."\r\n";
      }
       $GLOBALS['buttonenable']="";
  }
  else {echo '<option value="0">no courses to show</option>';
  $GLOBALS['buttonenable']="disabled";}
} 
 ?>

我在一个名为managerview.php的文件中写了所有这些 问题是当模态中出现的数据不正确时(不是数据库中的正确值) 任何帮助将提前感谢 对于这篇长篇文章感到抱歉,但我只想把一切都弄清楚

0 个答案:

没有答案