/    Sign up×
Community /Pin to ProfileBookmark

Quick qu please.

Hello there.
Hope you’re all well!

Quick PHP question please.
Below is my PHP mailer, as you can see I’m using validation for certain fields.

For example, to make sure the “name” field has been filled in, I have:

[code=php]if (empty($_POST[‘NAME’])) {
$msg .= “Your name cannot be blank.<br />”;
}[/code]

I am using the same for a drop-down menu, as the selected value doesn’t have a value, can I have a certain thing for one dropdown menu?

I want to make sure something is seleced, so I could use:

[code=php]if (empty($_POST[‘ACCEPT’])) {
$msg .= “Your acceptance must be selected.<br />”;
}[/code]

That works fine, however I have two selections, Yes and No, how can I do that if No is selected, it will echo another message and not submit?

[code=php]<?PHP
$msg = “”;
if (empty($_POST[‘NAME’])) {
$msg .= “Your name cannot be blank.<br />”;
}
if (!is_numeric($AGE)) {
$msg .= “Your age cannot be blank and must be numerical.<br />”;
}
if (!preg_match(“/^[a-zA-Z0-9]+$/”, $USERNAME)) {
$msg .= “Your username cannot be blank and must contain only A-Z, a-z and 0-9.<br />”;
}
if (!preg_match(‘/[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)*@[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)+/’, $_POST[‘applyEMAIL’])) {
$msg .= “Your E-mail address is invalid.<br />”;
}
if (!empty($msg)) {
include “../header.php”; # include your header
echo “<p>$msg</p>”;
include “../footer.php”; # include your footer
exit();
}

#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Distributed by [url]http://www.webdevfaqs.com[/url] #
# Written by Ryan Brill – [email][email protected][/email] #
# All Rights Reserved – Do not remove this notice #
#######################################################

## The lines below need to be edited…

###################### Set up the following variables ######################
#
$to = “[email protected]”; #set address to send form to
$subject = “Results from your web form.”; #set the subject line
$headers = “From: some@email”; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
$location = “link/to/thankyoupage”; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################

## set up the time ##

$date = date (“l, F jS, Y”);
$time = date (“h:i A”);

## mail the message ##

$msg = “Below is the result of your feedback form. It was submitted on $date at $time.nn”;
$msg .= “IP: “.$_SERVER[‘REMOTE_ADDR’].”nn”;

if ($_SERVER[‘REQUEST_METHOD’] == “POST”) {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) .” : “. $value . “n”;
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) .” : “. $value . “n”;
}
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header (“Location:$location”);
}
else {
echo “Thank you for submitting our form. We will get back to you as soon as possible.”;
}

?>[/code]

Thanks.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@olafFeb 21.2004 — Hallo;

what about this:

if (empty($_POST['ACCEPT'])) {

$msg .= "Your acceptance must be selected.<br />";

}

elseif ($_
POST['ACCEPT'] == "no") {

$msg .= "You have to say yes!<br />";

else {

submit...
Copy linkTweet thisAlerts:
@BonnerFeb 23.2004 — [code=php]if (!preg_match("/b[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z0-9._%-]{2,5}b/i", $_POST['applyEMAIL'])) {[/code]

is much quicker and neater for your e-mail checker by the way.
×

Success!

Help @DanUK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...