I am retrieving a list from my sql db into a select option box which works fine. The other part of the script should enable a selected name to be printed into a textarea, however this bit is giving a headache. Hope someone could be of help.

<HTML>
 <HEAD>
 <TITLE>List of Employees</TITLE>
 <h1><center><img src="images/slim.gif" width="543" height="72"></p></h1>
 </HEAD>
 <BODY>

<?php
session_start(); 

   //connect to database
   $conn = mysql_connect("localhost", "", "")
      or die(mysql_error());
   mysql_select_db("me",$conn) or die(mysql_error());

   if ($_POST[op] != "view") {
      //haven't seen the form, so show it
        
     //get parts of records
     $get_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
         from employee order by lname, fname";
     $get_list_res = mysql_query($get_list) or die(mysql_error());

     if (mysql_num_rows($get_list_res) < 1) {
         //no records
         echo "<p><em>Sorry, no records to select!</em></p>";
         
        
 } else {
         //has records, so get results and print in a form
         echo "
         <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
         <P><strong>List of Employees:</strong><br>
         <select name=\"sel_id\">
         <option value=\"\">— Select Colleagues —</option>";
         
	    
         

         while ($recs = mysql_fetch_array($get_list_res)) {
             $emp_no = $recs['emp_no'];
             $display_name = stripslashes($recs['display_name']);

             echo "<option value=\"$emp_no\">
                  $display_name</option>";
         }

         echo "
         </select>
         <input type=\"hidden\" name=\"op\" value=\"view\">
         <p><input type=\"submit\" name=\"submit\"
             value=\"Add to List\"></p>
         </FORM>";
      }
      
     
  } else if ($_POST[op] == "view") {

      $get_addresses = "select emp_no, fname, lname, dept, position
           from employee where emp_no = $_POST[sel_id]";
      $get_addresses_res = mysql_query($get_addresses);
      
         if (mysql_num_rows($get_addresses_res) > 0) {

          // don't have anything doing
         
          while ($add_info = mysql_fetch_array($get_addresses_res)) {
              $my_id = $add_info[emp_no];
              $my_fname = $add_info[fname];
              $my_lname = $add_info[lname];
              $my_dept = $add_info[dept];
              $my_position = $add_info[position];
			  $emp_list = "$my_id $my_fname $my_lname";
              // echo "$emp_list $my_fname $my_lname";
              
              $emp_array = array ($emp_list);
              $emp_id = array ($my_id);
                         
              // print the emp_array into the text area
              for($x=0; $x<=count($emp_array); $x++)
              {
              	echo "textspace";
              }
          }

         
      }

      }

                
  
?>
<textarea id="employees" name="textspace" cols="50" rows="5"></textarea>
 </BODY>
 </HTML>

Recommended Answers

All 10 Replies

I had a quick look at your script, the first thing I noticed was that these lines:

//get parts of records
     $get_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
         from employee order by lname, fname";
     $get_list_res = mysql_query($get_list) or die(mysql_error());

were within the IF statement so would not be processed unless the condition is true, move these lines to above the IF statement or re-declare them in the ELSE clause to get this to work.

The statment is checking whether a submission was made becuse I am much more interested in whether submission made was the view. I have an else if statement that loads the data in the select option which I am able to see the list. However, the display of the selection into the TEXTAREA is the problem even though it displays what I select it imposes over the select option and therefore I am not able to make another selection to increase the list.

At another look, your $_POST values are incorrect, it should be $_POST['value'] not $_POST[value] so this line:

$get_addresses = "select emp_no, fname, lname, dept, position
           from employee where emp_no = $_POST[sel_id]";

should be:

$get_addresses = "select emp_no, fname, lname, dept, position
           from employee where emp_no = " . $_POST['sel_id'] . "";

Xan,
Many thanks, that part is working fine now.

Still stuck with printing the option into the teaxarea. I wonder if I should put the textarea in a form or table? though it would have been ideal leaving it as it is.

<textarea id="employees" name="textspace" cols="50" rows="5"></textarea>

If you mean this one, then it would be something like:

<textarea id="employees" name="textspace" cols="50" rows="5"><?=$my_fname?></textarea>

Assuming you want the $my_fname value in the box.

I have tried the solution in the last post #6 but it doesn't work.

Should the textarea be inserted somewhere within the php script? or the position shouldn't matter?

I have made changes to the script, there has been an improvement - whereby if I click on the 'ADD To LIST' button, the TEXTAREA as well as the page stays put. However, the selected option is printed next to the TEXTAREA.

I am also interested in putting all the list that will be selected and printed into the TEXTAREA into an array that I will refer to further processing.

the new code is found below, i will need your help.

<?php
session_start(); 

   //connect to database
   $conn = mysql_connect("localhost", "", "")
      or die(mysql_error());
   mysql_select_db("me",$conn) or die(mysql_error());
   
   $get_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
         from employee order by lname, fname";
                  
     $get_list_res = mysql_query($get_list) or die(mysql_error());

     if (mysql_num_rows($get_list_res) < 1) {
         //no records
         echo "<p><em>Sorry, no records to select!</em></p>";
         
         } else {
         //has records, so get results and print in a form
         echo "
         <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
         <P><strong>List of Employees:</strong><br>
         <select name=\"sel_id\">
         <option value=\"\">— Select Colleagues —</option>";
         
	        

         while ($recs = mysql_fetch_array($get_list_res)) {
             $emp_no = $recs['emp_no'];
             $display_name = stripslashes($recs['display_name']);

             echo "<option value=\"$emp_no\">
                  $display_name</option>";
         }
         
          echo "
         </select>
         <input type=\"hidden\" name=\"op\" value=\"view\">
         <p><input type=\"submit\" name=\"submit\"
             value=\"Add to List\"></p>
         </FORM>";
        }
        
      // draw the textarea
      echo "<textarea id='employees' name=\"textspace\" cols=\"40\" rows=\"5\" wrap=\"virtual\">".$textspace."</textarea>";
	  
	  
	  if ($_POST[op] == "view") {

      $get_addresses = "select emp_no, fname, lname, dept, position
           from employee where emp_no = " . $_POST['sel_id'] . "";
      $get_addresses_res = mysql_query($get_addresses);
      
         if (mysql_num_rows($get_addresses_res) > 0) {

          // don't have anything doing
          /**
           * while the employees are being selected put their emp_no into an array
           * which will be used search their free slots
           */

          while ($add_info = mysql_fetch_array($get_addresses_res)) {
              $my_id = $add_info[emp_no];
              $my_fname = $add_info[fname];
              $my_dept = $add_info[dept];
              $my_position = $add_info[position];
              $my_lname = $add_info[lname];
			  $emp_list = "$my_id $my_fname $my_lname";
		     $emp_name = "$my_fname $my_lname";
		     $textspace = $emp_name;
		     echo "$textspace";
            //  echo "$emp_list $my_fname $my_lname";
            
            
              
              $emp_array = array($emp_list);
              $emp_id = array ($my_id);
              
              //print ("<textarea id=\"employees\" name=\"textspace\" cols=\"50\" rows=\"5\">.$emp_name</textarea>");
                         
            // print the emp_array into the text area
              for($x=0; $x<=count($emp_array); $x++)
              {
              	  $employees = $emp_name;
				  //echo "emps.employees.value='$employees'";
              }
          }

         
      }

      }
?>

Would somebody be kind enough to point out what to do to get the selected option to display in the TEXTAREA?

Would somebody be kind enough to point out what to do to get the selected option to display in the TEXTAREA?

in between the opening and closing textarea tags include this line of code

<? echo isset($_POST['sel_id'])?$_POST['sel_id']:""; ?>

The statements are already in php tags so I had to take out the <? ?> with the answer you gave. It gave a an error and I had tried debugging but now the error jumps to the next code instead. So I have inserted what I you had wanted done into the code. may be you can take a look and figure out the syntax.

echo "<textarea id=\"employees\" name=\"textspace\" cols=\"40\" rows=\"5\" wrap=\"virtual\"><? echo isset($_POST['sel_id'])?$_POST['sel_id']:""; ?></textarea>";
	echo "<input type=\"reset\" name=\"clear\" value=\"Reset\">";
	echo "     ";
	echo "<input type=\"submit\" name=\"myfix\" value=\"Next Step\">";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.