PHP using a multiple select list.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
relambert
Forum Newbie
Posts: 2
Joined: Tue Jan 18, 2011 5:00 pm

PHP using a multiple select list.

Post by relambert »

I'm very new at PHP and I'm having an issue with the output from my submitted forms. The user and email fields work as they are supposed to. However, the target field is giving me some grief.

What I need is for the all of the selections in the target field to show up when echoed. However, only the last selection shows up. The 'target' is populated dynamically using databases so it would be virtually impossible to code each and every option into an array...

Is there a shortcut available?

Code: Select all

<?php

echo "<p>Welcome <b>".$_POST["user"]. "</b>!</p>";
echo "<p>Your email address is:<br/><b>".$_POST["email"]. "</b></p>";
echo "<p>You have ordered:<br/><b>".$_POST["target"]. "</b></p>";
?>
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: PHP using a multiple select list.

Post by anantha »

the code which u have shown is not enough to solve ur problem..
relambert
Forum Newbie
Posts: 2
Joined: Tue Jan 18, 2011 5:00 pm

Re: PHP using a multiple select list.

Post by relambert »

I'm new to this -- what else do you need? The html page with the form?

Source code is long... and the functions work exactly like they're supposed to. EXCEPT when I view the results of the page, it says I've ordered "array". Is there a way to print the array itself?

Code: Select all

<?php require_once('../Connections/GCDistProducts.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Implode multiple selection
if (isset($_POST["target"]) && is_array($_POST["target"])) {
  $_POST["target"] = implode(" , ", $_POST["target"]);
  $HTTP_POST_VARS["target"] = $_POST["target"];
}
// FELIXONE ? 2002/2004   SB by Felice Di Stefano - www.felixone.it

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_GCDistProducts, $GCDistProducts);
$query_Child = "SELECT * FROM `aritemlist.rpt`";
$Child = mysql_query($query_Child, $GCDistProducts) or die(mysql_error());
$row_Child = mysql_fetch_assoc($Child);
$totalRows_Child = mysql_num_rows($Child);

mysql_select_db($database_GCDistProducts, $GCDistProducts);
$query_Parent = "SELECT * FROM categorylistid";
$Parent = mysql_query($query_Parent, $GCDistProducts) or die(mysql_error());
$row_Parent = mysql_fetch_assoc($Parent);
$totalRows_Parent = mysql_num_rows($Parent);

mysql_select_db($database_GCDistProducts, $GCDistProducts);
$query_Search = "SELECT * FROM `aritemlist.rpt`";
$Search = mysql_query($query_Search, $GCDistProducts) or die(mysql_error());
$row_Search = mysql_fetch_assoc($Search);
$totalRows_Search = mysql_num_rows($Search);
?>
<?php
if ($row_Child)     {
  echo "<SC" . "RIPT>\n";
  echo "var WAJA = new Array();\n";

  $oldmainid = 0;
  $newmainid = $row_Child["C"];
  if ($oldmainid == $newmainid)    {
    $oldmainid = "";
  }
  $n = 0;
  while ($row_Child)     {
    if ($oldmainid != $newmainid)     {
      echo "WAJA[".$n."] = new Array();\n";
      echo "WAJA[".$n."][0] = '".WA_DD_Replace($newmainid)."';\n";
      $m = 1;
    }

    echo "WAJA[".$n."][".$m."] = new Array();\n";
    echo "WAJA[".$n."][".$m."][0] = "."'".WA_DD_Replace($row_Child["A"])."'".";\n";
    echo "WAJA[".$n."][".$m."][1] = "."'".WA_DD_Replace($row_Child["B"])."'".";\n";

    $m++;
    if ($oldmainid == 0)      {
      $oldmainid = $newmainid;
    }
    $oldmainid = $newmainid;
    $row_Child = mysql_fetch_assoc($Child);
    if ($row_Child)     {
      $newmainid = $row_Child["C"];
    }
    if ($oldmainid != $newmainid)     {
      $n++;
    }
  }

  echo "var Child_WAJA = WAJA;\n";
  echo "WAJA = null;\n";
  echo "</SC" . "RIPT>\n";
}
function WA_DD_Replace($startStr)  {
  $startStr = str_replace("'", "|WA|", $startStr);
  $startStr = str_replace("\\", "\\\\", $startStr);
  $startStr = preg_replace("/[\r\n]{1,}/", " ", $startStr);
  return $startStr;
}
?>
<script type="JavaScript">
oldvalue = "";
function passText(passedvalue) {
  if (passedvalue != "") {
    var totalvalue = passedvalue+"\n"+oldvalue;
    document.displayform.itemsbox.value = totalvalue;
    oldvalue = document.displayform.itemsbox.value;
  }
}
//  End -->
</script>
<script type="text/javascript">
<!--
function WA_ClientSideReplace(theval,findvar,repvar)     {
  var retval = "";
  while (theval.indexOf(findvar) >= 0)    {
    retval += theval.substring(0,theval.indexOf(findvar));
    retval += repvar;
    theval = theval.substring(theval.indexOf(findvar) + String(findvar).length);
  }
  retval += theval;
  if (retval == "" && theval.indexOf(findvar) < 0)    {
    retval = theval;
  }
  return retval;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function WA_UnloadList(thelist,leavevals,bottomnum)    {
  while (thelist.options.length > leavevals+bottomnum)     {
    if (thelist.options[leavevals])     {
      thelist.options[leavevals] = null;
    }
  }
  return leavevals;
}

function WA_FilterAndPopulateSubList(thearray,sourceselect,targetselect,leaveval,bottomleave,usesource,delimiter)     {
  if (bottomleave > 0)     {
    leaveArray = new Array(bottomleave);
    if (targetselect.options.length >= bottomleave)     {
      for (var m=0; m<bottomleave; m++)     {
        leavetext = targetselect.options[(targetselect.options.length - bottomleave + m)].text;
        leavevalue  = targetselect.options[(targetselect.options.length - bottomleave + m)].value;
        leaveArray[m] = new Array(leavevalue,leavetext);
      }
    }
    else     {
      for (var m=0; m<bottomleave; m++)     {
        leavetext = "";
        leavevalue  = "";
        leaveArray[m] = new Array(leavevalue,leavetext);
      }
    }
  }  
  startid = WA_UnloadList(targetselect,leaveval,0);
  mainids = new Array();
  if (usesource)    maintext = new Array();
  for (var j=0; j<sourceselect.options.length; j++)     {
    if (sourceselect.options[j].selected)     {
      mainids[mainids.length] = sourceselect.options[j].value;
      if (usesource)     maintext[maintext.length] = sourceselect.options[j].text + delimiter;
    }
  }
  for (var i=0; i<thearray.length; i++)     {
    goodid = false;
    for (var h=0; h<mainids.length; h++)     {
      if (thearray[i][0] == mainids[h])     {
        goodid = true;
        break;
      }
    }
    if (goodid)     {
      theBox = targetselect;
      theLength = parseInt(theBox.options.length);
      theServices = thearray[i].length + startid;
      var l=1;
      for (var k=startid; k<theServices; k++)     {
        if (l == thearray[i].length)     break;
        theBox.options[k] = new Option();
        theBox.options[k].value = thearray[i][l][0];
        if (usesource)     theBox.options[k].text = maintext[h] + WA_ClientSideReplace(thearray[i][l][1],"|WA|","'");
        else               theBox.options[k].text = WA_ClientSideReplace(thearray[i][l][1],"|WA|","'");
        l++;
      }
      startid = k;
    }
  }
  if (bottomleave > 0)     {
    for (var n=0; n<leaveArray.length; n++)     {
      targetselect.options[startid+n] = new Option();
      targetselect.options[startid+n].value = leaveArray[n][0];
      targetselect.options[startid+n].text  = leaveArray[n][1];
    }
  }
  for (var l=0; l < targetselect.options.length; l++)    {
    targetselect.options[l].selected = false;
  }
  if (targetselect.options.length > 0)     {
    targetselect.options[0].selected = true;
  }
}
function WA_AddValueToList(ListObj,TextString,ValString,Position)  {
  if (isNaN(parseInt(Position)))   {
    Position = ListObj.options.length;
  }
  else  {
    Position = parseInt(Position);
  }
  if (ListObj.length > Position)  {
  ListObj.options[Position].text=TextString;
  if (ValString != "")  {
    ListObj.options[Position].value = ValString;
  }
    else  {
      ListObj.options[Position].value=TextString;
    }
  }
  else  {
    var LastOption = new Option();
    var OptionPosition = ListObj.options.length;
    ListObj.options[OptionPosition] = LastOption;
    ListObj.options[OptionPosition].text = TextString;
    if (ValString != "")  {
      ListObj.options[OptionPosition].value = ValString;
    }
    else  {
      ListObj.options[OptionPosition].value=TextString;
    }
  }
}

function WA_subAwithBinC(a,b,c)
{

	var i = c.indexOf(a);
	var l = b.length;

	while (i != -1)	{
		c = c.substring(0,i) + b + c.substring(i + a.length,c.length);  //replace all valid a values with b values in the selected string c.
  i += l
		i = c.indexOf(a,i);
	}
	return c;

}

function WA_RemoveSelectedFromList(theBox,nottoremove,noneselectedoption,noneselectedvalue,noneselectedtext)     {
  var n=0;
  var selectedArray = new Array();
  for (var j=0; j<theBox.options.length; j++)     {
    if (!theBox.options[j].selected || nottoremove.indexOf("|WA|" + theBox.options[j].value + "|WA|") >= 0)     {
      theBox.options[n].value = theBox.options[j].value;
      theBox.options[n].text = theBox.options[j].text;
      n++;
    }
    else {
	    selectedArray[selectedArray.length] = j;
    }
  }
  for (var k=0; k<selectedArray.length; k++)  {
    theBox.options[selectedArray[k]].selected = false;
  }
  m = n;
  while (m<=j)     {
    theBox.options[n] = null;
    m++;
  }
  if (theBox.options.length == noneselectedoption && noneselectedtext != "")     {
    noneselectedvalue = WA_subAwithBinC("|WA|",",",noneselectedvalue);
    noneselectedtext = WA_subAwithBinC("|WA|",",",noneselectedtext);
    WA_AddValueToList(theBox,noneselectedtext,noneselectedvalue,0);
  }
  for (var l=0; l < theBox.options.length; l++)    {
    theBox.options[l].selected = false;
  }
}

function WA_AddSubToSelected(sublist,targetlist,repeatvalues,leavetop,leavebottom,noseltop,noselbot,topval,toptext)     {
  for (var j=0; j<noseltop; j++)     {
    sublist.options[j].selected = false;
  }
  for (var k=0; k<noselbot; k++)     {
    sublist.options[sublist.options.length-(k+1)].selected = false;
  }
  if (sublist.selectedIndex >= 0)      {
   if (leavebottom)     {
      botrec = new Array(2);
      if (targetlist.options.length > 0)  {
        botrec[0] = targetlist.options[targetlist.options.length-1].value;
        botrec[1] = targetlist.options[targetlist.options.length-1].text;
        targetlist.options[targetlist.options.length-1] = null;
      }
    }
    if (!leavetop && targetlist.options.length > 0)     {
      if (targetlist.options[0].value == topval)     {
        targetlist.options[0] = null;  
      }
    }
    else     {
      if (leavetop && toptext != '')     {
	    targetlist.options[0] = new Option();
        targetlist.options[0].value = topval;
        targetlist.options[0].text = toptext;
      }
    }
    for (var o=0; o<sublist.options.length; o++)     {
      if (sublist.options[o].selected && o >= noseltop && o < sublist.options.length - noselbot)     {
        theText = sublist.options[o].text;
        theValue = sublist.options[o].value;
        addvalue = true;
        if (!repeatvalues)      {
          for (var p=0; p<targetlist.options.length; p++)     {
            if (theValue == targetlist.options[p].value)      {
              addvalue = false;
            }
          }
        }
        if (addvalue)  WA_AddValueToList(targetlist,theText,theValue,targetlist.options.length);
      }
    }
    if (leavebottom && botrec[1])     {
      WA_AddValueToList(targetlist,botrec[1],botrec[0],targetlist.options.length);
    }
  }
  for (var l=0; l < targetlist.options.length; l++)    {
    targetlist.options[l].selected = false;
  }
}
//-->
</script>



</head>

<body class="oneColElsCtrHdr" onLoad="WA_AddSubToSelected(MM_findObj('Child'),MM_findObj('target'),false,false,false,0,0,'0','')">

<div id="container">
  <div id="mainContent">
    <h1>Online Ordering. Wamp wamp.</h1>
    <h1>&nbsp;</h1>
    <form action="send_simpleform.php" method="POST" form name="SelectItem" id="SelectItem">
      <p>
        Name:
      
    <input type="text" name="user" />
        Email:
        <input type="text" name="email" rows="5" cols="40">
      </p>
      <table width="680" height="136" border="1">
        <tr>
          <td width="178">Sales Department</td>
          <td width="399"><select name="Parent" size="5" id="Parent" onChange="WA_FilterAndPopulateSubList(Child_WAJA,MM_findObj('Parent'),MM_findObj('Child'),0,0,false,': ');WA_FilterAndPopulateSubList(Child_WAJA,MM_findObj('Parent'),MM_findObj('Child'),0,0,false,': ')">
            <?php
do {  
?>
            <option value="<?php echo $row_Parent['A']?>"><?php echo $row_Parent['B']?></option>
            <?php
} while ($row_Parent = mysql_fetch_assoc($Parent));
  $rows = mysql_num_rows($Parent);
  if($rows > 0) {
      mysql_data_seek($Parent, 0);
	  $row_Parent = mysql_fetch_assoc($Parent);
  }
?>
          </select></td>
          <td width="81">&nbsp;</td>
        </tr>
        <tr>
          <td>Product</td>
          <td><select name="Child" size="5" multiple="multiple" id="Child">
          </select></td>
          <td><label>
            <input name="add" type="button" id="add" onClick="WA_AddSubToSelected(MM_findObj('Child'),MM_findObj('target'),false,false,false,0,0,'0','')" value="Add to List" />
          </label></td>
        </tr>
        <tr>
          <td>Order List</td>
          <td><select name="target[]" size="30" multiple="yes" id="target" type="text">
          </select></td>
          <td><p>
            <input name="Remove" type="button" id="Remove" onClick="WA_RemoveSelectedFromList(MM_findObj('target'),'|WA|0|WA|',-1,'0','None Selected')" value="Remove" />
          </p>
          </td>
        </tr>
      </table>
      <p>
        <label></label>
        <label></label>
        <label></label>
        <label></label>
        <label></label>
        <label>
        <input type="submit" name="submit" id="submit" value="Place Order">
        </label>
      </p>
    </form>
    <form name="form1" method="post" action="">
      <input type="hidden" name="hname" id="sailboat">
      <input type="hidden" name="hemail" id="hemail">
      <input type="hidden" name="horder" id="horder">
    </form>
    <p>&nbsp;</p>
    </form>
  <!-- end #mainContent --></div>
  <!-- end #container -->
</div>
</body>
</html>
<?php
mysql_free_result($Parent);

mysql_free_result($Search);

mysql_free_result($Child);
?>
Post Reply