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

My HTML isn't showing up if I have php before it.

Working on the Stripe API demo and can't get my html to show up on my index.php file.

Hi Ron,

Typically, when that happens, there is something wrong with the php code.

Jeff

5 Answers

Here is my code.

<?php require_once('./config.php'); ?>

<form action="/charge" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_6kKcDK5MuAasFL210X1C3w4v"
    data-image="/test.png"
    data-name="Testing Stripe Site"
    data-description="2 widgets ($20.00)"
    data-amount="8000">
  </script>
</form>

If the two files are in the same directory change your require to:

<?php require_once('config.php'); ?>

Also, the code in the config file has to be correct.

This is the most likely bug - if there's a syntax error in config.php, index.php will not be able to complete. Remember php is a server side language - it creates the html on the server before sending it to the user. When you name a file .php, this tells the server it has to decipher your code to return html. If it can't create the html (i.e. if you have a syntax error in your php and it doesn't know what to do with the file) you won't see any output on the screen at all. When you exclude the require_once file, you remove the error, which is why the html is successfully outputted to the screen.

You could send us your config.php file if you haven't already found the bug. Watch out for those semi-colons!

Hey Tom,

here's my code:

<?php
require_once('../lib/Stripe.php');

$stripe = array(
  "secret_key"      => "sk_test_LU5Jd71HZF47huDRYS7Xz6Nr",
  "publishable_key" => "pk_test_6kKcDK5MuAasFL210X1C3w4v "
);

Stripe::setApiKey($stripe['secret_key']);
?>

is this file:

<?php

require_once('../lib/Stripe.php');

?>

in the right place? Your syntax looks okay to me :) If you're not looking in the right place for that file, you will break the page. When you require something, you're telling php that your code won't work without this file. it's 'required'! So if php can't find the file, it won't execute the script.

What's your folder structure like?

I'm a little rusty on this area but I'm pretty sure includes/requires etc are relative to the file you're current in.

Start with some test code.

Step 1: Add this code to the top of config.php

<?php
echo "I'm in config.php";
exit;
 ?>

All being well, you should see the test code on the screen. If you don't then you're not looking in the right place for config.php.

Once you get this test to pass, do exactly the same for Stripe.php. Step 2: At the top of Stripe.php,

<?php
echo "I'm in stripe.php";
exit;
 ?>

you should see this error message too. if you don't see it, you're not looking in the right place for Stripe.php.

If you want to make really sure nothing else is causing your error, remove or comment out all other code, leaving only the require_once statements.

Let us know how you do

My guess is it's you require_once path. Where is the config.php file located and where is the form file located?

Hey Jeff,

Thanks for the reply, the config.php file is in the same directory as the index.php, the funny thing is that if I remove the php code from the top of that file, the html form works just fine.

Ron,

For starters I put Tom's echo statements in the config and Stripe files. No matter what I do the echo statement at the top of Stripe.php is never displayed. I also put

<input type="text" name="temp" id="temp" value="Temp">

at the bottom of the form below the script. It seems to appear and not appear along with the button.

If I comment require('../lib/Stripe.php'); echo "I'm in config.php"; will show; otherwise nothing. If I comment Stripe::setApiKey($stripe['secret_key']); as well as require then the button will appear.

Now if I change require('../lib/Stripe.php'); to include I get the echo statement from the config file. If I leave include and comment Stripe::setApiKey I get the button as well as the echo statement from config.php. No matter what I do I never receive the echo statement from Stripe.php. It seems this file is never being processed.

I'm going to go out on a limb and suggest there is a problem with the secret_key.

Jeff