Solved

select menu inside/outside a function

Posted on 2010-09-01
6
354 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now