I'm reading Headfirst PHP & MYSQL.
I did what the book asked me to do, and this is the output in the browser:

Aliens Abducted Me - Report an Abduction
'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '
'; echo 'Describe them' . $alien_description . '
'; echo 'Was fang there?' . $fang_spotted . '
'; echo 'Your email address is' . $email; ?>

This is the code for the html file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

  <p>Share your story of alien abduction:</p>
  <form action = "report.php" method = "post">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" id="howlong" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
    No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
      alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea id="other" name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
  </form>
</body>
</html>

This is the code for the .php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

<?
phpinfo();
?>
</body>
</html>

I'm wondering if it isn't a PHP installation error or something on my part.
I'm running Windows 7 64 bit.
Please help, I really want to get the hang of PHP!

Recommended Answers

All 12 Replies

EDIT:
Sorry guys, here's the code for the PHP file I made a mistake.

<html>
 <head>
<title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<?php

$when_it_happened = $_POST['$when_it_happened'];
$how_long = $_POST['$how_long'];
$alien_description = $_POST['$how_long'];
$fang_spotted = $_POST['$fang_spotted'];
$email = $_POST['email'];

echo 'Thanks for submitting the form.<br/>';
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br/>';
echo 'Describe them' . $alien_description . '<br/>';
echo 'Was fang there?' . $fang_spotted . '<br/>';
echo 'Your email address is' . $email;
?>

</body>
</html>
Member Avatar for diafol

You still got a problem?

Does your php module have a name that ends in .php? Is your localhost running when you try this. If so, what did you install for a LAMP stack? Did you run some sort of simple "hello World" php program at the start to verify that your installation was functioning properly?

Member Avatar for Zagga

Try removing the dollar signs when collecting the form data. And try to use the same field names in both pages.
For example, change $when_it_happened = $_POST['[B]$[/B]when[B]_[/B]it[B]_[/B]happened']; to $when_it_happened = $_POST['whenithappened']; Zagga

Does your php module have a name that ends in .php? Is your localhost running when you try this. If so, what did you install for a LAMP stack? Did you run some sort of simple "hello World" php program at the start to verify that your installation was functioning properly?

Not quite sure what you mean. My localhost is functioning, and what's a LAMP stack?
I never tried running anything before this.

@Zagga

Thanks, I actually didn't notice my errors..
however, the problem still persists.
Here is my new code:
html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

  <p>Share your story of alien abduction:</p>
  <form method="post" action="report2.php">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" id="howlong" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
    No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
      alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea id="other" name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
  </form>
</body>
</html>

PHP file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

<?php
  $name = $_POST['firstname'] . ' ' . $_POST['lastname'];
  $when_it_happened = $_POST['whenithappened'];
  $how_long = $_POST['howlong'];
  $how_many = $_POST['howmany'];
  $alien_description = $_POST['aliendescription'];
  $what_they_did = $_POST['whattheydid'];
  $fang_spotted = $_POST['fangspotted'];
  $email = $_POST['email'];
  $other = $_POST['other'];

  $to = 'owen@aliensabductedme.com';
  $subject = 'Aliens Abducted Me - Abduction Report';
  $msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
    "Number of aliens: $how_many\n" .
    "Alien description: $alien_description\n" .
    "What they did: $what_they_did\n" .
    "Fang spotted: $fang_spotted\n" .
    "Other comments: $other";
  mail($to, $subject, $msg, 'From:' . $email);

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;
?>

</body>
</html>

And the output actually varies from browser to browser; I tried using Firefox, Google Chrome, and Internet Explorer.
Firefox loads the HTML form, and once the button is clicked, it launches a download with a .php file, which is the name of the php file my html file is referencing.
This is what is in the downloaded php (basically the source code for my php file).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

<?php
  $name = $_POST['firstname'] . ' ' . $_POST['lastname'];
  $when_it_happened = $_POST['whenithappened'];
  $how_long = $_POST['howlong'];
  $how_many = $_POST['howmany'];
  $alien_description = $_POST['aliendescription'];
  $what_they_did = $_POST['whattheydid'];
  $fang_spotted = $_POST['fangspotted'];
  $email = $_POST['email'];
  $other = $_POST['other'];

  $to = 'owen@aliensabductedme.com';
  $subject = 'Aliens Abducted Me - Abduction Report';
  $msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
    "Number of aliens: $how_many\n" .
    "Alien description: $alien_description\n" .
    "What they did: $what_they_did\n" .
    "Fang spotted: $fang_spotted\n" .
    "Other comments: $other";
  mail($to, $subject, $msg, 'From:' . $email);

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;
?>

</body>
</html>

Now when I run Google Chrome, the html form loads, however this is what I get after i submit.

'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '
'; echo 'Number of aliens: ' . $how_many . '
'; echo 'Describe them: ' . $alien_description . '
'; echo 'The aliens did this: ' . $what_they_did . '
'; echo 'Was Fang there? ' . $fang_spotted . '
'; echo 'Other comments: ' . $other . '
'; echo 'Your email address is ' . $email; ?>

IE Explorer loads the html form, and after I click the submit button on my form, I receive a blank page.

Does anyone have a sample "hello world" using php that I could test? I think it has to do with my php installation.

Member Avatar for Zagga

The hello world script is

<?php
echo "Hello world";
?>

save the file as test.php

Are you running this script on a localhost (your own pc) or on an internet hosting account?

Honestly, I have no clue. All I know is that I have:
Apache Installed
PHP Installed for Apache
Localhost works

I simply open the html file with a browser, click the submit button, and have it do it's thing.

Is there any way to run a php file by itself?

Member Avatar for Zagga

You just open the file in the browser.
The address will be something like http://localhost/test.php

I think I know what I might be doing wrong.
When I open the file, in my case, this is the URL. file:///C:/Users/Marek/Desktop/test.php

I'm not running it through localhost. How do I run it through localhost using apache?

EDIT:
Nevermind, I figured out I have to place it in the apache httdocs folder, and now I can run it from localhost. But the output of the .php file when ran in localhost is the following:

<?php
echo "Hello world";
?>

Member Avatar for Zagga

It all depends on where you installed apache.
You need to place your website files in the folder called htdocs in your apache directory.
Did you use a package like WAMP or something similar to install PHP and Apache all in 1 go or did you install them seperately.

Have a look at some of the packages available, they make the install much easier (this is what chrishea was referring to earlier when he mentioned LAMP stack). I use XAMPP myself and found it very easy to install.

commented: Solved my error. Great! +1

Zagga,
I installed XAAMP for I manually installed everything else before. It shows "hello world" now. Guess it was a problem with my distribution installation. Thank you so much! Cheers.

Yeah great information
I learn with this post a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.