I am having a difficulty of finding a answer that is simple to follow and understand... I making a site (on dreamweaver cs5) and the frontpage of the site has a select city and a dropdown of all the major cities and a email text box. I have a few questions and those are as follows:
What is my next step now that I have the html pretty much done... do I connect it to a database?
What php script would I need to make sere both fields (the city and email) are filled? and where would I enter this php script?
Here is some of my code in case you were wondering: http://answers.yahoo.com/question/index?qid=20110611111223AAeAnrT (had to put it on here because overflow wouldn't let me put in code..)
Just a quick starter, to get your form up.
On creating your PHP script, I don't know much about Dreamweaver, but if you have PHP installed on your server, you should be able to create it in the same directory as your HTML. All of the form elements should be in a form tag, and should point to the PHP, such as <form method="POST" action="dosomething.php">
before and </form>
after.
Also, a "Please select your city..."
might be nice to have on the form, like <option value="unset">Please select your city...</option>
. (Also, all of the options in the sample should have a value
attribute).
I don't know how much you have learned about PHP, so I'm going to try to start with the basics. The PHP script would be a plain-text file with the extension .php
, such as dosomething.php
. Inside of the script, the PHP code needs to be surrounded by the PHP start and end tags, <?php
and ?>
.
The values inputed into the form should be accessible with the $_POST
variables in PHP, so in the script $_POST['select']
will be set to the current value. I recommend setting the names to something you can remember, such as selectedCity
and emailAddress
.
In our PHP script, we will want to get the variables from our form, and check to see if they are both filled. Then the data will get written to the database. I have created a sample snippet below that is commented, but extra security should be added, and this code should not be used as-is.
<?php
$city = $_POST['selectedCity']; // Get the city the user selected from the form
$addr = $_POST['emailAddress']; // Save the email address the user entered
if($city == "unset")
{
// Stops user if a city hasn't been selected
die("Please select a city."); // Stop executing code, and tell user to go back and select a city
}
if($addr == "")
{
// Stops user if the email address is blank (also would be good to make sure email address is correct, like [email protected])
die("Please enter a valid email address");
}
if(!file_exists("../mailinglist.sqlite"))
{
// Creates the database if it doesn't exist
// The database should be outside the document root (meaning you can't access it through the web)
$db = sqlite_open("../mailinglist.sqlite"); // Opens the database, creates it if non-existent (it is)
sqlite_query("CREATE TABLE users (city, email)"); // Creates a table for users
}
else
{
$db = sqlite_open("../mailinglist.sqlite"); // Opens the database if it exists
}
sqlite_query("INSERT INTO users (city, email) VALUES ('".sqlite_escape_string($city)."','".sqlite_escape_string($city)."')"); // Add the new user to the database
?>
(Anything that you need help with that is in blue should be searchable on the PHP Documentation)
The code above will take the output from the HTML form, check to make sure that it is not empty, and enter it into a database, creating a new database if it does not exist. Again, this is just a starter, and the code needs to be improved before taking it live.
Hope this helps!