I made a form for a 5K run where the user enters basic information about him/her (first name, last name, email, organization, whether they are attending the event, and checking
There a basically two ways to use a form to send data to a database. First method is to let people fill out the form and send the data to an next page. You can add Java script to check if the filled out data meets your requirement and to imediatedly respond to . E.g. have all required field have been filled. The next page progresses the data and send the data to the MySQL server. Form methods you could use are POST / GET. Second method is to display the form on one page and do the prossesing of the data on the same page (PHP_SELF or by referencing the same page in you form action). The second method requires that code is very structured. If you need to check if the data has been submitted. Next you need to verify is valid. If the form was submitted and the input valid the data is submitted to the database and the visitor is displayed a text like "Thank your for submitting your form . We will contact you as soon as possible.". If the form was submitted and the data was not valid the visitor should stay on the same page en get an error messages like "The following fields need to be fill out: ...." and the form should be displayed. If the form was not submitted (on the first visit) the visitor should get the form displayed.
The most easy method to setup would be the first method the second is the nicest I think, but I suggest you start out with the first method. Once the first method is working you could add things like Javascript and / or change it to method two.
Try this:
".$row['name']."\n";
}
}
mysqli_close($link);
}
//Check if variables are post if so filter the input if not initiating variables for form
IF (isset($_POST['firstname'])){
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
}
ELSE {
$firstname = "";
}
IF (isset($_POST['lastname'])){
$lastname = filter_var($_POST['lastname'],FILTER_SANITIZE_STRING);
}
ELSE {
$lastname = "";
}
IF (isset($_POST['org'])){
$org = $_POST['org'];
}
ELSE {
$org = "";
}
IF (isset($_POST['email'])){
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
}
ELSE {
$email = "";
}
IF (isset($_POST['attendant'])){
$attendant = filter_var(filter_var($_POST['attendant'],FILTER_SANITIZE_STRING));
}
ELSE {
$attendant = "";
}
IF (isset($_POST['waiver'])){
$waiver = filter_var(filter_var($_POST['waiver'],FILTER_SANITIZE_STRING));
}
ELSE {
$waiver = "";
}
function submit_form(){
$host = "localhost";
$user = "user";
$password = "password";
$database = "database";
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$org = $_POST['org'];
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
$attendant = $_POST['attendant'];
// open connection to database
$link = mysqli_connect($host, $user, $password, $database);
IF (!$link){
echo ("Unable to connect to database!");
}
ELSE {
//INSERT VALUES INTO DATABASE
$query = "INSERT INTO basicInfo (firstname,lastname,email,attendant,org,time) VALUES('".$firstname."', '".$lastname."', '".$email."', ".$attendant.", ".$org.", NOW())";
return mysqli_query($link,$query);
}
//close connection to database
mysqli_close($link);
}
//Warning messages initiation
$warning_firstname = "*Required";
$warning_lastname = "*Required";
$warning_org = "*Required";
$warning_email = "*Required";
$warning_attendant = "*Required";
$warning_waiver = "*Required";
$formfirstpart = <<
Form 5K RUN
We're excited to introduce the 5K Run to VIA-1! During this year's conference, Individuals can register to participate in the fun across University of Iowa's campus in order to raise money for this year's CPP