Page 2 of 3
Posted: Tue Aug 08, 2006 4:52 am
by jmilane
arborint wrote:milane wrote:Like I said, I want to do it myself... I work for a nonprofit... ie - we dont have no money.
Not looking for a handout... just a tool I can use to make life easier.
Forms processing is just complex enough that it is difficult for beginners to do, and difficult for experienced programmers to generalize (or at least agree on how to do it).
That said the basics are pretty simple. First you need a HTML form, which starts with a form tag that specifies the page that the form will be submitted to and the method (GET or POST). It looks like this:
Code: Select all
<form action="myformpage.php" method="post">
Then there are form fields. There are a couple of different kinds. The basic text input looks like this with a name and a value (which is empty in this example):
Code: Select all
<input type="text" name="first_name" value=""/>
Then a button to submit the form:
Code: Select all
<input type="submit" name="submit" value="Go"/>
And finally you need to close the form with the tag:
I am going the add a error message which will come from the PHP script below. The whole thing looks like this (put it in a file named
myformtemplate.php):
Code: Select all
<span style="color:red"><?php echo implode('', $errors); ?></span>
<form action="myformpage.php" method="post">
<input type="text" name="first_name" value=""/>
<input type="submit" name="submit" value="Go"/>
</form>
OK, so that's the HTML part. Now you need the PHP script to manage the form. One way to do it goes something like this:
1. Get the values from the $_POST superglobal array.
2. Filter the values to make sure there are no unwanted characters
3. Check that required values exist and are valid
4. If there are errors then display the form
5. If there are no errors then redirect to the next page (this prevents resubmits).
So the PHP code would looks something like this (put this code in the file
myform.php):
Code: Select all
// filter the value from the form to only all letters and space
$first_name = preg_replace('/[^a-zA-Z\ ]/', '', $_POST['last_name']);
// check the name based on some rules and set errors if there are problems
$errors = array();
if ($first_name == '') {
$errors[] = 'Please enter a first name. ';
} elseif (strlen($first_name) < 3) {
$errors[] = 'The first name must be at least three letters long. ';
}
// check if the form is valid
if ($errors) {
// show the form if there are errors
include 'myformtemplate.php';
} else {
// do and other processing here, such as saving to a database or emailing
mail('me@mysite.com', 'Form Message', "A message from $first_name");
// redirect to another page if all required fields are acceptable
header('Location: http://www.mysite.com/mynextpage.php');
}
To test it go to
http://www.mysite.com/myform.php
There are some other things to add like checking for submit and not showing error messages the first time the page is displayed ... but that's the basics.
EDIT - fixed (strlen($first_name) < 3) per Everah
I appreciate your help very much... but what about all the database stuff? The SQL? This needs to be stored in a db. The table is already built.
J
Posted: Tue Aug 08, 2006 10:51 am
by Christopher
What kind of database are you using?
Posted: Tue Aug 08, 2006 10:53 am
by jmilane
mysql for this one
Posted: Tue Aug 08, 2006 12:30 pm
by RobertGonzalez
Other than posting here, have you tried anything else? Like tutorials, books, examples, open source apps? There are a lot of resources out there (these forums being one of them) that all appreciate a user's attempt at figuring it out first.
Most posters here are more than willing to help out with your troubles, but few are willing to code the solution for you. I'd recommend downloading the samples chapters of Kevin Yank's Sitepoint book
'Build Your Own Database Driven Website Using PHP & MySQL' and reading them briefly. Everything you want to do is actually clearly described in the first four free chapters of that book.
Posted: Tue Aug 08, 2006 12:35 pm
by Christopher
The basics would be something like this:
Code: Select all
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');
$first_name = mysql_real_escape_string($first_name);
$last_name = mysql_real_escape_string($last_name);
$query = "INSERT INTO my_table (first_name,last_name) VALUES ('$last_name','$last_name')";
$result = mysql_query($query);
You need to add error checking for the connect, select and query.
Posted: Tue Aug 08, 2006 12:37 pm
by jmilane
Everah wrote:Other than posting here, have you tried anything else? Like tutorials, books, examples, open source apps? There are a lot of resources out there (these forums being one of them) that all appreciate a user's attempt at figuring it out first.
Most posters here are more than willing to help out with your troubles, but few are willing to code the solution for you. I'd recommend downloading the samples chapters of Kevin Yank's Sitepoint book
'Build Your Own Database Driven Website Using PHP & MySQL' and reading them briefly. Everything you want to do is actually clearly described in the first four free chapters of that book.
Yeah. I know this.
If you
read my original post, I was looking to see if anyone knew of any form generation programs that worked. Again, I have downloaded several.
I never asked anyone to DO anything for me.
Like I said, I am in the process of learning, but am under the gun with work and need to get a hold of one of these programs.
I dont want anyone to do anything for me. Please dont misconstrue my intent. If the answer is, "I dont know... sorry guy... you're gonna have to learn it." then thats the answer.
Thanks.
Posted: Tue Aug 08, 2006 1:01 pm
by RobertGonzalez
Fair enough. I was just noting that you are getting a lot code posted for you and you are posting none of your own. I understand being under the gun, also.
I would recommend you take arborints code sections and modify them for your needs. He has posted the form, the data checking and now the database insertion. It is the basics, but you can probably get away with using it almost exactly like it is.
Posted: Tue Aug 08, 2006 1:06 pm
by jmilane
Everah wrote:Fair enough. I was just noting that you are getting a lot code posted for you and you are posting none of your own. I understand being under the gun, also.
I would recommend you take arborints code sections and modify them for your needs. He has posted the form, the data checking and now the database insertion. It is the basics, but you can probably get away with using it almost exactly like it is.
You want code from me?
i->dont.know$php
{
if ( $dummy = yes
$me = "clueless"
$this->no idea
)
}
Seriously... I only wanted a pointer towards something. If and when I ever know what I am doing, I will share it with you.
I will do the best I can. Thanks, everyone, for your help. Truly. I do appreciate it very very much - I will make it work.
J
Posted: Tue Aug 08, 2006 1:10 pm
by RobertGonzalez
Sorry, I think I may have confused you a little bit. I apologize.
Take arborints code, try to make it work for you. If you can' get it to work for you, post back what you are experiencing and we can walk you through that. When you can, pick up the sample chapters of the book I posted about earlier. It is a really nifty trainer. I learned the earliest portions of PHP development from that book.
Then, as all hell breaks loose in you apps, come back and we will walk you through it. Sooner or later you will move from being a $dummy to being a $stud->guru.
Posted: Tue Aug 08, 2006 1:11 pm
by Christopher
If you want to try the code I posted, just see if you can get the basics working and post what you have done. It does need some little things added here and there, but we can help with that if you give it a go first.
Posted: Tue Aug 08, 2006 1:11 pm
by jmilane
Everah wrote:Sorry, I think I may have confused you a little bit. I apologize.
Take arborints code, try to make it work for you. If you can' get it to work for you, post back what you are experiencing and we can walk you through that. When you can, pick up the sample chapters of the book I posted about earlier. It is a really nifty trainer. I learned the earliest portions of PHP development from that book.
Then, as all hell breaks loose in you apps, come back and we will walk you through it. Sooner or later you will move from being a $dummy to being a $stud->guru.
No, I was making a joke.
(ha ha)
Thank you for all your help.
J
Posted: Tue Aug 08, 2006 1:16 pm
by jmilane
arborint wrote:If you want to try the code I posted, just see if you can get the basics working and post what you have done. It does need some little things added here and there, but we can help with that if you give it a go first.
You rock, bro... thank you.
Ill contact you if I cannot get it to work. I have been fighting with mysql all day and have not even been able to address this. I am trying to get the logging turned on - need to do that before I do this.
Thanks!!
Posted: Tue Aug 08, 2006 1:19 pm
by jmilane
Everah wrote:Sorry, I think I may have confused you a little bit. I apologize.
Take arborints code, try to make it work for you. If you can' get it to work for you, post back what you are experiencing and we can walk you through that. When you can, pick up the sample chapters of the book I posted about earlier. It is a really nifty trainer. I learned the earliest portions of PHP development from that book.
Then, as all hell breaks loose in you apps, come back and we will walk you through it. Sooner or later you will move from being a $dummy to being a $stud->guru.
Thanks for the vote of confidence.
You are very kind. You will always be my $stud.
This board seems like it is full of good people. If I could make you all brownies, I would. Hell, I will. I will just eat them for you.
I'll be back!
J
Posted: Tue Aug 08, 2006 1:23 pm
by Luke
yea, we're awesome.

Posted: Tue Aug 08, 2006 3:58 pm
by RobertGonzalez
/ pats self on back while wondering when ninja is going to get that whipped cream off his head..