<?php
$name = $_POST["name"];
$email = $_POST["email"];
$gender = $_POST["gen"];
$age = $_POST["age"];
$comments = $_POST["comments"];
if(empty($_POST["name"])){
echo "empty name";
}
if(empty($_POST["email"])){
echo "empty email";
}
///
if(empty($_POST["gen"])){
echo "empty gender";
}
if(empty($_POST["comments"])){
echo "empty comments";
}
if(!isset($_POST['submit'])) {
?>
<html>
<head>
<title>Lab6 : P1</title>
</head>
<body>
<fieldset>
<legend><h4>Enter your information in the fields below</h4></legend>
<form name="info" method="post" action="<?php echo $PHP_SELF;?>" method="post">
<strong>Name:</strong> <input type="text" name="name" id="name" /><br>
<strong>Email:</strong> <input type="text" name="email" id="email" /><br>
<br>
<strong>Gender</strong><br>
<input type="radio" name="gen" value="Male">Male</input><br>
<input type="radio" name="gen" value="Female">Female</input><br>
<br>
<select name="age">
<option value="Under 30">Under 30</option><br>
<option value="Between 30 and 60">Between 30 and 60</option><br>
<option value="60+">60+</option>
</select>
<br>
Comments: <textarea name="comments" cols="20" rows="5"> </textarea>
</fieldset>
<input type="submit" name="submit" value="Submit my Information" />
</form>
</body>
</html>
<?
}
else {
echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>";
echo "<br>We will reply to you at:" . "<em>" . $email . "</em>";
}
?>
I need it to validate the fields: name, email, comments for empty strings (not allowed) and not allow unselected radio buttons for gender and age selection.
Can anyone help
<?php
// to eventually re-fill the fields
$name = "";
$email = "";
$gender = "";
$age = "";
$comments = "";
// to re-select a radio button and select option
$Mchecked = "";
$Fchecked = "";
$selectMinus_30="";
$select30_to_60="";
$select60_plus="";
// to display errors
$error = "";
$done=false;
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["age"])){
if($_POST["name"]==""){
$error = "empty name <br/>";
}
if($_POST["email"]==""){
$error = $error . "empty mail <br/>";
}
if(!isset($_POST["gen"])){
$error = $error . "empty gender <br/>";
}
else{
$gender = $_POST["gen"];
if ($gender == "Male"){
$Mchecked = "checked";
}
else if ($gender == "Female"){
$Fchecked = "checked";
}
}
if($_POST["comments"]==""){
$error = $error . "error: empty comments <br/>";
}
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$age = $_POST["age"];
if ($age == "Under 30"){
$selectMinus_30 = "selected";
}
else if ($age == "Between 30 and 60"){
$select30_to_60 = "selected";
}
else if ($age == "60+"){
$select60_plus = "selected";
}
if ($error==""){
$done=true;
}
}
?>
<html>
<head>
<title>Lab6 : P1</title>
</head>
<body>
<?php if (!$done){ ?>
<fieldset>
<legend><h4>Enter your information in the fields below</h4></legend>
<p class="error" style="color:red;"><?php echo $error;?></p>
<form name="info" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<strong>Name:</strong> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br/>
<strong>Email:</strong> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br/>
<br/>
<strong>Gender</strong><br/>
<input type="radio" name="gen" value="Male" <?php echo $Mchecked;?>>Male</input><br/>
<input type="radio" name="gen" value="Female" <?php echo $Fchecked;?>>Female</input><br/>
<br/>
<select name="age" value="<?php echo $age;?>">
<option value="Under 30" <?php echo $selectMinus_30;?>>Under 30</option><br/>
<option value="Between 30 and 60" <?php echo $select30_to_60;?>>Between 30 and 60</option><br/>
<option value="60+" <?php echo $select60_plus;?>>60+</option>
</select>
<br/>
Comments: <textarea name="comments" cols="20" rows="5"><?php echo $comments; ?></textarea>
</fieldset>
<input type="submit" name="submit" value="Submit my Information" />
</form>
<?php }else{
echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>";
echo "<br/>We will reply to you at:" . "<em>" . $email . "</em>";
} ?>
</body>
</html>
Here I did proper validation, and also re-fill the form when you submit with any empty field.
P.S. Thank you Marc B , I didn't realize that my first post was aweful.
Related
I'm recieving an issue in the following php code. I am recieiving an unknown variable error in line 146, (echo $newrecord) variable. I'm not sure what is wrong with this variable, I have defined it in the IF statement, and am simply echoing if it is successful. I originally had that segment of code (after ) at the top of the script, but it was causing issues with the mandatory field error messages displaying properly. Any help is appreciated!
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = $subErr = "";
$name = $email = $gender = $comment = $website = $sub = $newrecord = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["Name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["Email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["Email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["Website"])) {
$website = "";
} else {
$website = test_input($_POST["Website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["Comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["Comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["Subscription"])) {
$subErr = "Subscription is required"; }
else {
$sub = test_input($_POST["Subscription"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Southern Tier Daily News</h2>
<form method="post" action="Newspaper3.php">
<input type="hidden" name="submitted" value="true"/>
<img src="https://bloximages.newyork1.vip.townnews.com/dnews.com/content/tncms/custom/image/5eec4204-483e-11e6-93c8-97ef236dc6c5.jpg?_dc=1468334339" alt="HTML5 Icon" style="width:128px;height:128px;">
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset>
<legend>Newspaper Subscription Request</legend>
Name: <input type="text" name="Name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="Email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="Website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="Comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Subscription:
<select name="Subscription">
<option value=""></option>
<option value="Daily">Daily</option>
<option value="Evening">Evening</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
</select>
<span class="error">* <?php echo $subErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
<br><br>
Visit Admin Page
</fieldset>
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
echo "<br>";
echo $sub;
?>
<?php
if (isset($_POST['submitted'])) {
include('connect-mysql.php');
$fname = $_POST['Name'];
$femail = $_POST['Email'];
$fcomment = $_POST['Comment'];
$fsubsciption = $_POST['Subscription'];
$sqlinsert = "INSERT INTO newspaper (Name, Email, Comment, Subscription) VALUES ('$fname',
'$femail', '$fcomment', '$fsubsciption')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
} // end of nested if statement
$newrecord = "1 record added to the database";
} // end of main if statement
?>
<?php
echo $newrecord
?>
</body>
</html>
newrecord is defined and initialized inside the if statement, therefore if your code opts to the else, it will skip the if and your newrecord variable won't exist.
$newrecord is defined within an if statement, when the if is not executed the variable is not available. You can define it by default adding $newrecord = ''; before you start the if for the submit.
I'm having issues trying to output data which a user has inputted into a form.. It's a 2 block form, I'm just trying to save the data into 2 variables and then echo the variables. But I don't understand where I'm going wrong. Any help appreciated.
<?php
echo $problem = "";
if(isset($_POST['submit']) && $_POST['submit']=="submit"){
if(!empty($_POST['eWeight']) && $_POST['eWeight']!=''){
$eWeight = mysqli_real_escape_string($conn, trim($_POST['eWeight']));
} else {
$problem .= "Please enter a weight. <br/>";
}
if(!empty($_POST['gym']) && $_POST['gym']!=''){
$gym = mysqli_real_escape_string($conn, trim($_POST['gym']));
} else {
$problem .= "Please enter time at gym. <br/>";
}
echo $eWeight, $gym;
}
?>
<?php
if($conn){
echo "connected";
}
echo $problem;
echo $eWeight;
?>
<form class="pure-form pure-form-stacked" name="contact_weight">
<label for="eWeight">Enter Weight: </label> <input type="number" id="eWeight" name="eWeight" placeholder="88" required/>
<label for="gym">Enter Time at Gym: </label> <input type="number" id="gym" name="gym" placeholder="60" required/>
<button class="submit" type="submit">Submit Form</button>
</form>
You can do something like this
<?php
echo $problem = "";
if(isset($_POST['submit'])){
if(!empty($_POST['eWeight']) && $_POST['eWeight']!=''){
$eWeight = mysqli_real_escape_string($conn, trim($_POST['eWeight']));
} else {
$problem .= "Please enter a weight. <br/>";
}
if(!empty($_POST['gym']) && $_POST['gym']!=''){
$gym = mysqli_real_escape_string($conn, trim($_POST['gym']));
} else {
$problem .= "Please enter time at gym. <br/>";
}
echo $eWeight, $gym;
}
if($conn){
echo "connected";
}
echo $problem;
echo $eWeight;
?>
<form class="pure-form pure-form-stacked" name="contact_weight" action="" method="post">
<label for="eWeight">Enter Weight: </label> <input type="number" id="eWeight" name="eWeight" placeholder="88" required/>
<label for="gym">Enter Time at Gym: </label> <input type="number" id="gym" name="gym" placeholder="60" required/>
<button name="submit" class="submit" type="submit">Submit Form</button>
</form>
If user leaves anything blank, my error messages do not display. It simply takes me to my action page where the input is blank. Why don't my errors display? Thanks.
<p><span class="error">* required field.</span></p>
<form name="form2" method="post" action="favorites.php">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Favorite Season:
<input type="radio" name="season" value="winter">Winter
<input type="radio" name="season" value="spring">Spring
<input type="radio" name="season" value="summer" checked>Summer
<input type="radio" name="season" value="fall">Fall
<span class="error">* <?php echo $seasonErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
Your name is: <?php echo $_POST["name"]; ?><br>
Your favorite season is: <?php echo $_POST["season"]; ?><br>
<?php
$nameErr = $seasonErr = "";
$name = $season = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["season"])) {
$seasonErr = "Season is required";
} else {
$season = test_input($_POST["season"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
The reason being is, that you have your PHP below your form. Place it above your HTML form.
Sidenote: This answers the question and does not cover the fact that you should be getting undefined index notices on initial page load, depending on how your server's error reporting level is setup.
<?php
$nameErr = $seasonErr = "";
$name = $season = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["season"])) {
$seasonErr = "Season is required";
} else {
$season = test_input($_POST["season"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p><span class="error">* required field.</span></p>
<form name="form2" method="post" action="favorites.php">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Favorite Season:
<input type="radio" name="season" value="winter">Winter
<input type="radio" name="season" value="spring">Spring
<input type="radio" name="season" value="summer" checked>Summer
<input type="radio" name="season" value="fall">Fall
<span class="error">* <?php echo $seasonErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
Your name is: <?php echo $_POST["name"]; ?><br>
Your favorite season is: <?php echo $_POST["season"]; ?><br>
So, it's best to check if anything is set/not empty.
References:
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/function.empty.php
I am trying to create a simple form validation and the form wont submit until all of the fields were set. I have two files here.
-form.php
-process.php
For some reason the $error won't appear and the radio button wont submit. Is there anything wrong?
here's the form.php:
<?php
if(isset($_GET['error']) && $_GET['error']!=""){
echo $_GET['error'];
}
?>
<body>
<form action="process.php" method="POST">
<p>
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="">
</p>
<p>
<label for="location">Dojo Location:</label>
<select name="location">
<option value="Mountain View">Mountain View</option>
<option value="San Francisco">San Francisco</option>
<option value="South Korea">South Korea</option>
<option value="Philippines">Philippines</option>
</select>
</p>
<p>
<label for="language">Favorite Language:</label>
<select name="language">
<option value="JavaScript">JavaScript</option>
<option value="PHP">PHP</option>
<option value="Ruby">Ruby</option>
<option value="Python">Python</option>
</select>
</p>
<p>
<label for="comment">Comment: (Optional)</label><br/>
<textarea rows="10" cols="50" name="comment"></textarea>
</p>
<p>
<label for="comment">Can we store cookies in your computer?</label>
<input type="radio" name="cookies" value="yes">Yes
<input type="radio" name="cookies" value="no">No
</p>
<input type="submit" value="Submit">
</form>
here's the process.php:
<?php
if (isset($_POST["submit"])) {
if (empty($_POST["name"])) {
$Error = "Missing Name";
}
if (empty($_POST["location"])) {
$Error = "Missing Location";
}
if (empty($_POST["language"])) {
$Error = "Missing language";
}
if (empty($_POST["cookies"])) {
$Error = "Select cookies";
}
}else{
$name = $_POST['name'];
$location = $_POST['location'];
$language = $_POST['language'];
$comment = $_POST['comment'];
$cookies = $_POST['cookies'];
}
if($Error!=""){
header("Location:form.php?error=".$Error);
}
?>
<h2>Submitted Information:</h2>
<p><?php echo "NAME: {$name}"; ?> </p>
<p><?php echo "DOJO LOCATION: {$location}"; ?></p>
<p><?php echo "FAVORITE LANGUAGE: {$language}:"; ?> </p>
<p><?php echo "COMMENT: {$comment}"; ?></p>
<p><?php echo "COOKIES: {$cookies}"; ?></p>
Any idea?
Try something like this in your process.php
if($Error!=""){
header("Location:form.php?error=".$Error);
}
On your form.php
if(isset($_GET['error']) && $_GET['error']!=""){
echo $_GET['error'];
}
In your process.php change the code to
<?php
if (isset($_POST["submit"])) {
$Error ="";
if (isset($_POST["name"]) && $_POST["name"]!="") {
$Error = "Missing Name";
}
if (isset($_POST["location"]) && $_POST["location"]!="") {
$Error = "Missing Location";
}
if (isset($_POST["language"]) && $_POST["language"]!="") {
$Error = "Missing language";
}
if (isset($_POST["cookies"]) && $_POST["cookies"]!="") {
$Error = "Select cookies";
}
if($Error!=""){
header("Location:form.php?error=".$Error);
}
$name = $_POST['name'];
$location = $_POST['location'];
$language = $_POST['language'];
$comment = $_POST['comment'];
$cookies = $_POST['cookies'];
}
?>
Either you need a form to redirect back to your form.php, or move the echo $Error to your process.php so you can show the error from that page.
I have a simple example of check box's which remember what has been selected after the form has been submitted and there is an error.
That part works great... but, I would like to post the resultant 'checked' boxes to my forms mail function.
What I have now only reports if its checked or unchecked I would prefer to have the checked box's 'value' without the unchecked box even registering.
<?php
$CB_1 = 'unchecked';
$CB_2 = 'unchecked';
$CB_3 = 'unchecked';
$CB_4 = 'unchecked';
$CB_5 = 'unchecked';
if (isset($_POST['submit'])) {
if (isset($_POST['CB_1'])) {$CB_1 = $_POST['CB_1'];
if ($CB_1 == 'item_01') {$CB_1 = 'checked';}
}
if (isset($_POST['CB_2'])) {$CB_2 = $_POST['CB_2'];
if ($CB_2 == 'item_02') {$CB_2 = 'checked';}
}
if (isset($_POST['CB_3'])) {$CB_3 = $_POST['CB_3'];
if ($CB_3 == 'item_03') {$CB_3 = 'checked';}
}
if (isset($_POST['CB_4'])) {$CB_4 = $_POST['CB_4'];
if ($CB_4 == 'item_04') {$CB_4 = 'checked';}
}
if (isset($_POST['CB_5'])) {$CB_5 = $_POST['CB_5'];
if ($CB_5 == 'item_05') {$CB_5 = 'checked';}
}
}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. #TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
check box 01: $CB_1
check box 02: $CB_2
check box 03: $CB_3
check box 04: $CB_4
check box 05: $CB_5
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php if (!empty($error)) echo $error ?>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="CB_1" value="item_01" <?PHP echo $CB_1; ?> /> Item 01
<input type="checkbox" name="CB_2" value="item_02" <?PHP echo $CB_2; ?> /> Item 02
<input type="checkbox" name="CB_3" value="item_03" <?PHP echo $CB_3; ?> /> Item 03
<input type="checkbox" name="CB_4" value="item_04" <?PHP echo $CB_4; ?> /> Item 04
<input type="checkbox" name="CB_5" value="item_05" <?PHP echo $CB_5; ?> /> Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>
I know this may not necessarily answer the question but a way you could make your code more efficiant is do this.
for($i=1; $i<=5;$i++)
{
if(isset($_POST['submit'])&& isset($_POST['CB_'.$i]) && $CB_.$i=='item_0'.$i)
{
$CB_.$i = $_POST['CB_'.$i];
$CB_.$i = 'checked';
}
}
If you fix it that way it may also make it easier for us to debug.
okay, I found a way to do what I wanted but I'm fairly sure it's not the way someone who has real experience with PHP would do it. Any suggestions?
redelman431, thanks for your suggestion but it was really too advanced for my skill level.
<?php
if(isset($_POST['item_01'])) {$item_01 = 'Item 01';}
if(isset($_POST['item_02'])) {$item_02 = 'Item 02';}
if(isset($_POST['item_03'])) {$item_03 = 'Item 03';}
if(isset($_POST['item_04'])) {$item_04 = 'Item 04';}
if(isset($_POST['item_05'])) {$item_05 = 'Item 05';}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. #TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
checked box's: $item_01 $item_02 $item_03 $item_04 $item_05
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="item_01" value="Item 01"
<?php if(isset($_POST['item_01'])) { echo 'checked'; } ?> /> Item 01
<input type="checkbox" name="item_02" value="Item 02"
<?php if(isset($_POST['item_02'])) { echo 'checked'; } ?> /> Item 02
<input type="checkbox" name="item_03" value="Item 03"
<?php if(isset($_POST['item_03'])) { echo 'checked'; } ?> /> Item 03
<input type="checkbox" name="item_04" value="Item 04"
<?php if(isset($_POST['item_04'])) { echo 'checked'; } ?> /> Item 04
<input type="checkbox" name="item_05" value="Item 05"
<?php if(isset($_POST['item_05'])) { echo 'checked'; } ?> /> Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>
I found what I was looking for I suppose in the way of an array which I think 'redelman431' was trying to get me to do but it didn't make any sense to me at the time.
I then stumbled onto another array by 'David BĂ©langer' that for some reason made more sense to me so I used it into my check box's and it works fabulously.
the new problem is that I cant get the check box's that have been checked to remember that if there is an error elsewhere on the page which is a disaster as my final form has a ton of them.
Any ideas?
<?php
# Default Vars
$_group_01 = '';
if(isset($group_01) === TRUE){
# Is Array ?
if(is_array($group_01) === TRUE){
# Count
$c = count($group_01);
# Loop
for($i=0; $i < $c; $i++){
$_group_01.= (isset($group_01[$i]) === TRUE ? $group_01[$i] : '').($i == ($c-1) ? '' : ($i == $c-2 ? ' and ' : ', '));
}
}
}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. TEST .' <'. $email .'>';
$to = "someone#company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
Items Checked: $_group_01
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="group_01[]" value="Item 01"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 01
<input type="checkbox" name="group_01[]" value="Item 02"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 02
<input type="checkbox" name="group_01[]" value="Item 03"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 03
<input type="checkbox" name="group_01[]" value="Item 04"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 04
<input type="checkbox" name="group_01[]" value="Item 05"
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>