Link to home
Start Free TrialLog in
Avatar of NZ7C
NZ7C

asked on

Returning option values to form with PHP

Here is a portion of my form page and the action page. The page works just fine EXCEPT it does not echo back the list of all user last names. It will echo back the number of users just fine.  
<? echo "$lastname" ?> does not work and
<? echo "$_POST[lastname]" ?> does not work. What am I doing wrong?
I know I should have this simple stuff in my head by now - but it does not seem to be sinking in. Thanks in advance for any help. I've found thiis forum to be invaluable.

PAGE NAME = ADMNIN_USER.PHP:

require_once('Includes/conn.php');
require_once('functions.php');
session_start();
check_for_valid_user();.........ETC...

 <form action="admin_search_all.php" method="post">
      <input name="searchterm" class="selector_two"   type="text" size="14" maxlength="14"></p>
    <p id="search_button2"><input type="submit" class="button" value="List ALL"></p>
 </form>
 <form >
      <p>Number of users found:<span class="empty"><? echo $_GET['msg']; ?></span></p>
            <p>      <select name="uid" class="selector_search_box" >
                <option value="$uid"><? echo "$lastname" ?></option>                                  //THIS DOES NOT ECHO
              </select></p>
            <p>Do you want to EDIT or DELETE this user?</p>
              <input type="submit" class="button" value="EDIT user profile">
            <input type="submit" class="button" value="DELETE user">
 </form>

PAGE NAME = ADMIN_SEARCH_ALL.PHP:

require_once('Includes/conn.php');
require_once('functions.php');
session_start();
check_for_valid_user();

//build and issue query
  $sql = "SELECT * FROM t_user";
  $result = mysql_query($sql,$connection) or die(mysql_error());
//check the number of results
  $num = mysql_num_rows($result);
 
while ($row = mysql_fetch_array($result))
     {
            $uid = $row['uid'];
            $firstname = $row['firstname'];
            $lastname = $row['lastname'];
            $username = $row['username'];
      }
      $msg = "$num";
      header("Location:http://www.blah_blah_blah/admin_user.php?msg=" . urlencode($msg));
?>
Avatar of Zyloch
Zyloch
Flag of United States of America image

Hi NZ7C,

I believe it would be easier if your mysql queries were in admin_user.php instead of admin_search_all.php.

This is because otherwise you would have to serialize the array of returned results and store it in a session variable, and that's just unnecessary.

For example, admin_user.php can be like:

<?php
require_once('Includes/conn.php');
require_once('functions.php');
session_start();
check_for_valid_user(); //.........ETC...

//build and issue query
  $sql = "SELECT * FROM t_user";
  $result = mysql_query($sql,$connection) or die(mysql_error());
//check the number of results
  $msg = mysql_num_rows($result);

//Get all our user info
$uid = array();
$firstname = array();
$lastname = array();
$username = array();

while ($row = mysql_fetch_array($result))
{
   $uid[] = $row["uid"];
   $firstname[] = $row["firstname"];
   $lastname[] = $row["lastname"];
   $username[] = $row["username"];
}

?>
 <form action="admin_search_all.php" method="post">
      <input name="searchterm" class="selector_two"   type="text" size="14" maxlength="14"></p>
    <p id="search_button2"><input type="submit" class="button" value="List ALL"></p>
 </form>
 <form >
     <p>Number of users found:<span class="empty"><?php echo $msg; ?></span></p>
          <p>     <select name="uid" class="selector_search_box" >
<?php

for ($i = 0;$i < @count($uid);$i++)      
{
   echo("<option value=\"" . $uid[$i] . "\">" . $lastname[$i] . "</option>\n");
}

?>        
             </select></p>
          <p>Do you want to EDIT or DELETE this user?</p>
            <input type="submit" class="button" value="EDIT user profile">
           <input type="submit" class="button" value="DELETE user">
 </form>


Regards,
Zyloch
Avatar of NZ7C
NZ7C

ASKER

Zylock -
I have actually had a somewhat similar version of the code you presented functioning and I know if I used your suggestion it would work. And perhaps that is the proper way.....but first let me give my reasoning for the separate page approach and perhaps you can offer some clarity. First I am new to PHP and switching from ASP. In ASP and in the books I have looked at on PHP it is constantly preached to keep your code modularized - ie keep style sheets separate from shtml pages and script pages.  So I am trying to keep my structure in that manner, CSS files. SCRIPT files, and HTML files - using includes and calling functions as needed.

Your example keeps the sql query on the same page - as do a number of PHP books. Why does there seem to be a contradiction between the high emphasis on separating  or modularizing as mentioned above and your example (iwhich does agree with many PHP book)s? What am I missing?  In ASP these woiuld normally be on separate pages.
I hope my question is clear. Thank you for such a quick response!
Regards, NZ7C
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NZ7C

ASKER

A most excellent commentary and most helpful - I understand it and, as I think about it, it makes perfect sense. Thank you very, very much - I REALLY appreciate your time.
Regards, NZ7C
Avatar of NZ7C

ASKER

Umm...clarification.....did you mean to leave the action the same in the above script ie: <form action="admin_search_all.php" method="post"> even tho it is on the same page?
Hmm... The thing is, since you're able to select lastnames from the select box, I was thinking perhaps you will target admin_search_all.php to get particular information on a user. For example, admin_users could be like a forum member list and admin_search_all.php could be the profile of a single member.

Of course, if that is the case, admin_search_all isn't really a good name for the file -_- but I wasn't really sure.