I would like to ask some question of sending variable to the same page:

Eg. I have created a drop down menu which list the class name of my school. User can then select the class name from drop down menu, right after selection, the page would send variable to itself (same page), and then the same page would additionally display class students of the selected class.

My questions are:
(1) I add this script on top of the page:
$class_no = $HTTP_GET_VARS['class_no'];
to receive class_no right after user select from drop down menu. I would like to know how about if the user come to this page for the first time. PHP would prompt me it is underfined because user has never make any selection

(2) Right after the selection from drop down menu, how can I send the class_no to the same page and then for listing out the student_name?

Thanks

    Post what code you have so far. We can work from there. Sounds like you just need to add an if(isset($_GET['submitButton'])) sort of statement.

      I have written something like this till now. I tried to added "@" symbol in order to avoid the error msg. But I do guess these is another better method.

      For question (2), I tried to use javascript to send the variable to the same page. But what make me feel uncomfortable is I need to send $mail_outbox_no thru javascript.

      I would like to seek for some advice to improve those 2 questions

      <?

      session_start();
      include ('db.php');

      $teacher_no = $HTTP_SESSION_VARS['teacher_no'];
      $admin_flag = $HTTP_SESSION_VARS['admin_flag'];
      $mail_outbox_no = $HTTP_GET_VARS['mail_outbox_no'];

      @$class_no = $HTTP_GET_VARS['class_no'];
      ?>

      <html>
      <head>
      <title>Mail Administration</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <script language = "JavaScript">
      function show_student() {
      var class_no = document.class_form.class_no.value;
      var mail_outbox_no = document.class_form.mail_outbox_no.value;
      location.href = "select_recipient.php?class_no="+class_no+"&mail_outbox_no="+mail_outbox_no;
      }
      </script>

      </head>

      <body bgcolor="#FFFFFF" text="#000000" bgproperties="fixed" background="img/wallpaper_blue.jpg">
      <b>Select Recipient(s)</b><br><br>

      <?
      if ($admin_flag == "T") {
      $sql = "SELECT no, class_name from class WHERE school_no='$school_no' ORDER BY class_name";
      } else {
      $sql = "SELECT no, class_name from class WHERE teacher_no='$from_teacher' ORDER BY class_name";
      }
      $rs_query = mysql_query(($sql),$conn);
      $no_of_class = mysql_num_rows($rs_query);
      print "<b>Please select a class:</b><br>";
      print "<form name=class_form>";
      print "<select name='class_no' onChange='show_student()'>";
      print "<option>Please select a class</option>";
      for ($index=0;$index<$no_of_class;$index++) {
      $arr[$index] = mysql_fetch_array($rs_query);
      print "<option value='".$arr[$index]['no']."'>".$arr[$index]['class_name']."</opotion>";
      }
      print "</select>";
      print "<input type='hidden' name=mail_outbox_no value=$mail_outbox_no>";
      print "</form>";
      print "<br>";
      ?>

        Ok, to answer your questions

        1)

        if($_GET['class_no']) {
            // if the GET variable is there, set this new variable
            // otherwise, nothing will happen, 
            // and you won't get an error for it
            $class_no = $_GET['class_no']; 
        }

        2) Setting "<form name=\"class_form\" action=\"".$SERVER['PHP_SELF'].$SERVER['argv']."\">

        This says to use the current page to submit to itself, and jeep all arguments(GET variables) that are present. Then set

        <select name=\"class_no\" onChange=\"document.class_form.submit();\">

          Ok, make the following changes. It will work.
          1) Old: print "<option>Please select a class</option>";
          New: print "<option value=\"\">Please select a class</option>";

          2) After the dropdown list coding(<SELECT>), write the following code,

          if(isset($class_no) && $class_no != ""){
          //HERE THE CODE FOR DISPLAYING CLASS STUDENTS WOULD BE USED

          /*
          please be sure that $class_no is available only when the form is
          submitted and the variables passed to itself
          
          So on the first attemp the class students will not be displayed
          becaue $class_no is not set at that time.
          */

          }

          3) Also on submitting the form, if you want the class_no selected as default in droplist, you need to write this in for loop instead of what you have written:

          for ($index=0;$index<$no_of_class;$index++) {
          $arr[$index] = mysql_fetch_array($rs_query);
          print "<option value='".$arr[$index]['no']."'";
          if($class_no == $arr[$index]['no']) print " SELECTED";
          print ">".$arr[$index]['class_name']."</opotion>";

          }

          I hope this works. Best of Luck

            Thanks rcmehta_14
            Problem is solved. You also remind to to set default for the selection. Thanks

              Hi rolwong!

              The desision is a simple one and is used in many situations.

              In PHP there is a function isset($var) which checks whether is defined. So you can use this:

              if (isset($class)){
              Echo "Students from class $class:";
              }

              Or you can add a switch() controll if you need it.

              For more information on the use of the isset() function you might be interested to see the replies (whene posted) to the thread I posted just a minute ago "Logical operator" (or something like that).

              Contact me if you need more help.

              Djumaka.

                Write a Reply...