34drysdale Posted March 15, 2011 Share Posted March 15, 2011 Hi everyone! I'm new to PHP and needed from you. I have the following code that I can't figure out why it doesn't work on the last elseif. If I comment out the first elseif (line 19-39), it does go through the last elseif. Can someone here help? Thank you!!! The content of Instructors.txt as follow: ID#|fName lName|email_address|course1:course2:course3 ID#|fName lName|email_address|course1:course3 ID#|fName lName|email_address|course1:course2:course4 ID#|fName lName|email_address|course2:course3:course5:course6 <?php //If we submitted the form if(empty($_POST['Location'])) { $FH = fopen("Locations.txt", "rb"); print "<FORM name=\"myForm\" action=\"test4.php\" method=\"POST\">\n"; print "<P> Location:\n"; print "<select name=\"Location\">\n"; print "<option value=\"\">-- Select --\n"; while (!feof($FH)){ $line_of_text = fgets($FH); $line = explode('|', $line_of_text); print "<option value=$line[1]>$line[1]"; } fclose($FH); print "<input type=\"submit\" value=\" Next 1 \">\n"; exit(); } elseif(empty($_POST['Instructor'])) { $location = ($_POST['Location']); $FH = fopen("Instructors.txt", "rb"); print "<FORM name=\"myForm\" action=\"test4.php\" method=\"POST\">\n"; print "<P> Location: <input type=text value=$location>\n"; print "<P> Instructor: \n"; print "<select name=\"Instructor\">\n"; print "<option value=\"\">-- Select --\n"; while (!feof($FH)){ $line_of_text = fgets($FH); $line = explode('|', $line_of_text); if ($line[0] == "$location") { print "<option value=\"$line[1]\">$line[1]\n"; } } fclose($FH); print "<input type=\"submit\" value=\" Next 2 \">\n"; exit(); } elseif(empty($_POST['Course'])) { $location = ($_POST['Location']); $instructor = ($_POST['Instructor']); $FH = fopen("Instructors.txt", "rb"); print "<FORM name=\"myForm\" action=\"test4.php\" method=\"POST\">\n"; print "<P> Location: <input type=text value=$location>\n"; print "<P> Instructor: <input type=text value=\"$instructor\">\n"; print "<P> Course: \n"; print "<select name=\"Course\">\n"; print "<option value=\"\">-- Select --\n"; while (!feof($FH)){ $line_of_text = fgets($FH); $line = explode('|', $line_of_text); if ($line[1] == "Bert Vandenber") { $course = explode(':', $line[3]); for ($i=0; $i<count($course); $i++) { print "<option value=\"$course[$i]\">$course[$i]\n"; } } } fclose($FH); print "<input type=\"submit\" value=\" Next 3 \">\n"; exit(); } ?> Link to comment https://forums.phpfreaks.com/topic/230660-need-help-with-if-empty/ Share on other sites More sharing options...
abhi_madhani Posted March 15, 2011 Share Posted March 15, 2011 Hi In If, ElseIf, Else logic, the ElseIf logic is only run, when its preceding IF/ElseIf has appeared to be false. and if its comes true it makes a exit, (That's the case I think is with your code.) Because the second ElseIF (Instructor) is coming true, it straight away execute the that logic, and makes a exit without reading ElseIf (Course). So I hope it clears the road bit. Regards Link to comment https://forums.phpfreaks.com/topic/230660-need-help-with-if-empty/#findComment-1187697 Share on other sites More sharing options...
34drysdale Posted March 15, 2011 Author Share Posted March 15, 2011 Yes when load the script, it just prints for Location (valid) and click on "Next 1", it then prints Location (with previous selected info) and Instructor for selection. Select instructor and click on "Next 2", it should prints Location & Instructor with previous selected info, right? But the script jumps back to the beginning and just prints the Location with no selected info even the last elseif changed to else. So, the problem is it doesn't pass Location and Instructor at the second click on "Next 2" button. Thanks, bobo Link to comment https://forums.phpfreaks.com/topic/230660-need-help-with-if-empty/#findComment-1187755 Share on other sites More sharing options...
abhi_madhani Posted March 16, 2011 Share Posted March 16, 2011 Hi, I actually tried running the script, but it didn't work because of missing files, if you could upload the sample somewhere, it would be easier to figure out your explanation. Regards, Abhishek Link to comment https://forums.phpfreaks.com/topic/230660-need-help-with-if-empty/#findComment-1188076 Share on other sites More sharing options...
34drysdale Posted March 17, 2011 Author Share Posted March 17, 2011 Guys, thanks. It's my coding problem. I missed the name of input at second elseif statement. Link to comment https://forums.phpfreaks.com/topic/230660-need-help-with-if-empty/#findComment-1188481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.