thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

hidden field in form automatically filled out #220

Open bertlietaert opened 7 years ago

bertlietaert commented 7 years ago

Love the series! I'm new to php and mysql, and use the knowledge I gain from the series to build a simple web application. Well, simple, in theory. For a dummie like me, it's quite a challenge.

I'm a guitar teacher and I would like to have an application to manage my class administration. I managed to setup a login form (if I succeed, I would like to give colleges the change to use the system), created a form to add students to the database, and to create a dynamic navigation bar based on the student ID. When I log in as another user, only the students that were created by that user are displayed.

index.php?student=1 displays student 1's data, index.php?student=2 displays student 2's data, and so on.

I would like to add a second form to insert some info about a specific lesson in a table 'lessons'. I created the table with the following fields: lesson_id (AI), user_id, student_id, lesson_content, timestamp.

I would like the user_id and student_id to be filled out automatically (I gave both the type="hidden"). I figured out how to do that for the user_id, making use of the variable $user. But how can I put the student_id from the url in the student_id field in the database.

I tried the following:

<?php 

                if(isset($_POST['submit2'])) {

                    $q = "INSERT INTO lessons (user_id, student_id, lesson_content) VALUES ('$user[uid]', '$studentid', '$_POST[lesson_content]')";
                    $r = mysqli_query($dbc, $q);
            }

            ?>

For the dynamic navigation, is wrote:

<?php

if(isset($_GET['student'])) {

    $studentid = $_GET['student'];

} else {

    $student_id = 1;
}

$q = "SELECT * FROM students WHERE sid = $student_id";
$r = mysqli_query($dbc, $q);

$student = mysqli_fetch_assoc($r);

?>

Thanks in advance!

creptor commented 7 years ago

I'm sorry I don't understand the way you want to archive this, please post code per page (whole code), so i can follow your logic.

Feel free to use https://gist.github.com/ or http://pastebin.com/

Note: You have a error on a variable in the second code, please fix with the following

if(isset($_GET['student'])) {

   $student_id= $_GET['student'];// replaced "$studentid" with "$student_id"

} else {

   $student_id = 1;
}
bertlietaert commented 7 years ago

Thanks for the quick reply! The thing is, I don't know how to achieve this. I'm afraid there might be little logic in my code. 2 weeks ago, I didn't even know what php and mysql were. I have a lot to learn.

You're right about the error. I translated some variables in my post so that they would make more sense, and I forgot to adapt that one. I'll post my code like it is now.

DB-connecion page:

<?php
#database connection here...
$dbc = mysqli_connect('localhost', 'root', '', 'dummy');
?>

Login page:

<?php 

# Start session:
session_start();

include('db.php');

if($_POST) {

    $q = "SELECT * FROM users WHERE email = '$_POST[email]' AND password = '$_POST[password]'";
    $r = mysqli_query($dbc, $q);

    if(mysqli_num_rows($r) == 1) {

        $_SESSION['username'] = $_POST['email'];
        header('Location: index.php');

    }

}
 ?>

<!DOCTYPE html>

<html>
<head>
    <title>Login</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

    <!-- jQuery CSS -->
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />

    <style>

    /* Set black background color, white text and some padding */
    footer {
      background-color: #555;
      color: white;
      padding: 15px;

   /* Remove the navbar's default margin-bottom and rounded borders */
    .navbar {
      margin-bottom: 0;
      border-radius: 0;   

  </style>

    <!-- font awesome -->
    <script src="https://use.fontawesome.com/418909d377.js"></script>

    <!--jQuery -->
    <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>

    <!--jQuery UI-->
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

</head> 
<body>
<div class="container">

    <div class="row">

        <div class="col-md-4 col-md-offset-4">

                <div class="panel panel-info">

                    <div class="panel-heading">

                            <h4><strong>Login</strong></h4>

                    </div>

                        <div class="panel-body">

                            <form action="login.php" method="post" role="form">
                              <div class="form-group">
                                <label for="email">Email address</label>
                                <input type="email" class="form-control" name="email" id="email" placeholder="Email">
                              </div>
                              <div class="form-group">
                                <label for="password">Password</label>
                                <input type="password" class="form-control" name="password" id="password" placeholder="Password">
                              </div>

                              <!--
                              <div class="checkbox">
                                <label>
                                  <input type="checkbox"> Check me out
                                </label>
                              </div>
                              -->
                              <button type="submit" class="btn btn-default">Submit</button>
                            </form>

                        </div> <!--panel body ending-->

                </div> <!--panel ending-->

        </div> <!--col ending-->

    </div> <!--row ending-->

</div> <!--container ending -->

</body> 

</html>

Setup page:

<?php

#user

$q = "SELECT * FROM users WHERE email = '$_SESSION[username]'";
$r = mysqli_query($dbc, $q);

$user = mysqli_fetch_assoc($r);

?>  

<?php

if(isset($_GET['student'])) {

    $studentid = $_GET['student'];

} else {

    $studentid = 1;
}

$q = "SELECT * FROM students WHERE sid = $studentid";
$r = mysqli_query($dbc, $q);

$student = mysqli_fetch_assoc($r);

?>

index.php:

<?php

# start the session:
session_start();

if(!isset($_SESSION['username'])) {
    header('location: login.php');
    }

?> 

<?php 

include('db.php'); 
include('setup.php');

?>

<!DOCTYPE html>

<html>
<head>
    <title>Dummie</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- Custom styles for this template -->
    <link href="sticky-footer-navbar.css" rel="stylesheet">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

    <!-- jQuery CSS -->
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <link rel="stylesheet" type="text/css" href="style.css">

    <style>

    /* Set black background color, white text and some padding */
    footer {
      background-color: #555;
      color: white;
      padding: 15px;

   /* Remove the navbar's default margin-bottom and rounded borders */
    .navbar {
      margin-bottom: 0;
      border-radius: 0;

      .list-group {

}   

      </style>

    <!-- font awesome -->
    <script src="https://use.fontawesome.com/418909d377.js"></script>

    <!--jQuery -->
    <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <!--jQuery UI-->
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

    <!--Latest compiled and minified JavaScript-->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

</head> 
<body>

<nav class="navbar navbar-default"><!--start navbar-->
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Klasadmin 1.0</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="?student=1">Home</a></li>
        <ul class="nav navbar-nav">

         <li class="dropdown">

            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Mijn Leerlingen<span class="caret"></span></a>
            <ul class="dropdown-menu">

                <?php

                if(isset($_POST['submitted']) == 1) {

                    $q = "INSERT INTO students (uid, st_first, st_last, graad) VALUES ('$user[uid]', '$_POST[st_first]', '$_POST[st_last]', '$_POST[graad]')";//uid is een waarde die uit $user gehaald wordt
                    $r = mysqli_query($dbc, $q);
            }

            ?>

                <?php

                    $q = "SELECT * FROM students WHERE uid = '$user[uid]' ORDER BY st_last ASC";//enkel velden ophalen van uid in kwestie
                    $r = mysqli_query($dbc, $q);

                    while($student_list = mysqli_fetch_assoc($r)) {

                        echo '<li><a href="?student='.$student_list['sid'].'">'.$student_list['st_first'].' '.$student_list['st_last'].'</a></li>';

                    }?> 

                    </ul>
        <li><buton style="margin-left: 20px"s type="button" class="btn btn-default navbar-btn" data-toggle="modal" data-target="#NieuweLeerling"><i class="fa fa-plus" aria-hidden="true"></i></button></li>

      </ul>

      <ul class="nav navbar-nav navbar-right" style="margin-left: 250px">

         <li class="dropdown">

            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $user['first']; ?><span class="caret"></span></a>
            <ul class="dropdown-menu">

                 <li><a href="logout.php">Logout</a></li>

            </ul>

         </li>
      </ul>

    </div>
  </div>
</nav>  <!--end of navbar-->

<!--form to add new students (popup)--->

<div class="modal" role="dialog" id="NieuweLeerling">

                <div class="modal-dialog modal-sm">
                    <div class="modal-content">

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h3 class=""modal-title">Nieuwe leerling:</h3>
                        </div><!--end of modal header-->

                    <div class="modal-body">

            <form action="index.php" method="post" role="form">

                <input type="hidden" name="uid" />
                <div class="form-group">

                    <label for="st_first">Voornaam:</label>
                    <input class="form-control" type="text" name="st_first" id="uid" placeholder="voornaam">

                </div>

                <div class="form-group">

                    <label for="st_last">Naam:</label>
                    <input class="form-control" type="text" name="st_last" id="uid" placeholder="naam">

                </div>

                <div class="form-group">

                    <label for="graad">Graad:</label>
                    <select class="form-control" name="graad" id="graad">

                        <option value="0">Geen graad</option>

                        <?php

                            $q = "SELECT * FROM graden ORDER BY graad_id ASC";
                            $r = mysqli_query($dbc, $q);

                            while($graad_list = mysqli_fetch_assoc($r)) {

                                ?>

                                <option value="<?php echo $graad_list['graad']; ?>"><?php echo $graad_list['graad']; ?></option>
                        <?php   } ?>

                    </select>

                </div>

                <button type="submit" class="btn btn-warning">Opslaan</button>
                <input type="hidden" name="submitted" value="1" /></br>

            </form>
            </div>

            <div class="modal-footer">
            <a href="" class="btn btn-default" data-dismiss="modal">Sluiten</a>

            </div>  <!--end of modal footer-->
            </div><!--end of modal content-->
            </div><!--end of modal dialog-->

            </div><!--end of modal-->

<div class="container"><!--main body-->

    <div class="row">

        <!--attempt to send results of the 'new lesson form' to db. This is where I have a problem. No idea how to automatically fill out the sid (that is in the url.)-->

        <?php 

                if(isset($_POST['submit2'])) {

                    $q = "INSERT INTO les (uid, sid, lesbeschrijving) VALUES ('$user[uid]', '$studentid', '$_POST[lesbeschrijving]')";//uid is een waarde die uit $user gehaald wordt
                    $r = mysqli_query($dbc, $q);
            }

            ?>

        <!--here some info about the selected student is loaded-->
        <div class="col-md-6">

            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
            <div class="panel-heading" role="tab" id="headingTwo">
            <h4 class="panel-title">
             <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
            <h3><?php echo $student['st_first'].' '.$student['st_last']; ?></h3>
            </a>
            </h4>
             </div>
             <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
              <div class="panel-body">
              <p><?php echo $student['graad'].' '.$student['filiaal']; ?></p>
             </div>
            </div>
             </div>

            <!--new lesson form-->

            <form action="index.php" method="post" role="form">

                <input type="hidden" name="uid" />
                <input type="hidden" name="sid" />

                <div class="form-group">

                <label for="les">Les inhoud:</label>
                <textarea class="form-control" rows="5" name="lesbeschrijving" id="les"></textarea>

                </div>

                <button type="submit" name="submit2" class="btn btn-warning">Opslaan</button>

            </form>

        </div><!--end of main rechterkant-->

        <div class="col-md-6">
            <!--<div><h3>Vorige les</h3></div>-->

        </div>

</div><!--end of row-->

</div><!--end of main body container-->

<footer class="footer">
    <div class="container">
 <center><p style="margin-top: 4px;" class="text-muted">Footer text</p></center>
 </div>
</footer>

</body> 

</html>
creptor commented 7 years ago

I'm sorry that I'm asking this but I need to know if you're using has base the site that is here, or trying to create a new one from scratch?? -- It's important to know, some things are missing from the full series

Also, is that the backend (administrator setting and stuff), or the front end??

What kind of database are you using?? (mySQL is the basic one for almost every server)

creptor commented 7 years ago

And most importantly, are you going to use clean URLs??