HTML forms with PHP

Kyle Barry

Regular
Joined
Aug 2, 2008
Messages
50
Location
New Jersey
WCA ID
2007BARR02
Hi, I know a lot of cubers have programming interests, and I'm trying to set up a few websites, but have run into unexpected problems.

I have basically no experience, and am probably doing something very obviously wrong, so if you can help me out, that would be great.

I'm tying to set up an HTML form to display a text box, where people can type their emails in, hit the Submit button, and it be retrieved and posted in my .php file. I've looked up ways to do this, but for whatever reason, it's not working. Here's my code so far:
This is the html document.

Code:
    <form action="emails.php" method="post">
    Email:
    <input type="text" name="email" />
    <input type="submit" value="Submit" />
    </form>

My .php document, entitled emails.php is here:
Code:
<html>
<body>

<?php

$email = $_POST['email'];

echo "Email" $email;

?>

</body>
</html>

Does anyone see any obvious problems, thanks in advance.
 
Well you do need to concatenate your strings using the . character or you'll get an error. Other than that I see no reason why your code wouldn't work. Is php working properly? What do you get when execute php_info()?
 
First of all, I'm not actually on using a website, just html and php files, just making sure that's known. Also, I'm guessing my problem is that I haven't downloaded Apache or something to read .php, I assumed my browser would, but I guess that's not the case. If you think that's the problem, what shall I do? Thanks, sorry for the ignorance.
 
I think you should:

1.) Run xampp-control.exe.
2.) There's going to be a list of Modules, Start Apache and MySql.
3.) go to the htdocs folder, and make a new folder inside it with any name you want.
4.) put your .php, .html, image files and everything else in that folder you just made.
5.)go to a browser and type in //localhost/yourfoldername

Hope that works. Web design class doesn't cover too much about it.
 
Last edited:
Download this http://www.apachefriends.org/download.php?xampp-win32-1.7.3.exe

1. Once you do that double click on the file above and then install it as you would any other software.
2. Assume you install it at C:/Program Files/xampp
3. Now put your files in C:/Program Files/xampp/htdocs/Email
4. You may need to create the Email folder above.
5. In your browser type "http://127.0.0.1/Email"

You should now be able to see the Email that you enter on your Index Page.
 
Ok, I tried all this stuff, and it seems like I'm close. I can now open the C:/ProgramFiles... in the browser and it shows the files and folders. But do I have to adjust my code in some way? When I type in my subscribe box, it's still doesn't show me the email text? Not sure what to do.
 
Ok, I tried all this stuff, and it seems like I'm close. I can now open the C:/ProgramFiles... in the browser and it shows the files and folders. But do I have to adjust my code in some way? When I type in my subscribe box, it's still doesn't show me the email text? Not sure what to do.

If you go to file://.... then you're just going through the file system. You'll want to do a http://localhost/ to connect back to your webserver (which is running on the localhost), in order to have it do PHP processing.
 
First of all, I'm not actually on using a website, just html and php files, just making sure that's known. Also, I'm guessing my problem is that I haven't downloaded Apache or something to read .php, I assumed my browser would, but I guess that's not the case. If you think that's the problem, what shall I do? Thanks, sorry for the ignorance.

Sorry for not responding, I went to sleep instead :p

Ok, this is a brief explanation of how this is supposed to work (and why it hasn't worked for you). PHP relies on the client-server model, which consists of:

The client(s): a client will usually be an individual computer that sends requests to the server, probably through a web browser.
The server: the server is a computer that has a web server running (which is just a program capable of receiving requests and returning web pages), normally apache. PHP is executed on this side, your browser has nothing to do with it.

Both the server and client can be the same computer, but the model stands, your computer would just have to send requests and responses to itself (a reference to your local computer is 127.0.0.1). So this is how the page you're trying to build would work if you had apache properly running:

Client: Using your browser, you send a request of the form page to 127.0.0.1
Server: The server grabs the page conatining the form and sends it back to the client.
Client: You fill out the form and send the request of emails.php to the server. This requests contains some information in it's post data, namely whatever you entered in the email field.
Server: The server gets the request and grabs emails.php to return to the client. It notices that there's some PHP in emails.php, so it's executed before sending the response back to the client. PHP has access to the client's request's post data, which is used to generate some text in the webpage that's going to be sent back to the client.
Client: you get a plain web page that says "Email [email protected]".

As you can see, the client and it's browser don't know anything about php, they just send requests and get web pages in return, with no knowledge of how the information sent in the requests is used to generate the received responses.

As far as the installation goes, I haven't installed apache on a windows system in years. What I did back then was download and install apache, download the php package, applied some changes to apache's configuration file to let it know where the php package was and enable php scripts to be executed, and then restart apache so those changes apply. I'm guessing there's much easier ways to do this now.
 
Hey, thanks again for your help, and I've gotten to the point where xampp is actually working, and when I enter into my form on my html page, I get a blank page, like you said, saying "Email blah blah blah." Now, I am having trouble finding out how to extract and save what is being posted there. Every time I write in the form again, it resets and I only have one email. Any suggestions?
 
Back
Top