Multiple Selection List

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Multiple Selection List

Post by AliasBDI »

I have a form with a multiple selectable list in it. When the page posts to another php page, it only shows that the last selection of the list is passing. Here is my code:

Code: Select all

<form name="form" method="post" action="dump.php" target"_post">
<p>Select table(s) to export:<br>
  <select name="tables" size="6" multiple="multiple">
    <option value="con_news">con_news</option>
    <option value="user_info">user_info</option>
    <option value="var_econtrolversions">var_econtrolversions</option>
    <option value="var_level">var_level</option>
  </select>
  <br>
  <br>
  dump format:</p>
<table width="200">
  <tr>
    <td><label>
      <input type="radio" name="format" value="SQL">
      SQL</label>
    </td>
  </tr>
  <tr>
    <td><label>
      <input type="radio" name="format" value="Excel CSV">
      Ms Excel</label>
    </td>
  </tr>
</table>
<br>
<p>
  <input type="submit" name="Submit" value="Submit"> 
  </p>
</form>
And the dump.php file:

Code: Select all

$tables = $_POST['tables'];
$format = $_POST['format'];
echo $tables;
echo $format;
exit();
Any ideas?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I tried as you said timvw. The code works, but now I cannot get it into a variable. Here is my code:

Code: Select all

<$php
if (isset($_REQUEST['tables'])) {
	while (list ($key, $val) = each ($_REQUEST['tables'])) {
		echo "$val, ";
	}
}

$format = $_REQUEST['format'];
echo $format;
exit();
?>
I need to echo it into a query later so I need it like $format.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$safe_list = array('format','tables');
foreach($safe_list as $v)
  $$v = isset($_POST[$v]) ? $_POST[$v] : (isset($_GET[$v]) ? $_GET[$v] : '');
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

feyd

Post by AliasBDI »

feyd, your code is much too complex for me. I tried to get it to work, but I cannot figure out how to use it. Could you explain or give the code with the original?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$safe_list = array('format','tables');
create a list of keys that are accepted into the script.

Code: Select all

foreach($safe_list as $v)
iterate through the keys in the safe list.

Code: Select all

$$v = isset($_POST&#1111;$v]) ? $_POST&#1111;$v] : (isset($_GET&#1111;$v]) ? $_GET&#1111;$v] : '');
create a variable named the accepted/expected key. If it was posted, use that. If it was in the get, use that. Otherwise, an empty string.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Maybe I'm using your code wrong... I have this now:

Code: Select all

<?php $safe_list = array('format','tables'); 
foreach($safe_list as $v)  $$v = isset($_POST[$v]) ? $_POST[$v] : (isset($_GET[$v]) ? $_GET[$v] : '');

echo $v;
//$format = $_REQUEST['format'];
//echo $format;
exit(); ?>
It is returning "tables" and that's it. My echo for $format works fine. I can use that, so I thought of pulling it out of code there.

The user selects "sel1", "sel2", "sel3" in the 'tables' field of the form and selects "selA" in the 'format' field of the same form. They hit GO. And it should return this:
sel1, sel2, sel3, selA
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

$v variable holds the name of variable to be imported. Use your names ($format, $tables) after foreach part
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I changed the code echo:

Code: Select all

<?php 
ehco $tables;
echo $format;
exit();
?>
echo $tables returns "Array". Not the value(s) in the variable. echo $format works fine though.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

AliasBDI wrote: echo $tables returns "Array". Not the value(s) in the variable. echo $format works fine though.
because echo won't print arrays. try this:

Code: Select all

if(is_array($table)) print_r($table); else echo $table;
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

OK. it is echoing the Array now. I'm sorry, I didn't know that echo would not work. After all of this work, this is not what I was looking for. I'm trying to the multi-select field into a variable so that I can use it in a query under the SELECT $tables .... So I need it to show like "sel1, sel2, sel3". Am I not getting it right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$tables = implode(', ', $tables);
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Perfect... Thanks for the help. Here is the code:

Code: Select all

<?php 
$safe_list = array('format','tables'); 
foreach($safe_list as $v)  $$v = isset($_POST[$v]) ? $_POST[$v] : (isset($_GET[$v]) ? $_GET[$v] : '');
$tables = implode(', ', $tables);
echo $tables;
echo $format;
exit();
?>
Thanks guys!
Post Reply