Solved

select menu inside/outside a function

Posted on 2010-09-01
6
360 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:cwiedmann
ID: 33581728
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\">";
0
 
LVL 3

Expert Comment

by:Peter_Werner
ID: 33581774
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.
0
 
LVL 5

Expert Comment

by:cwiedmann
ID: 33581813
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rgb192
ID: 33581828
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

0
 
LVL 3

Accepted Solution

by:
Peter_Werner earned 500 total points
ID: 33582007
You need to declare post method or use $_GET[]
<form method="post" action="#">

One more hint: if your database can contain any string, use htmlspecialchars() to escape possible html tags in the output coming from the DB.
0
 

Author Closing Comment

by:rgb192
ID: 33590135
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question