Page 1 of 1

Multiple Selection List

Posted: Mon Aug 30, 2004 3:32 pm
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?

Posted: Mon Aug 30, 2004 4:42 pm
by timvw

Posted: Wed Sep 01, 2004 1:03 pm
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.

Posted: Wed Sep 01, 2004 1:14 pm
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] : '');

feyd

Posted: Wed Sep 08, 2004 1:31 pm
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?

Posted: Wed Sep 08, 2004 1:36 pm
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.

Posted: Fri Sep 10, 2004 8:50 am
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

Posted: Fri Sep 10, 2004 9:39 am
by Weirdan
$v variable holds the name of variable to be imported. Use your names ($format, $tables) after foreach part

Posted: Fri Sep 10, 2004 9:50 am
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.

Posted: Fri Sep 10, 2004 10:24 am
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;

Posted: Fri Sep 10, 2004 10:40 am
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?

Posted: Fri Sep 10, 2004 10:42 am
by feyd

Code: Select all

$tables = implode(', ', $tables);

Posted: Fri Sep 10, 2004 10:56 am
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!