Validation fails

Hi everybody,

I’ve been writing a health calculator for a school project for about 2 days now and so far so good it all works fine, untill my teacher told me you could leave fields blank and the calculation would still be performed so I started writing a validation page, but it doesn’t work for even a tiny bit. So I’m asking for your help here.

First you have the page/ form where information is wrote

[code]

input{ width: 200px; } td{ width:150px; text-align:center; border:0px black solid; } Gezondheidstest
	<td><input type="text" name="age"
    	 onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Typ hier uw leeftijd':this.value;" value="Typ hier uw leeftijd" 		     					 			 onchange="if (/^\.?$/.test(this.value) || !/^-?\d*\.?\d*$/.test(this.value)) {alert('This field requires a Number'); this.value=''; this.focus()}" />
    </td>
</tr>    
<tr>    
	<td><input type="text" name="length" value="Typ hier uw lengte in in centimeter bv. 179" 
    	onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Typ hier uw lengte in centimeter bv. 179':this.value;" 
        onchange="if (/^\.?$/.test(this.value) || !/^-?\d*\.?\d*$/.test(this.value)) {alert('This field requires a Number'); this.value=''; this.focus()}" />
    </td>
    
	<td><input type="text" name="weight" value="Typ hier uw gewicht in Kg in" 
    	onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Typ hier uw gewicht in Kg in':this.value;" 
        onchange="if (/^\.?$/.test(this.value) || !/^-?\d*\.?\d*$/.test(this.value)) {alert('This field requires a Number'); this.value=''; this.focus()}" />
    </td>
Geslacht? - - Man Vrouw
Rookt je? - - nee ja
Hoeveel uur per week sport je?
0-3h
3-6h
6-10h
+10h
Verwijder gegevens
[/code]

And then the validation page comes (validate.php)

[php]

processing... <?php $sport=$_POST['sport']; $name=$_POST['name']; $weight=$_POST['weight']; $length=$_POST['length']; $smoke=$_POST['smoke']; $age=$_POST['age']; $sex=$_POST['sex']; //Variabelen ophalen uit input form
if ($name = '' or $name = 'Typ hier uw naam') {$incorrectName = '<li>Vul je naam in asjeblieft</li>';}
if ($weight = '' or $weight = 'Typ hier uw gewicht in Kg in') {$incorrectWeight = '<li>Vul je gewicht in asjeblieft</li>';}
if ($length = ''or $length = 'Typ hier uw lengte in in centimeter bv. 179') {$incorrectLength = '<li>Vul je lengte in asjeblieft</li>';}
if ($age = '' or $age= 'Typ hier uw leeftijd') {$incorrectAge = '<li>Vul je leeftijd in asjeblieft</li>';}
if ($sex = '') {$incorrectSex = '<li>Vul je geslacht in asjeblieft</li>';}
if ($smoke = '') {$incorrectSmoke = '<li>Vul in of je rookt asjeblieft</li>';}
if ($sport = '') {$incorrectSport = '<li>Vul in hoeveel je ongeveer sport asjeblieft</li>';} //incorrect gegevens aanduiden

		if ($name = ""
			or
			$length = ""
			or 
			$weight = ""
			or
			$age = ""
			or
			$smoke = ''
			or
			$sport = ''
			or
			$sex = '') {print '<ul>'.$incorrectName.''.$incorrectAge.''.$incorrectLength.''.$incorrectWeight.''.$incorrectSex.''.$incorrectSmoke.''.$incorrectSport.'</ul><br /><a href="gezondheidstest.html">Klik hier om terug te gaan naar het formulier</a>';}
			else {
				include "calculate.php" ;}

?>

[/php]

And then normally the actually calculation (calculate.php) would show, but it isn’t showing at all

[php]

body{ font-size:13pt; font-family:Arial, Helvetica, sans-serif; } .note{ font-size:11pt; font-family:"Times New Roman", Times, serif; font-style:italic; color: #666; } em{ font-size:13pt; font-family:Arial, Helvetica, sans-serif; color:red; } calculate [/php]

It’s meant to display errors when field/options are left blank or when a default value isn’t changed (for example the name input). And I know that you’ll most likely think it could’ve been written a lot better but I’m only writing php for 2 days now so please try to miminize comments about this issue.

Hoping for help
Greetings

Yoram

Don’t worry about that too much for now - we all have to start somewhere. But I would recommend that, when you become more experienced at the language, it’s a good idea to see how you can improve your code and programming skills in general.

In terms of the default values, could you echo out the values of the $_POST variables (not the values of them when they are in $_POST, but after this code:

[php] if ($name = ‘’ or $name = ‘Typ hier uw naam’) {$incorrectName = ‘

  • Vul je naam in asjeblieft
  • ’;} if ($weight = ‘’ or $weight = ‘Typ hier uw gewicht in Kg in’) {$incorrectWeight = ‘
  • Vul je gewicht in asjeblieft
  • ’;} if ($length = ''or $length = ‘Typ hier uw lengte in in centimeter bv. 179’) {$incorrectLength = ‘
  • Vul je lengte in asjeblieft
  • ’;} if ($age = ‘’ or $age= ‘Typ hier uw leeftijd’) {$incorrectAge = ‘
  • Vul je leeftijd in asjeblieft
  • ’;} if ($sex = ‘’) {$incorrectSex = ‘
  • Vul je geslacht in asjeblieft
  • ’;} if ($smoke = ‘’) {$incorrectSmoke = ‘
  • Vul in of je rookt asjeblieft
  • ’;} if ($sport = ‘’) {$incorrectSport = ‘
  • Vul in hoeveel je ongeveer sport asjeblieft
  • ’;} //incorrect gegevens aanduiden[/php]

    E.g:

    [php]echo $name, ‘
    ’;
    echo $weight, ‘
    ’;
    etc etc.[/php]

    Hi there,

    First of all, thank you very much for helping me out. But I don’t really understand what you’re asking me to do, could you maybe try saying it a bit less in “php-terms” (I know it’s stupid but total new guy over here).

    greets

    Yoram

    You have a part of code that gets variables from the $_POST array (sent by the users’ form) and then puts them into their own variables for easy use:

    [php] $sport=$_POST[‘sport’];
    $name=$_POST[‘name’];
    $weight=$_POST[‘weight’];
    $length=$_POST[‘length’];
    $smoke=$_POST[‘smoke’];
    $age=$_POST[‘age’];
    $sex=$_POST[‘sex’]; //Variabelen ophalen uit input form[/php]

    Then you have code that checks their values:

    [php] if ($name = ‘’ or $name = ‘Typ hier uw naam’) {$incorrectName = ‘

  • Vul je naam in asjeblieft
  • ’;}
    if ($weight = ‘’ or $weight = ‘Typ hier uw gewicht in Kg in’) {$incorrectWeight = ‘
  • Vul je gewicht in asjeblieft
  • ’;}
    if ($length = ''or $length = ‘Typ hier uw lengte in in centimeter bv. 179’) {$incorrectLength = ‘
  • Vul je lengte in asjeblieft
  • ’;}
    if ($age = ‘’ or $age= ‘Typ hier uw leeftijd’) {$incorrectAge = ‘
  • Vul je leeftijd in asjeblieft
  • ’;}
    if ($sex = ‘’) {$incorrectSex = ‘
  • Vul je geslacht in asjeblieft
  • ’;}
    if ($smoke = ‘’) {$incorrectSmoke = ‘
  • Vul in of je rookt asjeblieft
  • ’;}
    if ($sport = ‘’) {$incorrectSport = ‘
  • Vul in hoeveel je ongeveer sport asjeblieft
  • ’;} //incorrect gegevens aanduiden[/php]

    After that piece of code, please echo out all of the variables that you are using:

    [php]echo $name, ‘
    ’;
    echo $weight, ‘
    ’;
    echo $length, ‘
    ’;
    echo $age, ‘
    ’;
    echo $sex, ‘
    ’;
    echo $smoke, ‘
    ’;
    echo $sport;[/php]

    So I did like you told me

    [php]if ($name = ‘’ or $name = ‘Typ hier uw naam’) {$incorrectName = ‘

  • Vul je naam in asjeblieft
  • ’;}
    if ($weight = ‘’ or $weight = ‘Typ hier uw gewicht in Kg in’) {$incorrectWeight = ‘
  • Vul je gewicht in asjeblieft
  • ’;}
    if ($length = ''or $length = ‘Typ hier uw lengte in in centimeter bv. 179’) {$incorrectLength = ‘
  • Vul je lengte in asjeblieft
  • ’;}
    if ($age = ‘’ or $age= ‘Typ hier uw leeftijd’) {$incorrectAge = ‘
  • Vul je leeftijd in asjeblieft
  • ’;}
    if ($sex = ‘’) {$incorrectSex = ‘
  • Vul je geslacht in asjeblieft
  • ’;}
    if ($smoke = ‘’) {$incorrectSmoke = ‘
  • Vul in of je rookt asjeblieft
  • ’;}
    if ($sport = ‘’) {$incorrectSport = ‘
  • Vul in hoeveel je ongeveer sport asjeblieft
  • ’;} //incorrect gegevens aanduiden
    echo $name, '<br>';
    echo $weight, '<br>';
    echo $length, '<br>';
    echo $age, '<br>';
    echo $sex, '<br>';
    echo $smoke, '<br>';
    echo $sport;[/php]
    

    but al it does is echo out the predefined value’s for $name, $weight, $length and $age (showing the output in html below)

    [code]processing…

    Typ hier uw naam
    Typ hier uw gewicht in Kg in
    Typ hier uw lengte in in centimeter bv. 179
    Typ hier uw leeftijd


    [/code]

    Apologies for not spotting this earlier!

    [php] if ($name = “”
    or
    $length = “”
    or
    $weight = “”
    or
    $age = “”
    or
    $smoke = ‘’
    or
    $sport = ‘’
    or
    $sex = ‘’) {print ‘

      ’.$incorrectName.’’.$incorrectAge.’’.$incorrectLength.’’.$incorrectWeight.’’.$incorrectSex.’’.$incorrectSmoke.’’.$incorrectSport.‘

    Klik hier om terug te gaan naar het formulier’;}
    else {
    include “calculate.php” ;}[/php]

    You should have double == as you’re comparing the values!

    Thank you so much!
    It validates now and if everything it filled out it does the calculation but for some reason if you don’t fill in the $sex, $smoke or $sport variables it doesn’t show the appropriate warning in in the validation page, any ideas how this could come?

    greetings and thank you so much for your help so far!

    Yoram

    Can you post the output of:

    [php]print_r($_POST);[/php]

    (Placed at the top of your code). This will allow us to see what values are being given to those fields.

    Hi again,

    I found the problem, it was a typo, so now everything is working just fine.
    I want to thank you so much for your time and really appreciate your help.

    Greets

    Yoram

    Sponsor our Newsletter | Privacy Policy | Terms of Service