Need assistance in print/echo html code with if fuction

I have used the below php code

[php]<?php
$numberforms = $_POST[‘D1’];
print “you have selected $numberforms fields, click back on your browser if this is incorrect”;
if
( $numberforms == “1” );
print ( a whole load of html code );
} else {
print “please make a valid selection”;
}
?>[/php]

and this is not working. Can somebody please give me the correct code to print the html code.

All help is greatly appreciated.

Just put all of the HTML code in a separate file and then use an INCLUDE instead of a PRINT.

Also check your Syntax on the IF statement.

[php]<?php
$numberforms = $_POST[‘D1’];
print “you have selected $numberforms fields, click back on your browser if this is incorrect”;
if ( $numberforms == “1” ) {
include (‘path/to/file/with a whole load of html code’ );
} else {
print “please make a valid selection”;
}
?>[/php]

OK, I have got that working, but now face another problem.

I have the below code which I use to say if the drop down menu = 2, show the file form2.php but it is showing lots of other pages.

[php]<?php
$numberforms = $_POST[‘D1’];
print “you have selected $numberforms fields, click back on your browser if this is incorrect”;
if
( $numberforms == “1” ); {
include (‘form.php’ );
}
if
( $numberforms == “2” ); {
include (‘form2.php’ );
}
if
( $numberforms == “3” ); {
include (‘form3.php’ );
}
if
( $numberforms == “4” ); {
include (‘form4.php’ );
}
if
( $numberforms == “5” ); {
include (‘form5.php’ );
}
if
( $numberforms == “6” ); {
include (‘form6.php’ );
}
if
( $numberforms == “7” ); {
include (‘form7.php’ );
}if
( $numberforms == “8” ); {
include (‘form8.php’ );
}
if
( $numberforms == “9” ); {
include (‘form9.php’ );
}
if
( $numberforms == “10” ); {
include (‘form10.php’ );
}[/php]

If you try out my script on http://www.07005.net/test/how%20many%20forms.htm and select any from the drop down menu you can see that it is including all 20 form.php files.

I know the reason for this and this is because I have simply entered the code include (‘formx.php’); and this is the problem.

I need to make it so that if $numberfroms == “5” THEN include form5.php and not all the others.

Please help!

You should try else statements instead of a bunch if like this:

[php]<?
$numberforms = $_POST[‘D1’];
print “you have selected $numberforms fields, click back on your browser if this is incorrect”;
if
( $numberforms == 1 ) {
include (‘form.php’ );
}
elseif
( $numberforms == 2 ) {
include (‘form2.php’ );
}
elseif
( $numberforms == 3 ) {
include (‘form3.php’ );
}
elseif
( $numberforms == 4 ) {
include (‘form4.php’ );
}
elseif
( $numberforms == 5 ) {
include (‘form5.php’ );
}
elseif
( $numberforms == 6 ) {
include (‘form6.php’ );
}
elseif
( $numberforms == 7 ) {
include (‘form7.php’ );
}
elseif
( $numberforms == 8 ) {
include (‘form8.php’ );
}
elseif
( $numberforms == 9 ) {
include (‘form9.php’ );
}
else
( $numberforms == 10 ) {
include (‘form10.php’ );
}
?>[/php]

I also noticed you were using a semi-colon after your if statements this is incorrect. I removed the double qoutes around your numbers because you really don’t need them there when dealing with numbers. That shouldn’t make a differance either way. Try the corrected code above to see if it changes anything.

Better to use a SWITCH function:

[php]

<? $numberforms = $_POST['D1']; print "you have selected $numberforms fields, click back on your browser if this is incorrect"; switch ($nubmerforms) { case 1: include ('form.php' ); break; case 2: include ('form2.php' ); break; case 3: include ('form3.php' ); break; case 4: include ('form4.php' ); break; case 5: include ('form5.php' ); break; case6: include ('form6.php' ); break; case 7: include ('form7.php' ); break; case 8: include ('form8.php' ); break; case 9: include ('form9.php' ); break; case 10: include ('form10.php' ); break; } ?>[/php]

You should also have a default form as well if no other case is relevant:
http://us2.php.net/switch

If all your forms are in the form of formX.php then why not just do this?

[php]

<? $numberforms = $_POST['D1']; print "you have selected $numberforms fields, click back on your browser if this is incorrect"; if ($numberforms <=10 ) { include ('form'.$nubmerforms.'php'); } else { echo " The form you selected does not exist"; } ?>

[/php]

Good point Paul… Very smart thinking by the way on the whole code crunch. I didn’t even think of it. :D

:oops:

I should be the one blushing! :oops:

Just to let you all know I have found this out :smiley:

Glad to be of help! Ok…ok…Paul did pretty much all of the work, but I tried!

lol, thank for everyone who helps me! If any one needs some hosting for some test php scripts let me know :D

Sponsor our Newsletter | Privacy Policy | Terms of Service