Problems calling PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Problems calling PHP

Post by wall »

Hi,
I've recently been trying to set up a simple Form-to-Email service within my website, but no matter how simple my code is - or even if i copy someone elses code, it never seems to implement properly.
For example -

Code: Select all

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form</title>
</head>
 
<body>
<form method="post" action="contact.php">
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form> 
</body>
</html>
Will not show anything but a blank page on this;

Code: Select all

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
$email_to = "somebody@email.com";
$email_subject = "Comment from Customer";
$email_body = $_POST['message'];
 
if(mail($email_to, $email_subject, $email_body)){
    echo "The email($email_subject) was successfully sent.";
} else {
    echo "The email($email_subject) was NOT sent.";
}
?>
</body>
</html>
I'm extremely new at this, and not particularly adept with php coding. If you could offer me any advice on what I have done wrong, it would be greatly appreciated.
I plan to make something more complex than this, but I thoughtit best to figure out what is wrong first rather than jump head-first into a new language I seem to be struggling with. Just to be clear, the problem I have is that when I click the "submit" button, it redirects me to the page "contact.php" where all that is shown is a blank page, and no email is sent (i did change the "somebody@email.com" to my own email, which was a hotmail account, for convenience of testing) which I presume, means there is something wrong with my code.
Thanks,
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Problems calling PHP

Post by robnet »

Have you tried running a simple php page yet?

Just try something like this:

Code: Select all

<?php
echo "Hello world from PHP";
?>
 
Report back with what happens here.

.. And make sure error reporting is turned on :)
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Re: Problems calling PHP

Post by wall »

I put in

Code: Select all

<body>
<?php
echo "Hello world from PHP";
?>
</body>
and used "preview in browser" on Dreamweaver, and nothing came up
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems calling PHP

Post by jackpf »

Have you got error reporting turned on?

Also, what do you see in the page source?
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Re: Problems calling PHP

Post by wall »

I edited my php.ini file to turn on error reporting -

Code: Select all

; error_reporting= E_ALL
as for page source -

Code: Select all

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
echo "Hello world from PHP";
?>
 
</body>
</html>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems calling PHP

Post by jackpf »

How are you opening/running the script?

And you do actually have php installed right?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Problems calling PHP

Post by Weiry »

wall wrote:and used "preview in browser" on Dreamweaver, and nothing came up
dreamweaver only previews the file from a FILE view, not a server view.

A php document MUST be viewed from a server running PHP.
If you have a server with PHP installed on it, you need to go to
http://localhost/myPHPpage.php
in order to view the page with php code compiling.
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Re: Problems calling PHP

Post by wall »

I'm using wamp and have it turned on, but I have no idea what I'm supposed to do with it.

If FTP'd the php file to a hosting service, and it comes up "hello world from PHP" so I guess that solves that problem, but I have no idea how to use wamp instead of my hosting service to preview my php
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Problems calling PHP

Post by Weiry »

Ok if you have wamp already, then thats good.
When you want to view your PHP files. You need to put your PHP files in the /wamp/www/ directory.
For instance, my wamp folder is located in my C:/ drive

So i put my PHP files in C:/wamp/www/MyWebSiteDirectory/
Where MyWebSiteDirectory is just a folder you want to put the files in. You can equally just put your files in the root www directory.

Now im not sure what version of WAMP you have, but i know with my current version you do not need to create an ALIAS to map the directory, but if you do, then here are the steps to do it.
1. Start wampServer
2. Left click your wamp icon in the task bar, go to the Apache>Alias Directories>Add an Alias
3. You will have a command prompt window open up asking you information about your alias. The first of which, is the alias name.
So using my above example, my alias i will call "myWebSite" <without quotations in the command prompt>
4. You will then be asked to locate the directory through command prompt.
So using my above example, my location would be C:/wamp/www/MyWebSiteDirectory/
5. Done

Now when you want to view any file in your website directory you open up a web browser and go to the following:
http://localhost/myWebsite/
NOTE: i use the name of the ALIAS, not the name of the folder, after the http://localhost/
But 9/10 times you will be calling your ALIAS the same as your directory anyway.
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Re: Problems calling PHP

Post by wall »

It works now, thanks very much :)
Feel quite stupid for not having realised that earlier, when even copied code wasnt showing up.
Thanks for all the help
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Problems calling PHP

Post by Weiry »

Everyone has to start somewhere, im just glad its all working now :D
I never run into an issue such as the "preview" button from dreamweaver, because i develop entirely using Notepad++ :D
i just think dreamweaver is evil 8) :twisted:
wall
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:05 am

Re: Problems calling PHP

Post by wall »

haha, notepad is probably not a good idea for a person like me.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Problems calling PHP

Post by Mirge »

wall wrote:haha, notepad is probably not a good idea for a person like me.
Notepad and Notepad++ are *very* different things :).

Notepad++ (<---click) is actually quite a good, lightweight text editor. You should give it a try.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems calling PHP

Post by jackpf »

Notepad++ ftw.
Post Reply