Solved

select menu inside/outside a function

Posted on 2010-09-01
6
351 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 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…

708 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

21 Experts available now in Live!

Get 1:1 Help Now