Help With Form

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
davmor1967
Forum Newbie
Posts: 3
Joined: Sat Jun 26, 2010 1:40 pm

Help With Form

Post by davmor1967 »

Sorry if this is in the wrong place. I am creating a form for a group insurance company. I am OK on setting up the form, but the wall I have run into is allowing the company person to add multiple employees.
Basically when an employee's information is added then the options are to add another employee or submit the form.
Any help or direction is greatly appreciated.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Help With Form

Post by JakeJ »

Without seeing your code it's tough to know where to start.

But it sounds like you need two different submit buttons. One submit button submits the form and moves on to whatever is next, returns home or whatever. The other one submits the data and then reloads the form in order to add another employee.

There are lots of tutorials out there for forms with multiple submit buttons. Go through a couple, give the code a try and then post back if you have a problem.
davmor1967
Forum Newbie
Posts: 3
Joined: Sat Jun 26, 2010 1:40 pm

Re: Help With Form

Post by davmor1967 »

What I am basically trying to do is this.
Have person complete contact information (easy enough)
Then, tell the form they want to add employee information. After adding employee 1 information their options would be to submit form or add another employee. After adding another they would have the same options as before, submit or add another. I am sure this can't be as difficult as I assume. I don't really want to send all the info to a database as these forms are a request for a quote and the client doesn't necessarily need to keep the info after the form results are received.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Help With Form

Post by JakeJ »

Where does the information go?
Not that it really matters; I just assumed the info was going to a database.

Make the form action a different page and on that page add code to do what you want.

Code: Select all

//process.php
<?
If ($_POST['submit'] == 'button1'){
   //process the form (email data, write to database, whatever)
   //redirect to another page (or make a "Submitted Successfully" message or something
}
Else {
  //process the form (email data, write to database, whatever)
 // go back to the original form so more data can be entered
Does that make sense?
davmor1967
Forum Newbie
Posts: 3
Joined: Sat Jun 26, 2010 1:40 pm

Re: Help With Form

Post by davmor1967 »

That may do what I need. I will repost with the results. Thanks for the help.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help With Form

Post by AbraCadaver »

This is very rough but may give you an idea of how to do what I think you're asking in straight PHP:

Code: Select all

if(isset($_POST['submit'])) {
	// do processing
	// redirect
	exit;
}

echo '<form method="post">'
.'contact info <input type="text" name="contact_info"><br>'
.'<input type="submit" name="submit" value="Submit">'
.'<input type="submit" name="add" value="Add Employee"><br>';

$i = 1;
if(isset($_POST['employee'])) {
	foreach($_POST['employee'] as $employee) {
		if(!empty($employee['first_name']) && !empty($employee['first_name'])) {
			echo 'employee '.$i.'<br>'
			.'first name <input type="text" name="employee['.$i.'][first_name]" value="'.$employee['first_name'].'"><br>'
			.'last name <input type="text" name="employee['.$i.'][last_name]" value="'.$employee['last_name'].'"><br>';
			$i++;
		}
	}
}
if(isset($_POST['add'])) {
	echo 'employee '.$i.'<br>'
	.'first name <input type="text" name="employee['.$i.'][first_name]"><br>'
	.'last name <input type="text" name="employee['.$i.'][last_name]"><br>';
}
echo '</form>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply