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

Pontus Bolmér
Pontus Bolmér
12,471 Points

Header wont work

Trying to get the header to redirect me to project_list.php but i simply wont.

<?php

function get_project_list() { include "connection.php"; try{ return $db->query('SELECT project_id, title, category FROM projects'); } catch(Exception $e) { echo "Error!: ". $e->getMessage() . "</br>"; return array(); } }

function add_project($title, $category) { include "connection.php";
$sql = "INSERT INTO projects(title,category) VALUES (?,?)";

try { $results = $db->prepare($sql); $resullts->bindValue(1,$title,PDO::PARAM_STR); $resullts->bindValue(2,$category,PDO::PARAM_STR); $results->execute(); } catch (Exception $e) { echo "Error!:" .$e->getMessage() . "<br />"; return false; } return true; }

This is the functions.

And this is where i call it

require 'inc/functions.php';

$pageTitle = "Project | Time Tracker"; $page = "projects";

if($_SERVER['REQUEST_METHOD'] == "POST") { $title = trim(filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING)); $category = trim(filter_input(INPUT_POST, 'category', FILTER_SANITIZE_STRING));

if(empty($title) || empty($category)) { $error_message = "PLEASE FILLE IN THE REQUIERED FIELDS: TITLE AND CATEGORY"; } else { if(add_project($title,$category)) { header("Location:project_list.php"); exit; } else { $error_message = "Could not add project"; } }

}

include 'inc/header.php';

12 Answers

Hey! in order to make your header work, you must not have any "echo" in the page itself(the page where the header is). as I can see you do have some "echo" there.

also, if you'll close up the php page by ?> the echo won't work either (I found it out recently).

Have a good one! -Shai.

Pontus Bolmér
Pontus Bolmér
12,471 Points

I removed the ecos, it still wont work for me :S

did you checked that the end of the file doesn't have ?>
?

Pontus Bolmér
Pontus Bolmér
12,471 Points

Hi! Yes i have :S I cant really see any errors in the code either.

can you give me a screenshot of the code?

wait, this whole thing is kind of a mess, all this code is inside <html>? or this is exactly what's on the page?

Pontus Bolmér
Pontus Bolmér
12,471 Points
   <?php require 'inc/functions.php';

$pageTitle = "Project | Time Tracker"; $page = "projects";

if($SERVER['REQUEST_METHOD'] == "POST") { $title = trim(filter_input(INPUT_POST, 'title', FILTER_SANITIZESTRING)); $category = trim(filter_input(INPUT_POST, 'category', FILTER_SANITIZE_STRING));

if(empty($title) || empty($category)) { $error_message = "PLEASE FILLE IN THE REQUIERED FIELDS: TITLE AND CATEGORY"; } else { if(add_project($title,$category)) { header("Location:project_list.php"); exit; } else { $error_message = "Could not add project"; } }

}

include 'inc/header.php';

<div class="section page"> <div class="col-container page-container"> <div class="col col-70-md col-60-lg col-center"> <h1 class="actions-header">Add Project</h1>   

  <form class="form-container form-add" method="post" action="project.php">
            <table>
                <tr>
                    <th><label for="title">Title<span class="required">*</span></label></th>
                    <td><input type="text" id="title" name="title" value="" /></td>
                </tr>
                <tr>
                    <th><label for="category">Category<span class="required">*</span></label></th>
                    <td><select id="category" name="category">
                            <option value="">Select One</option>
                            <option value="Billable">Billable</option>
                            <option value="Charity">Charity</option>
                            <option value="Personal">Personal</option>
                    </select></td>
                </tr>
            </table>
            <input class="button button--primary button--topic-php" type="submit" value="Submit" />
        </form>
    </div>
</div>

Sorry about the last one.

This is all the code in one page, first the php then the html form.

Also sorry about the last mess!

hey, please answer my last question^

Pontus Bolmér
Pontus Bolmér
12,471 Points

THis is exactly whats on the page :P

umm how do you expect from this part to work?

<div class="section page"> <div class="col-container page-container"> <div class="col col-70-md col-60-lg col-center"> <h1 class="actions-header">Add Project</h1>

<form class="form-container form-add" method="post" action="project.php"> <table> <tr> <th><label for="title">Title<span class="required"></span></label></th> <td><input type="text" id="title" name="title" value="" /></td> </tr> <tr> <th><label for="category">Category<span class="required"></span></label></th> <td><select id="category" name="category"> <option value="">Select One</option> <option value="Billable">Billable</option> <option value="Charity">Charity</option> <option value="Personal">Personal</option> </select></td> </tr> </table> <input class="button button--primary button--topic-php" type="submit" value="Submit" /> </form> </div> </div>

Pontus Bolmér
Pontus Bolmér
12,471 Points

Im not sure what you mean, this is the code i get from treehouse, i have not really changed anything.

the code I quoted to you, must be inside <html> tags in order to work.

Pontus Bolmér
Pontus Bolmér
12,471 Points

It is, it's the code i got from the start from Treehouse.