-1

very new to this, so I apologize in advance for rookie mistakes.

I am currently working through one of the PHP/MySQL introductory series from lynda.com. I am loving it thus far, and have been having success, but this one's got me confused.

I have created a form for user input, called new_subject.php. The following is called create_subject.php, and it meant to process the form data from new_subject.php. Please keep in mind that I have not yet added any code to do this, I am just testing a redirect function:

<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>

<?php
  if (isset($_POST['submit'])) {

  } else {
    // This is probably a GET request
    redirect_to("new_subject.php");
}
?>

 <?php
  if (isset($connection)) {mysqli_close($connection);}
?>

The point of the redirect_to function is to redirect the user to the form at new_subject.php if they were to type in create_subject.php manually in the browser. Here is what redirect_to function looks like in functions.php:

  function redirect_to($new_location) {
  header("Location: " . $new_location);
  exit;
  }

I am getting the following error when trying to get to create_subject.php manually:

Fatal error: Call to undefined function redirect_to()

The tutorial video says that I can either turn on output buffering, which I have tried to do in my php.ini file, but the error remains the same. The video says I can do that, or "fix the white space issue." I have no idea what this means.

I would appreciate any info on what this "white space issue" is, and if there is anything else I can do here. As far as I can see, I should be able to call this function without issues.

Thanks

  • Where is this function `redirect_to`? It appears that you aren't including it properly. – Blue Jun 12 '15 at 04:23
  • location header must be the first output.. have a look here http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php, or just search for location header php problem and you'll find a bunch of answers. in your case, you have a white-space before your 3rd opening php tag – Keith A Jun 12 '15 at 04:34
  • @FrankerZ redirect_to is called in functions.php – Kevin Fidyk Jun 12 '15 at 04:41
  • @KeithA thanks for taking a look... i took out the whitespace between all php tags, and still get the same error. – Kevin Fidyk Jun 12 '15 at 04:42
  • @KeithA thank you! Putting everything inside one php tag worked. – Kevin Fidyk Jun 12 '15 at 05:03

1 Answers1

0

for the whitespace issue, there must be no output prior to calling location header, in your code.. there must be no whitespace outside the php tags..

just rewrite so that all those are enclose in a single php tag and make sure there is no space outside of that, even a single one.

<?php 
require_once("../includes/db_connection.php");
require_once("../includes/functions.php");

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

  } else {
    // This is probably a GET request
    redirect_to("new_subject.php");
}

  if (isset($connection)) {mysqli_close($connection);}
?>

read here.. How to fix "Headers already sent" error in PHP

however, your main issue is the function is not found..

check your functions.php and make sure redirect_to is define properly. if that is the case, then most likely issue is in your "require_once" must be pointing to a non existent file. can you tell us your folder structure?

your functions.php must be located in a folder called includes located outside the folder where your create_subject.php is located.

if the 'includes' folder is located inside the folder where create_subject.php is, then change your require_once to

require_once("includes/db_connection.php");
require_once("includes/functions.php");

if both are located in the same folder, then change your require_once to

require_once("db_connection.php");
require_once("functions.php");
Community
  • 1
  • 1
Keith A
  • 771
  • 6
  • 12