There are many ways to tackle this (and anything else in life) but I'd most likely do it like so (of course this will need adjusting, it was written quickly):
- Ask for their name (and ideally their password too but...). So this is the first form.
- Query the database, get all information that matches that name. If you get any mysql_num_rows then move to #3 else go back to #1 and yell at them.
- Create a new form based on the above information.
- Rewrite all code, tables, everything, in lowercase! (optional! 😉 I did this below, adjust accordingly.
- Implement some real debugging, I didn't.
As far as "how" well how about:
<?php
// If an action is set (a form is submitted) set $action otherwise default to initial_form (first form)
// Use of ? : is nothing special, it's just a ternary operator
$action = empty($_POST['action']) ? 'initial_form' : $_POST['action'];
// The first form must have been submitted
if ($action === 'second_form') {
$name = trim($_POST['name']);
$sql = "SELECT region, useremail, bossemail, name
FROM sometable
WHERE name = '$name'";
if (!$result = mysql_query($sql)) {
echo "Error: Could not run query ($sql): " . mysql_error();
exit;
}
// If no rows exist that match the name then we have a problem! And will show the inital form again
if (mysql_num_rows($result) < 1) {
echo "Sorry but get a real name, nobody in your database matches the silly name <b>$name</b>";
echo "Try again below:";
initial_form();
} else {
$data = mysql_fetch_assoc($result);
second_form($data);
}
// The second form was submitted, let's end this
} elseif ($action === 'final_submit') {
// Define a few variables from our form
$bossemail = $_POST['bossemail'];
$useremail = $_POST['useremail'];
$region = $_POST['region'];
$name = stripslashes($_POST['name']);
// INSERT DATA INTO THE DATABASE HERE
// You write this code, you said you know how
// Now let's send a couple emails
// First let's define the message/body of our email
$message = "Hello $name !!!\n\nThis is an email about stuff, and stuff, and other stuff, like region: $region\n\nHave a nice day!";
// And now send them
mail($bossemail, 'the subject', $message, "From: [email]admin@someplace.com[/email]");
mail($useremail, 'the subject', $message, "From: [email]admin@someplace.com[/email]");
} else {
?>
<h3>Welcome</h3>
<p>Hello there, please give us your name so we can move on the stage two of our form.</p>
<?php
initial_form();
}
// Definition of some functions used above
function initial_form() {
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" name="submit" value="submit form">
<input type="hidden" name="action" value="second_form">
</form>
<?php
}
function second_form($data) {
// For now let's show what's in $data as it's the information
// from our database. Use it to get values that you'll them
// put into your form. For now let's look at it:
// I assumed the following exists in the form:
// <input type="hidden" name="action" value="final_submit">
// And submited to PHP_SELF like the other form
print_r($data);
}
?>