Page 1 of 1

Problems calling PHP

Posted: Tue Oct 13, 2009 2:15 am
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,

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 4:54 am
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 :)

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 7:30 am
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

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 7:40 am
by jackpf
Have you got error reporting turned on?

Also, what do you see in the page source?

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 7:47 am
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>

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 9:40 am
by jackpf
How are you opening/running the script?

And you do actually have php installed right?

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 9:57 am
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.

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 7:45 pm
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

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 8:07 pm
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.

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 8:13 pm
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

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 8:37 pm
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:

Re: Problems calling PHP

Posted: Tue Oct 13, 2009 9:54 pm
by wall
haha, notepad is probably not a good idea for a person like me.

Re: Problems calling PHP

Posted: Wed Oct 14, 2009 12:04 am
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.

Re: Problems calling PHP

Posted: Wed Oct 14, 2009 5:31 am
by jackpf
Notepad++ ftw.