help...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elitefreke
Forum Newbie
Posts: 3
Joined: Wed Sep 10, 2003 2:26 am

help...

Post by elitefreke »

I am trying to figure out the code for making a field "required" Basically, I just don't want to get a submission with no info and I cannot seem to grasp the concept of the compare features. Please help.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as simple example:

Code: Select all

<html>
	<head>
		<title>form test</title>
		<style texpe="text/css">
			span.error { display: block; color: red; }
		</style>
	</head>
	<body>
	<?php
		if (!isset($_POST['fieldA']) || strlen($_POST['fieldA'])==0)
			echo '<span class="error">fieldA is mandatory</span>';
		if (!isset($_POST['fieldB']) || strlen($_POST['fieldB'])==0)
			echo '<span class="error">fieldB is mandatory</span>';
		if (!isset($_POST['fieldC']) || strlen($_POST['fieldC'])==0)
			echo '<span class="error">fieldC is mandatory</span>';
	?>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<input type="text" name="fieldA" value="<?php echo @$_POST['fieldA']; ?>"/> :field A<br />
			<input type="text" name="fieldB" value="<?php echo @$_POST['fieldB']; ?>"/> :field B<br />
			<input type="text" name="fieldC" value="<?php echo @$_POST['fieldC']; ?>"/> :field C<br />
			<input type="submit" />	
		</form>
	</body>
</html>
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Post by mathewvp »

if you want to check before submitting the form whether user has entered all the required fields,you should use Javascript
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

mathewvp wrote:if you want to check before submitting the form whether user has entered all the required fields,you should use Javascript
But you should also check server-side (i.e. with PHP) in case the person has turned off JavaScript or doesn't have it available to them.

Mac
Post Reply