0

hey
i just started learning php programming, i wrote this code, although the HTML code and forms and everything is working but when i click on submit nothing happens.

the code :-

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<?php
	if(isset($_POST['submitted']))
	{
		if((isset($_POST['name']))&&(isset($_POST['school']))&&(isset($_POST['marks']))&&(isset($_POST['sex'])))
		{
			if($_POST['marks']>=80)
			  $grade='A';
			  else if(($_POST['marks']>=70)&&($_POST['marks']<80))
			  $grade='B';
			     else if(($_POST['marks']<70)&&($_POST['marks']>=60))
				 $grade='C';
				    else 
					$grade='D';
			echo "<p>".$_POST['name']." of ".$_POST['school']." has scored an average of ".$_POST['marks']." percentage. His/Her grade is :- ".$grade."</p>";
		}
	}
	function calendar()
	{
		$month=array(1=>"January","February","March","April","May","June","July","August","September","Ocotber","November","December");
		$days=range(1,31);
		$years=range(1900,2011);
		echo "Date of Birth : ";
		echo '<select name="month">';
		foreach($month as $key=>$value){
			echo '<option value='.$key.'">'.$value.'</option>';
		}
		echo "</select>";
		
		echo '<select name="days">';
		foreach($days as $key=>$value){
			echo '<option value="'.$key.'">'.$value.'</option>';
		}
		echo "</select>";
		
		echo '<select name="year">';
		foreach($years as $key=>$value){
			echo '<option value='.$key.'">'.$value.'</option>';
		}
		echo '</select>';
		echo "<br /><br />";
	}	
?>
<fieldset>
<legend>Form 1</legend>
<form action="test.php" method="post">
Name : <input type="text" name="name" size="35" /><br /><br />
School : <input type="text" name="school" size="35" /><br /><br />
Avg. Marks : <input type="text" name="avg" size="4"/><br /><br />
Sex : <input type="radio" name="sex" value="m" />Male <input type="radio" name="sex" value="f" />Female<br /><br />
<?php calendar() ?>
<input type="submit" name="submit" value="Submit Form" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</fieldset>
<?php
include("footer.html");
?>
4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by dhruv_arora
0

Generally, it's elseif, not else if. But what doesn't work? Do some logging, does php enter the if, what does it assign to $grade, etcetera.

0

It does exactly what you programmed it to do!

The first that you could do to see what is happening is to insert a

print_r($_POST);

between lines 6 and 7. If you then run the code, you will see that all of your form variables have been passed back to the module (assuming that this module is called test.php - you don't need the action parameter by the way if you want it to come back to this module).

Then, you need to look at your IF statements on lines 7 and 9 to see if the conditions are satisfied. You will see that is true for line 7 but it might not be for line 9. The main thing is what happens when it gets to line 21. At that point, you have finished processing the form. Logically, the program should stop at this point. Since you didn't put an exit statement before line 21, it keeps right on going and displays the form again starting at line 50. If the 'If' statement at line 9 wasn't satisfied, then it will look as if nothing happened because the form will just be displayed again.

This problem involves how you think through your design and how you debug it if it doesn't work. The php isn't that big a factor. If you don't do the design and the debugging well, then I think you will find developing programs to be quite slow and frustrating. For the debugging part, you can only get better at it if you do it. Initially, it takes time but that's the only way that you can get better at it. If something isn't working your need to review what you've done and if nothing is obvious, then you need to start dumping values at key points to confirm the flow of control and see if the variables are what you expected them to be. Inserting one or two simple debugging statements into your code would have told you what was happening very quickly.

0

It does exactly what you programmed it to do!

That's the good and bad thing about computers, they do exactly what you tell them to do :)

Edited by twiss: n/a

1

just playing while the coffe brews,
the only thing I really noticed, was the difference in fieldnames in the original code
the html form inputs 'avg'
the php form processor validates on $_POST
= always rejects

added some (bad) code that populates the form with partial values more as an exercise than with any real consideration of whether it was valid

<?php 
include("header.html");
include("nav.html");
include("sidebars.html");
if(isset($_POST['submitted'])) {
	if((isset($_POST['name']))&&(isset($_POST['school']))&&(isset($_POST['marks']))&&(isset($_POST['sex']))) {
		if($_POST['marks']>=80)	 $grade='A';
		elseif(($_POST['marks']>=70)&&($_POST['marks']<80)) $grade='B';
		elseif(($_POST['marks']<70)&&($_POST['marks']>=60)) $grade='C';
		else $grade='D';
		echo "<p>".$_POST['name']." of ".$_POST['school']." has scored an average of ".$_POST['marks']." percentage. His/Her grade is :- ".$grade."</p>"; 
	exit(0); 
	} 
	else echo 'submitted form was not complete';
	}

function calendar() {
	$month=array(1=>"January","February","March","April","May","June","July","August","September","Ocotber","November","December");
	$days=range(1,31);
	$years=range(1900,2011);
	echo "Date of Birth : ";
	echo '<select name="month">';
	foreach($month as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['month'])){if($key==$_post['month']) echo "selected='selected'";}
		echo '>$value</option>';
		}
	echo "</select>";
	echo '<select name="days">';
	foreach($days as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['days'])){if($key==$_post['days']) echo 'selected="selected"';}
		echo ">$value</option>";
		}
	echo "</select>";
	echo '<select name="year">';
	foreach($years as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['year'])){if($key==$_post['year']) echo 'selected="selected"';}
		echo ">$value</option>";
		}
	echo '</select><br /><br />';
	}	
?>
<form action="test.php" method="post">
<fieldset>
<legend>Form 1</legend>
Name : <input type="text" name="name" size="35" <?php if(isset($_POST['name'])){echo "value='$_POST['name']'";} ?>/><br /><br />
School : <input type="text" name="school" size="35" <?php if(isset($_POST['school'])){echo "value='$_POST['school']'";} ?>/><br /><br />
Avg. Marks : <input type="text" name="marks" size="4" <?php if(isset($_POST['marks'])){echo "value='$_POST['marks']'";} ?>/><br /><br />
Sex : <input type="radio" name="sex" value="m" />Male <input type="radio" name="sex" value="f" />Female<br /><br /> <!-- something similar to add selected='selected' -->
<?php calendar() ?>
<input type="submit" name="submit" value="Submit Form" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</fieldset>
<?php include("footer.html"); ?>

Edited by almostbob: n/a

0
echo "<p>$_POST['name'] of $_POST['school'] has scored an average of .$_POST['marks'] percent. ".($POST['sex']=='f' ? 'Her' : 'His')." grade is :- $grade.</p>";

coffee's drunk, break over

Edited by almostbob: n/a

0

just playing while the coffe brews,
the only thing I really noticed, was the difference in fieldnames in the original code
the html form inputs 'avg'
the php form processor validates on $_POST
= always rejects

added some (bad) code that populates the form with partial values more as an exercise than with any real consideration of whether it was valid

<?php 
include("header.html");
include("nav.html");
include("sidebars.html");
if(isset($_POST['submitted'])) {
	if((isset($_POST['name']))&&(isset($_POST['school']))&&(isset($_POST['marks']))&&(isset($_POST['sex']))) {
		if($_POST['marks']>=80)	 $grade='A';
		elseif(($_POST['marks']>=70)&&($_POST['marks']<80)) $grade='B';
		elseif(($_POST['marks']<70)&&($_POST['marks']>=60)) $grade='C';
		else $grade='D';
		echo "<p>".$_POST['name']." of ".$_POST['school']." has scored an average of ".$_POST['marks']." percentage. His/Her grade is :- ".$grade."</p>"; 
	exit(0); 
	} 
	else echo 'submitted form was not complete';
	}

function calendar() {
	$month=array(1=>"January","February","March","April","May","June","July","August","September","Ocotber","November","December");
	$days=range(1,31);
	$years=range(1900,2011);
	echo "Date of Birth : ";
	echo '<select name="month">';
	foreach($month as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['month'])){if($key==$_post['month']) echo "selected='selected'";}
		echo '>$value</option>';
		}
	echo "</select>";
	echo '<select name="days">';
	foreach($days as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['days'])){if($key==$_post['days']) echo 'selected="selected"';}
		echo ">$value</option>";
		}
	echo "</select>";
	echo '<select name="year">';
	foreach($years as $key=>$value){
		echo "<option value='$key'";
		if(isset($_POST['year'])){if($key==$_post['year']) echo 'selected="selected"';}
		echo ">$value</option>";
		}
	echo '</select><br /><br />';
	}	
?>
<form action="test.php" method="post">
<fieldset>
<legend>Form 1</legend>
Name : <input type="text" name="name" size="35" <?php if(isset($_POST['name'])){echo "value='$_POST['name']'";} ?>/><br /><br />
School : <input type="text" name="school" size="35" <?php if(isset($_POST['school'])){echo "value='$_POST['school']'";} ?>/><br /><br />
Avg. Marks : <input type="text" name="marks" size="4" <?php if(isset($_POST['marks'])){echo "value='$_POST['marks']'";} ?>/><br /><br />
Sex : <input type="radio" name="sex" value="m" />Male <input type="radio" name="sex" value="f" />Female<br /><br /> <!-- something similar to add selected='selected' -->
<?php calendar() ?>
<input type="submit" name="submit" value="Submit Form" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</fieldset>
<?php include("footer.html"); ?>

problem solved, after i changed $_POST to $_POST, everything got solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.