Jump to content

Need Help with if empty


34drysdale

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.