I need help with getting .php files to run in my browser (Chrome). If it's totally php, it just shows the code itself in the browser. And if it's php and html I get something like this: http://i.imgur.com/QwsRiVG.jpg
Here is the code:

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name">
   <br><br>
   E-mail: <input type="text" name="email">
   <br><br>
   Website: <input type="text" name="website">
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

I'm sure I'm missing something obvious but we were literally just introduced to php in class.

Recommended Answers

All 7 Replies

Have you installed apache and php on your computer??

I've only installed XAMPP

Have you tested if it works??

Open http://localhost in your browser.
If it shows "It Works" do the following:
Go to xampp installation directory and find htdocs folder inside it.
Now paste the folder containing your webpages in this folder. Say folder name is "folder"
Your main file should be named index.php.
Now open in browser: http://localhost/folder/
This will execute the index.php file.
If you want to execute any other file, type in browser: http://localhost/folder/filename.php

You need to start the apache server from the xampp control panel first.

Okay thanks! I'll follow the steps you provided

When an answer solves your problem be sure to :
Mark the question solved.
Upvote and comment the answer.
This just helps both the parties and keeps things interesting and healthy.

commented: just because, its healthy :), and you didnt tell the OP to RTFM +13
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.