Problem with if condition using forms.

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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Problem with if condition using forms.

Post by JAM »

Manio wrote:Hi, I am new to this forum and to PHP. I have done some basic to intermediate level coding using Mysql and PHP. I was having a trouble with one of the codes, i hope you could help.

Following is the code of my main file after the authentication has been done.

Code: Select all

<?php
session_start();
$view = 1;
$globalid = $_SESSION["globalid"];
$link = mysql_connect('localhost');
if (!$link) {
   die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('student', $link);
if (!$db_selected) {
   die ('Can''t use student : ' . mysql_error());
}

if ($view == 1) {
        $query1 = "select bio.firstname as firstname, bio.lastname as lastname, bio.program as program,
        		  bio.focus as focus, bio.semester as semester, bio.year as year from bio
                  where bio.id = $globalid";
        $result1 = mysql_query($query1);
        $basicinfo = mysql_fetch_array($result1);
        mysql_free_result($result1);
        $query2 = "select alerts as alerts from alerts where alerts.id = 3";
        $result2 = mysql_query($query2);
        $alertsinfo = mysql_fetch_array($result2);
        mysql_free_result($result2);
        $view=2;
        require "studentserv_controller_view.php";
 }


 if ($_POST['submit'] == 'course_planner') {

        $redirect_to = "course_controller.php";
		require "redirect.php"; }
        $query1 = "select courses.courseid as courseid, courses.coursename as coursename, courses.instructorname as instructor,
        		  courses.semester as sem from courses where courses.semester = 'fall'";
        $result1 = mysql_query($query1);
        $num_rows = mysql_num_rows($result1);
        $i=1;
        mysql_data_seek($result1, 0);
        while ($row = mysql_fetch_array($result1) AND $i<$num_rows+1){
        $tmp1 = "courseid$i";
        ${$tmp1}= $row['courseid'];
        $tmp2 = "coursename$i";
        ${$tmp2} = $row['coursename'];
        $tmp3 = "instructor$i";
        ${$tmp3} = $row['instructor'];
        $tmp4 = "sem$i";
        ${$tmp4} = $row['sem'];
        $i++; }
        require "studentserv_controller_view.php";

?>
Manio wrote: The first if condition is working as wanted, but from the page student_controller_view.php the submit button which activates the second if condition seems to be non -functional. Here is the HTML of the student_controller_view.php.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Student services main</title>
<style type="text/css">
<!--
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 36px;
}
.style2 {
	color: #008000;
	font-weight: bold;
	font-family: Georgia, "Times New Roman", Times, serif;
}
.style3 {color: #804040}
body {
	background-color: #000000;
}
body,td,th {
	color: #FFFFFF;
}
.style4 {
	color: #FFFFFF;
	font-size: larger;
}
.style5 {font-family: Georgia, "Times New Roman", Times, serif; color: #008000;}
-->
</style>
</head>
<body>
      <form name="form1" method="post" action="studentserv_controller.php">
			<p align="left" class="style1">	<span class="style2"><img src="uab.JPG" width="168" height="58" align="left"> </span><span class="style5"><span class="style4">Student Services</span></span> </p>
			<hr>
			<p>
    <input type="submit" name="course_planner" value="Course Planner">

            </p>
    		<div id="Layer1" style="position:absolute; width:474px; height:118px; z-index:1; left: 140px; top: 121px; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000;">


	<textarea name="textarea" cols="65" rows="6"><?php print "$basicinfo[firstname] $basicinfo[lastname] \n$basicinfo[program] $basicinfo[focus] \n$basicinfo[semester] $basicinfo[year]" ?></textarea>


			</div>
   			<p>

    <input type="Submit" name="grades" value="Grades">

            </p>
			<p>

    <input type="Submit" name="payment" value="Payment">

            </p>
			<p>

    <input type="Submit" name="logout" value="logout">

            </p>
			<div id="Layer2" style="position:absolute; width:626px; height:343px; z-index:2; left: 142px; top: 272px; visibility: visible;">


	<textarea name="textarea" cols="100" rows="20"><?php print "$courseid1 $courseid2 $courseid3" ?></textarea>
    <? //$coursename[2] \n\n  $instruct[3] \n\n  $sem $num_rows" ?>
    <?php print "$num_rows $courseid5 $courseid0 $courseid3 $dummy $tmp1" ?>
    </form>
			</div>
			<p>&nbsp; 	</p>
			<p class="style3">&nbsp;
			</p>
            <p align="center">&nbsp;        </p>
			<p>&nbsp;</p>
            <p align="center">&nbsp;</p>
			<p align="center">&nbsp;</p>
			<p align="center"><img src="ncaab_uabblazers.jpg" width="94" height="72" align="left"></p>
</body>
</html>
Manio wrote:A little shabby but i hope you could help?Thanks in advance.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I see you do:
if ($_POST['submit'] == 'course_planner') {
and the form says:
<input type="submit" name="course_planner" value="Course Planner">

Then the value of $_POST['submit'] would be 'Course Planner' and not 'course_planner', so try:
if ($_POST['submit'] == 'Course Planner') { instead.
Manio
Forum Newbie
Posts: 2
Joined: Thu Apr 15, 2004 6:48 pm
Location: Alabama Birmingham

Post by Manio »

Hmm... thanks a lot. But with this another problem has arisen, the html pages of the two _view.php (PHP in HTML) files start overlapping.

Is there a way to close the page called in the the first if condition, before loading the second one?

Thanks
Manio
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well my tired eyes can't spot the exact point this is happening in your code, but the usual way of doing this type of thing is:
if(somecondition){
...do stuff...
include 'somepage.php';
} else {
...do stuff...
include 'someotherpage.php';
}

Sorry if this is too generic, if so let us know and we'll take another look at your code.
Manio
Forum Newbie
Posts: 2
Joined: Thu Apr 15, 2004 6:48 pm
Location: Alabama Birmingham

Post by Manio »

Thanks, It was helpful. I have figured out some more trivial problems.

Manio
Post Reply