Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Need to post data to different variable pages

How do i post the variable to another page, so then it looks at the variable and redirects them to the right page.

<?php 
session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");

// Require: use your own path
require_once 'morris.php';
require_once 'morris-charts.php';

// Optional: include chart line
require_once 'morris-line-charts.php';
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); } 

//define page title
$title = "Predict a Student's Grade";

?>
<!DOCTYPE html>
<html>



<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Past Student</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">




    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <script src="js/bootstrap.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
     <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>

</head>

<body>




<?php 

    //posted form data
    if (isset($_POST['name'])) {


        $name     = $_POST['name'];
        $subject1 = $_POST['subject1'];
        $grade1   = $_POST['grade1']; 
        $subject2 = $_POST['subject2'];
        $grade2   = $_POST['grade2'];
        $subject3 = $_POST['subject3'];
        $grade3   = $_POST['grade3'];
        $subject4 = $_POST['subject4'];
        $grade4   = $_POST['grade4'];
        $attendance = $_POST['attendance'];
        $gender     = $_POST['gender'];

          // <--------------Validation for form Post----------------------->




        $error = true;

        //validation make sure  2 of the same subjects haven't been chosen 
        if ($subject1 == $subject2 or $subject1 ==  $subject3 or  $subject1 == $subject4) {
           $error = false;
          } 
        elseif ($subject2 == $subject1 or $subject2 == $subject3 or $subject2 == $subject4) {
          $error = false;

          }
        elseif ($subject3 == $subject1 or $subject3 == $subject2 or $subject3 == $subject4) {
             $error = false;
            }
            // checks if the data entered is an integer 

      elseif ( is_int( $attendance ) && $attendance >= 0 && $attendance <= 100 ) {
      $error = false; // If the above condition is met, $error is set to false
    }



  if ($error == true){ 



      //selecting from the database

       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1");
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->execute();
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result

    // Output the predicted grade (or any item from the array) like this

    }
    else {
        header("location:error.php");
    }
    }?>

<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="jumbotron">

                <h1>

                  <?php 
                      if (empty($result['final_maths'])) {
                        header('location:error.html');
                       }
                       else {
                           echo "<h1>The predicted grade for " . $name . " is a " . $result['final_maths'] . "</h1>";
                                $grade = $result['final_maths'];
                                $_SESSION['predict'] = $grade;
                            }
                        ?>
                                           <!-- Button -->
                        <form action="sort.php " method="POST">

                       <input type="submit" name="graph" value ="graph"/>
                       </form>



                <p>
                    Please remember that these predictions does not mean the student will get this grade
                    <br>
                    <i> "It always seems impossible until it is done" </i>

                </p>


                <a class="btn btn-primary btn-lg btn-block" href="memberpage.php" role="button">Home  &#x2302;</a>

            </div> 
        </div>
    </div>
    </form>
    </fieldset>
</div>


</body>
</html>

There is a sort page that redirects to user to the right page when they click submit

<?php 
session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");
$grade = $_SESSION['graph'];

// Require: use your own path
require_once 'morris.php';
require_once 'morris-charts.php';

// Optional: include chart line
require_once 'morris-line-charts.php';
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); } 

//define page title
$title = "Predict a Student's Grade";

if ($grade == "A") {

    header("location:graphaa.html");
}





?>