Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select menu inside/outside a function

Posted on 2010-09-01
6
Medium Priority
?
363 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
Technology Partners: 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

704 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