Jump to content

[SOLVED] Required Fields in a Form


betsy_ls
 Share

Recommended Posts

I am trying to edit a form so that certain fields are required to submit.  I am very, very new to PHP though and am not sure how to do this.  The form already makes sure there is a valid email, but I also want the first name, last name and organization to be required.

 

Here is my PHP code.

 

 <?php





		if(isset($_POST['submitform']) && $_POST['submitform']==1) {



			if(isset($_POST['email']) && eregi("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", $_POST['email'])) {

				$to = "info@gttgrp.com";

				$subject = "GTT FORM SUBMISSION";

				$headers = "From: ".$_POST['email'];

				$date = date("r");

				$message = "Form submission sent from 'Subscribe' at ".$date."\n\n";



				$message .= "Salutation : ".$_POST['salutation']."\n";

				$message .= "First Name : ".$_POST['firstname']."\n";

				$message .= "Last Name : ".$_POST['lastname']."\n";

				$message .= "Company : ".$_POST['organization']."\n";

                                        $message .= "Title : ".$_POST['title']."\n\n";

                                        $message .= "Country : ".$_POST['country']."\n\n";

				$message .= "Phone : ".$_POST['phone']."\n";

				$message .= "Email : ".$_POST['email']."\n\n";



                                       if(isset($_POST['addtonetwork']) && $_POST['addtonetwork']=='yes') {

					$message .= "YES - Add to network.\n\n";

				} else {

					$message .= "NO - Do not add to network.\n\n";

				}







				if(mail($to, $subject, $message, $headers)) {



					include("includes/thankyou.php");

				} else {

					global $error;

					$error = "Mail could not be sent!";

					include("includes/form_subscribe.php");

				}

			} else {

				global $error;



				$error = "You must submit a valid E-Mail.";

				include("includes/form_subscribe.php");

			}



		} else {



			include("includes/form_subscribe.php");

		}

                 


            ?>

 

Please let me know if you need any other code.  I have been reading tutorials about this all day, but I do not know enough PHP to be able to apply it to the code I am editing.

 

Thanks!

Link to comment
Share on other sites

many ways to do this.

 

this is how i would do it:

 

<?php

$valid_email = false;
$valid_first = false;
$valid_last  = false;
$valid_org = false;

$errors = array();

if(isset($_POST['submitform']) && $_POST['submitform']==1) {

if(isset($_POST['email']) && eregi("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", $_POST['email'])) {
	$valid_email = true;
} else {
	array_push($errors, "You must submit a valid E-Mail.");
}

if ($_POST['firstname'])!="") {
	$valid_first = true;
} else {
	array_push($errors, "You must enter your first name.");
}

if ($_POST['lastname'])!="") {
	$valid_last  = true;
} else {
	array_push($errors, "You must enter your last name.");
}

if ($_POST['organization'])!="") {
	$valid_org = true;
} else {
	array_push($errors, "You must enter your organization.");
}


if ($valid_email && $valid_first && $valid_last && $valid_org) {

	$to = "info@gttgrp.com";
	$subject = "GTT FORM SUBMISSION";
	$headers = "From: ".$_POST['email'];
	$date = date("r");
	$message = "Form submission sent from 'Subscribe' at ".$date."\n\n";

	$message .= "Salutation : ".$_POST['salutation']."\n";
	$message .= "First Name : ".$_POST['firstname']."\n";
	$message .= "Last Name : ".$_POST['lastname']."\n";
	$message .= "Company : ".$_POST['organization']."\n";
						$message .= "Title : ".$_POST['title']."\n\n";
						$message .= "Country : ".$_POST['country']."\n\n";
	$message .= "Phone : ".$_POST['phone']."\n";
	$message .= "Email : ".$_POST['email']."\n\n";

					   if(isset($_POST['addtonetwork']) && $_POST['addtonetwork']=='yes') {
		$message .= "YES - Add to network.\n\n";
	} else {
		$message .= "NO - Do not add to network.\n\n";
	}




	if(mail($to, $subject, $message, $headers)) {

		include("includes/thankyou.php");
	} else {
		global $error;
		$error = "Mail could not be sent!";
		include("includes/form_subscribe.php");
	}
} else {
	global $error;
	$error =  implode("<br />", $errors);
	include("includes/form_subscribe.php");
}

} else {

include("includes/form_subscribe.php");
}


?>

Link to comment
Share on other sites

Thanks!  That worked.

 

Now, I was wondering how you keep the information they have already entered into the form when the error message comes up... so they don't have to type everything in again.

 

Something like this:

<form>
<input type="text" name="input name goes here" value"<?php echo $_POST['input name goes here']; ?>" />
</form

 

Obay beat me to it.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.