Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

select menu inside/outside a function

code1:
when the select statement is outside the function
post works

code2:
when the select statement is inside the function
post does not work

-------------
both code1 and code2 show a drop down select menu



I am using php+sqlserver 2005
<?php
//does not echo post
require_once('inc/common.php');
function displayProductFoldersC2($selected,$column) {
  echo'<select name="$column">';
    $sql = "SELECT distinct $column FROM rtrns where $column !='' ";
    foreach(dbfetcharray($sql) as $p) {
      if($p["$column"] == $selected) {
        echo '<option SELECTED value="' . $p["$column"] .'">' . $p["$column"] . '</option>';        
      }
      else {
        echo '<option value="' . $p["$column"] .'">' . $p["$column"] . '</option>';  
      }
    }
  echo "</selected>";
}  
$printable=$_POST[printable];
echo "<br>printable is $printable";
?>
<form action="#">
<?displayProductFoldersC2('Other','printable');?>
<input type=submit value=Submit>
</form>

Open in new window

<?php
//will echo post
require_once('inc/common.php');
function displayProductFoldersC2($selected,$column) {
  //echo'<select name="$column">';
    $sql = "SELECT distinct $column FROM rtrns where $column !='' ";
    foreach(dbfetcharray($sql) as $p) {
      if($p["$column"] == $selected) {
        echo '<option SELECTED value="' . $p["$column"] .'">' . $p["$column"] . '</option>';        
      }
      else {
        echo '<option value="' . $p["$column"] .'">' . $p["$column"] . '</option>';  
      }
    }
//  echo "</selected>";
}  
$printable=$_POST[printable];
echo "<br>printable is $printable";
?>
<form action="#">
<select name="printable">
<?displayProductFoldersC2('Other','printable');?>
</select>
<input type=submit value=Submit>
</form>

Open in new window

Avatar of cwiedmann
cwiedmann

You don't say what doesn't work, but the following line is problematic:

  echo'<select name="$column">';

Because you're using single quotes, it won't do variable substitution.

Try:


  echo "<select name=\"$column\">";
It's just a typo, you wrote </selected> instead of </select>. The output of the echo should be visible in the html source in both cases, it is just not rendered in the browser because the <select> is not closed.
Good point - I missed the typo.  You'll need to do the quotes as well to get the correct name for the select field, though.
Avatar of rgb192

ASKER

still does not echo the post
that I select using select menu
<?php
//does not echo post
require_once('inc/common.php');
function displayProductFoldersC2($selected,$column) {
  echo "<select name=\"$column\">";
    $sql = "SELECT distinct $column FROM rtrns where $column !='' ";
    foreach(dbfetcharray($sql) as $p) {
      if($p["$column"] == $selected) {
        echo '<option SELECTED value="' . $p["$column"] .'">' . $p["$column"] . '</option>';        
      }
      else {
        echo '<option value="' . $p["$column"] .'">' . $p["$column"] . '</option>';  
      }
    }
  echo "</select>";
}  
$printable=$_POST[printable];
echo "<br>printable is $printable";
?>
<form action="#">
<?displayProductFoldersC2('Other','printable');?>
<input type=submit value=Submit>
</form>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Peter_Werner
Peter_Werner

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 rgb192

ASKER

thanks