0

i am new to web development and i am trying to make a modal with data retrieved from data base depending on a selected value i used java script to get the selected value and to make the request

 <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>

when button is clicked modal should appear this is my html code for the buttton and modal

 <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>

my php code

     <?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";}
} 
 ?>

i wrote all of this in one file named managerview.php the problem is when the modal appear data in it isn't correct (not the correct values from database) any help would be appreciatedthanks in advance sorry for the long post but i was only trying to make everything clear

Marim
  • 11
  • 3
  • 1
    *data in it isn't correct (not the correct values from database)* are you saying it's picking up the wrong record from the table or not picking any record at all? – Searching Dec 19 '16 at 21:38
  • Your code is *wide open to SQL injection attacks*. See http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – random_user_name Dec 19 '16 at 22:18
  • @cale_b thank you i will check this – Marim Dec 20 '16 at 20:48
  • @Searching it is not picking any record at all – Marim Dec 20 '16 at 20:50
  • In that case I recommend debug the query parameter `?sv`, `select` statement, connection to your database, permission etc.. I would probably start with simple `SELECT * FROM COURSES WHERE ID = 1` statement with a known course `id` and see if the records are loaded and then check `.$GLOBALS['scid']` value and so on... – Searching Dec 20 '16 at 21:10
  • @Searching i tried to retrieve data from my database and this worked it seems that the problem is in the code i used to retrieve data for the modal – Marim Dec 21 '16 at 04:17

0 Answers0