Solved

select menu inside/outside a function

Posted on 2010-09-01
6
359 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

856 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