Validation script for standard value="" in input f


#1

Hi someone,

First of all, let me present my self and my level of skills: I have been writing HTML (not always valid though) for about five years now on and off, and has recently dived into the wonderful world of php. The majority of the scripts I use run on the pages I will be presenting in this forum are copyed, written off or based upon/tested based on the scripts I’ve found. So eventhough the scripts has got a couple of advanced skill properties, does not indicate that I am a skilled php programmer… So there you have it…

Here’s my case:

I have a simple form at this adress:
http://www.tvnt.no/nytipsform/tips-del1.php
(In Norwegian, but shouldn’t really matter…)

Obviously you’ll notice that every field in the form has a predifined value i.e. using the value=“Emne” in the input tag. This is for space efficiency

Now, to the mindwobbler:
I am trying to make a script (as earlier mentioned: Based upon the existing code I’ve found for validation) that can verify that the form is not being sent with the standard values… I.e. Where Emne (Subject) is not “Emne” or Navn (Name) is not “Ditt Navn” (“Your name”)…

Here’s the code from the php-file
(I don’t know which part you will be needing for the troubleshooting so I’m pasting the whole file in this post. Sorry for this… And PLEASE, comment on what I’m asking about only! I know the code might be a bit messy, but most of the code is working the way I want it to.)

[code]

Tips oss <?php if ($_SERVER['REQUEST_METHOD'] != 'POST'){ $me = $_SERVER['PHP_SELF']; ?>
Sett inn emne her...
Sett inn navn her...
Sett inn e-post her...
Sett inn bosted her...
Sett inn tlf her...
Din beskjed
	</div>
	<div id="tipbubblebg-message">
		<div id="tipbubble-message">
		Sett inn <b>beskjed</b> her...
		</div>
	</div>
	<div id="button1div">
		<input id="resetbutton" type="reset" name="Reset" value="Angre" onfocus="this.blur();" />
	</div>
	<div id="button2div">
		<input id="submitbutton" type="submit" name="Submit" value="Send" onfocus="this.blur();" />
	</div>
</div>
<div id="validationmarks">
		<a href="http://jigsaw.w3.org/css-validator/">
		<img	style="border:0;width:88px;height:31px"
				src="http://jigsaw.w3.org/css-validator/images/vcss" 
				alt="Valid CSS!" />
	</a>
		<a href="http://validator.w3.org/check?uri=referer">
		<img	style="border:0;width:88px;height:31px"
				src="http://www.w3.org/Icons/valid-xhtml10"
				alt="Valid XHTML 1.0 Transitional" />
	</a>
</div>
</form>
<?php
} else {
	// initialize a variable to 
	// put any errors we encounter into an array
	$errors = array();
	
	// test to see if the form was actually 
	// posted from our form
	$page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
	
	//puts illegal values in variables
	$illegalSubject = 'Emne';
	$illegalName = 'Ditt Navn';
	$illegalMail = 'din@mailadresse';
	$illegalCity = 'By / kommune';
	$illegalTel = 'Telefonnummer';
	$illegalMessage = 'Din beskjed';

	if (!ereg($page, $_SERVER['HTTP_REFERER']))
		$errors[] = "Ugyldig referanseskjema!n";
	// check to see if a name was entered
	if (!$_POST['Name'])
		// if not, add that error to our array
		$errors[] = "Feltet "Ditt Navn" kan ikke v?re tomt!";

/*[/code]
Here’s the code that I can’t udnerstand! How to check if $_POST[‘Name’] equals $illegalName and outputting the error to the $errors[] array???

[code]
*/
if (!$_POST[‘Name’] == $illegalName )
$errors[] = “Feltet “Ditt Navn” kan ikke fylles ut med standardverdien “Ditt Navn”!”;

	if (!$_POST['Subject'])
		// if not, add that error to our array
		$errors[] = "Emne er et obligatorisk felt!";
	// check to see if a subject was entered
	if (!$_POST['Tel'])
		// if not, add that error to our array
		$errors[] = "Telefonnummer er et obligatorisk felt!";
	// check to see if a message was entered
	if (!$_POST['Message'])
		// if not, add that error to our array
		$errors[] = "Din beskjed er et obligatorisk felt!";
	// if there are any errors, display them
	if (count($errors)>0){
		echo "<p id="header">F&Oslash;LGENDE FEIL OPPSTO:</p>n<p id="error">";
		foreach($errors as $err)
			echo "$err<br>n";
		echo "</p>";
	} else {
		error_reporting(0);
		$recipent	=	'andreas@aarlott.com';
		$subject	=	"Fra tvnt.no tipsskjema [".$_POST['Subject']."]";
		$fromname	=	ucwords(stripslashes($_POST['Name']));
		$frommail	=	$_POST['Mail'];
		$city		=	$_POST['City'];
		$tel		=	$_POST['Tel'];
		$sender		=	"From: ".$fromname." <".$frommail.">rn";
		$message	=	"Tips fra $fromname fra $citynnKontaktinformasjon:nTelefonnummer: $telnE-post-adresse: $frommailnnBeskjed:n".stripslashes($_POST['Message']);
		$message	=	convert_cyr_string($message,'d','w');
		$message	=	wordwrap($message, 70);
		$message	=	strtr($message,"|+"," ?");
		if(mail($recipent, $subject, $message, $sender))
			echo ("<p id="header">").strtoupper("Beskjeden er sendt").nl2br(".</p><p>
			<b>Tusen takk for ditt tips!</b>
			
			Til: Redaksjonen, TVNord-Tr&oslash;ndelag < $recipent >
			Emne: $subject
			
			$message
			</p>");
		else
			echo("
			<p><b>Beklager!</b>
			<br />Beskjeden kunne ikke leveres!
			<br />Dette kan skyldes en intern feil eller en serverfeil.
			</p>
			<p>
			<b>Vennligst pr&oslash;v igjen senere.</b>
			</p>
			<p id="dimtext">Hvis du &oslash;nsker &aring; rapportere feilen,
			<br />send da en e-post til <a href="mailto:andreas.aarlott@tvnt.no">andreas.aarlott@tvnt.no</a>
			</p>
			");
		}
	}
?>
[/code]

#2

Btw, I tried changing the couple of lines to:

if (!$_POST['Name'] != $illegalName ) $errors[] = "Feltet "Ditt Navn" kan ikke fylles ut med standardverdien "Ditt Navn"!";

But then the errormessage is always put into the errors[] array reguardless of what value I’ve typed into the input field…

Anyone?


#3

If you want to check if a variable is equal to some value you will want the following:

[php]

<? // This will check for an empty value and if it is equal to the illegal name variable. if (empty($_POST['Name']) || $_POST['Name'] == $illegalName ) { $errors[] = "Feltet "Ditt Navn" kan ikke fylles ut med standardverdien "Ditt Navn"!"; } [/php] You need to check if the variable is equal to the value which is ==, but you will want to make sure they also didn't send a blank value as well. Which is empty($variable) or $variable == "" Hope that helps.

#4

Thank you very much for the reply. BUT…

Seems like it’s letting the $illegalName variable pass through… :’(

It doesn’t validate. Just lets the post through somehow even if the field is entered with the value “Ditt Navn” (the $illegalName - variable)

What am I doing wrong? Or could this be a server side settings error?

(I know that tha ISP has safe_mode on and they are runnig PHP4 or PHP5 Useroptional on the server. (Mine is set to Ver 5), register_globals is set to off and php error messages is set to on. If that’s of any help…)

Anyone? I’d be greatful…


#5

Sorry for not seeing this earlier… You are using…

Ditt Navn for the illegal name.

But you set the value to ditt navn.

These are not equal. They are in different case and case matters when PHP does checking.

Hope that helps!


#6

OMFG! I feel so freaking stupid right now!

How come I didn’t see that?? I even used the ucwords() function for this field further down the code! I should have seen this myself!

But thank you for the splendid help anyway! Sorry for asking dumb questions… :frowning:


#7

Your welcome and not a big deal!

Just glad I could, sometimes its just the need for a second pair of eyes to see something we are missing :)

Been there, done that a lot!