/    Sign up×
Community /Pin to ProfileBookmark

Error Messages

Hello again. I have one final question.

If my visitors miss a field that is required, how do I set my page up to show the missing fields? For example, if the phone or email is required, how do I let them know that it is the phone or email that is missing.

The current code that let’s them know that they have missed a field is as follows:


———————————————————-

// check input, if any required fields are empty the show error page
if ( empty($firstname) ||
empty($lastname) ||
( $prefer == “email” && empty($email) ) ||
( $prefer == “phone” && empty($phone) ) )
{
header(“Location: “. ERROR);
exit;
}


———————————————————-

I want my missing page to reflect the missing fields. Is there a different code?

Also, while searching for the answer to my above question, I ran across this code used to validate the email address:


———————————————————-

// check client email address
if (preg_match(“/^w+@w+.[a-z]{2,}(.[a-z]{2,})?$/i”, $_POST[‘clientEmail’])) {
echo “Address is valid”;
}
else {
echo “Address is invalid”;
}


———————————————————-

Can I use this below the code for the error fields?

I would like to one day make it where the error messages will appear on the same page as the form, but atlas, I’m not quite that advanced.

Thanks again for your help.

Magikey.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@mityaJun 14.2004 — Ironically, Magikey, validating on the same page is easier than your current option, I reckon. It's also preferable to the end user, but it uses javascript, not PHP, since PHP always requires page transitions. JS runs post-HTML so can still be effective after loading.

<script language="javascript" type="text/javascript">

function validateMe() {

myfield_value = document.getElementById('myfield').value;

if (myfield_value.length > 0) {

document.returned = true;

} else {

alert('You have not completed this form.');

document.returned = false

}

}

</script>


And on the forum itself...

<form action="some_page.php" method="post" onSubmit="validateMe(); return document.returned">

<input type="text" id="myfield" name="myfield" size="20">

</form>


So there's a simple one-field validation. We can extend it to more fields, and even scrutinise the exact data people enter, but there's a starter for you. Search the net for javascript form validation, there's tonnes of stuff.
Copy linkTweet thisAlerts:
@MagikeyauthorJun 14.2004 — Thank you Mitya. I hadn't thought of that. I'm just learning PhP and all the pieces haven't come together yet. I plan to change that real soon.

Magikey.
Copy linkTweet thisAlerts:
@mityaJun 14.2004 — A wise decision, php is learner-friendly and very powerful. If you need any help then post again, and I can particularly recommend Larry Ullman's PHP for the World Wide Web book if you're wanting an aide.

Have fun.
Copy linkTweet thisAlerts:
@MagikeyauthorJun 14.2004 — Thanks again Mitya. I have the book. That was a major help to me while taking a six course. I'm okay doing piece work. Its putting it all together that has me really mystified.

Well, as the saying goes, "You must crawl before you walk."

Thanks again for your advice.

Magikey
×

Success!

Help @Magikey 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.19,
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,
)...