Jump to content

Dynamic selection menu with PHP/Java


ryanschefke

Recommended Posts

Hi Everyone - Can you help me understand why my dynamic selection list broke after adding [] to the name of the selection list, 1stLeft[]? It was working just fine with the small chunck of javascript to setup the left and right menus but when I try to create an array with the values using the php code below it breaks it. Without the [] next to 1stLeft in the name of the selection menu it works fine. I am trying to get it working then print out the values the user selecting (or moved to the box on the right hand side)

 

 

<script type="text/javascript">

  function OnTransferBtnClick(blnFromLeft)

  {

    var LeftListBox = document.forms[0].lstLeft;

    var RightListBox = document.forms[0].lstRight;

    var ListItems = new Array();

    FromList = (blnFromLeft ? LeftListBox : RightListBox);

    ToList = (blnFromLeft ? RightListBox : LeftListBox);

    for(var i=(FromList.options.length - 1);i>=0;i--)

    if(FromList.options.selected)

      {

        ListItems[listItems.length] = new Option(FromList.options.text);

        FromList.options = null;

      }

    for(var i=ListItems.length - 1;i>=0;i--)

      ToList.options[ToList.options.length] = ListItems;

    }

  </script>

 

 


$query_getstates = "SELECT state_code, state_name FROM states";
$result_getstates = @mysql_query($query_getstates); // run the query
echo "<select name=\"lstLeft[]\" size=\"10\" multiple>";
while ($row_getstates = mysql_fetch_array($result_getstates, MYSQL_NUM)) {
echo "<option value=\"" . $row_getstates[0] . "\">" . $row_getstates[1] . "</option>";
}

 

        <input Type="Button" Name="btnLtoR" Value=">>" onclick="OnTransferBtnClick(true)" title="Transfer Items Selected in the Left List Box to the Right List Box">     

        <input Type="Button" Name="btnRtoL" Value="<<" onclick="OnTransferBtnClick(false)" title="Transfer Items Selected in the Right List Box to the Left List Box">     

    <select name="lstRight[]" MULTIPLE size="10">

    </select>    </td>

 

Link to comment
Share on other sites

My full script is below. When the form is submitted I want to store all the selected values in MySQL. To test, I am trying to just print them to the screen. I can't figure out how to pass the selected items values in the submission form to print. You can see it in action here: http://www.leadliaison.com/filterState.php

 

<?PHP
require_once ("mysql_connect.php"); // connect to the db
include "configuration.inc";
//require_once ("passport.php"); // connect to the db


if (isset($submit)) {

for ($n=0; $n <= count($_POST['1stLeft']); $n++) {
	//echo "hi".$i;
     	echo $_POST['1stLeft'][$n];
	echo "<br>";
		}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lead Liaison - Sales Leads</title>
<link href="CSS/ll_main.css" rel="stylesheet" type="text/css" />
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
  <script type="text/javascript">
   function OnTransferBtnClick(blnFromLeft)
   {
    var LeftListBox = document.forms[0].lstLeft;
    var RightListBox = document.forms[0].lstRight;
    var ListItems = new Array();
    FromList = (blnFromLeft ? LeftListBox : RightListBox); 
    ToList = (blnFromLeft ? RightListBox : LeftListBox);
    for(var i=(FromList.options.length - 1);i>=0;i--)
     if(FromList.options[i].selected)
       {
        ListItems[ListItems.length] = new Option(FromList.options[i].text);
        FromList.options[i] = null;
       } 
     for(var i=ListItems.length - 1;i>=0;i--)
      ToList.options[ToList.options.length] = ListItems[i];
    } 

  </script>
  
</head>

<body>
<div id="container">
<?PHP include "header_internal.php";?>
<?php 
if (isset($message)) {
    echo '<table width="90%" border="0" align="center"><tr>
      <td class="rederrormessage">
  <div align="center"><img src="images/icon_critalert.gif" width="34" height="34" hspace="10" vspace="5" align="absmiddle" />'.$message.'
      </div></td>
    </tr></table>';
}
if (isset($success)) {
    echo '<table width="90%" border="0" align="center"><tr>
      <td class="successmessage">
  <div align="center"><img src="images/icon_confirmation.gif" width="34" height="34" hspace="10" vspace="5" align="absmiddle" />'.$success.'
      </div></td>
    </tr></table>';
}
?>
<table border="0" align="center" >
  <tr>
    <td height="295">
<div align="center">
    <div class="containerGrey">
    <!-- Container Head -->
    <div class="container_headGrey">
        <img src="images/sidebar_leftSquare.jpg" alt="" class="float_left"/>
        <img src="images/sidebar_rightSquare.jpg" alt="" class="float_right"/>
        <div class="container_head_textGrey">
            <div align="left">
            Choose States and Provinces</div>
            </div>
            </div>
            <!-- Container Content -->
            <div class="container_contentGrey">
            <div align="center">
            
<form action="<?PHP echo $PHP_SELF; ?>" method="post" name="statesform" id="statesform">  

<table width="70%">
   <tr>
    <td height="181">
    
<!--<select name="lstLeft" multiple size="10">
-->
<?PHP
$query_getstates = "SELECT state_code, state_name FROM states";
$result_getstates = @mysql_query($query_getstates); // run the query
echo "<select name=\"lstLeft\" size=\"10\" multiple>";
while ($row_getstates = mysql_fetch_array($result_getstates, MYSQL_NUM)) {
echo "<option value=\"" . $row_getstates[0] . "\">" . $row_getstates[1] . "</option>";
}
?>
        </select></td>
    <td>
     &nbsp    </td>
    <td>
     <table>
      <tr>
       <td>
        <input Type="Button" Name="btnLtoR" Value=">>" onclick="OnTransferBtnClick(true)" title="Transfer Items Selected in the Left List Box to the Right List Box">       </td> 
      </tr>
      <tr>
       <td>
        <input Type="Button" Name="btnRtoL" Value="<<" onclick="OnTransferBtnClick(false)" title="Transfer Items Selected in the Right List Box to the Left List Box">       </td>
      </tr>
     </table>    </td>
    <td>
     &nbsp    </td>
    <td>
     <select name="lstRight" MULTIPLE size="10">
     </select>    </td>
   </tr>
   
  </table>
  <table width="100%" border="0">
    <tr>
      <td><div align="center">
	<input name="submit" type="submit" id="submit" title="Submit this form" value="OK" />
</div></td>
    </tr>
    <tr>
      <td> </td>
    </tr>
  </table>
  </form>

  
             </div>
        </div>
    </div>
</div>
</td>
  </tr>
</table>
<p>
  <?PHP mysql_close(); ?>
</p>
<p>  </p>
<?PHP include "footer.php"; ?>
</div>
</body>
</html>



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.