Multiple choice text menus

  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

I'm stumped. I've tried everything on web that I can find. I have a multiple choice list menu that when I click submit, it only submits the last choice. I can choose all 3 options and it only chooses the last. PLEASE HELP!!
My code is below. Thanks so much. I'm so frustrated, and I'm sure it's an easy fix. I've also just tried pulling the options from a mysql database, and can pull the options fine, it's that I can't have multiple deploy to my database.
Code: [ Select ]

<head>
</head>

<body>
<form id="form1" name="form1" method="POST" action="/data/oe/hsa/form.php?">
 
<label>Providers
<select name="providers[]" size="4" multiple="TRUE" id="providers">
 <option value="Select">Select up to two options</option>
  <option value="1">BlueCross BlueShield</option>
  <option value="2">HealthPartners</option>

  <option value="3">PreferredOne</option>
 </select>
</label>
<p>
 <label>Submit
  <input type="submit" name="submit" id="submit" value="Submit" />
  </label>
  <br />
 
<input type="hidden" name="MM_insert" value="form1" />
</form>

</body>
</html>
  1. <head>
  2. </head>
  3. <body>
  4. <form id="form1" name="form1" method="POST" action="/data/oe/hsa/form.php?">
  5.  
  6. <label>Providers
  7. <select name="providers[]" size="4" multiple="TRUE" id="providers">
  8.  <option value="Select">Select up to two options</option>
  9.   <option value="1">BlueCross BlueShield</option>
  10.   <option value="2">HealthPartners</option>
  11.   <option value="3">PreferredOne</option>
  12.  </select>
  13. </label>
  14. <p>
  15.  <label>Submit
  16.   <input type="submit" name="submit" id="submit" value="Submit" />
  17.   </label>
  18.   <br />
  19.  
  20. <input type="hidden" name="MM_insert" value="form1" />
  21. </form>
  22. </body>
  23. </html>
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

You're going to receive an array on the server side. How are you processing the params (what leads you to believe you only get the last result)? Could we see the snippet of your processing code?
  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

Actually I am getting an array, my mistake. I'm getting the word 'array' in my mysql database!! What do you mean my processing parameters? I'm fairly new to this. Obviously I'm missing something!! Some other code that I wrote was processing last choice, this is processing the word 'array'.
This is code that should do same thing, but is the code that throws only last option. It's pulling choices out of a database, obviously.

<select name="providers[]" size="4" multiple="TRUE" id="providers">
<option value="Select">Select up to two options</option>
<?php
do {
?>
<option value="<?php echo $row_providers['autoId']?>"><?php echo $row_providers['provider']?></option>
<?php
} while ($row_providers = mysql_fetch_assoc($providers));
$rows = mysql_num_rows($providers);
if($rows > 0) {
mysql_data_seek($providers, 0);
$row_providers = mysql_fetch_assoc($providers);
}
?>
</select>
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

That means you're receiving all your options on the other side. I'm assuming PHP, but if you're using another language, I can probably help with that, too. Try something like this in your code:

Code: [ Select ]
$providerArr = $_POST['providers'];
foreach ($providerArr as $provider) {
   echo $provider."<br>\n";
}
  1. $providerArr = $_POST['providers'];
  2. foreach ($providerArr as $provider) {
  3.    echo $provider."<br>\n";
  4. }
  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

So, this is my complete code, minus the other input fields. What looks wrong? I've added you php code, and yes, I am using php, and have been trying to come up with this code, I knew I had to have something else to call the array to the database. Thanks for that help, but still frustrated. I'm sorry!! I'm working with Dreaweaver 9, and it won't let me map my provider[] array to the database. It wants me to remove the [] array? Also, not sure I have the script you provided calling in correct area. Most of this code isn't needed by you, but I put it all in, in case I'm in error somewhere else.
Code: [ Select ]
<?php require_once('../../../Connections/oe.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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
 $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
 $insertSQL = sprintf("INSERT INTO hsa (firstName, lastName, agency, hrContact, providers, schedule) VALUES (%s, %s, %s, %s, %s, %s)",
            GetSQLValueString($_POST['firstName'], "text"),
            GetSQLValueString($_POST['lastName'], "text"),
            GetSQLValueString($_POST['agency'], "text"),
            GetSQLValueString($_POST['hrContact'], "text"),
            GetSQLValueString($_POST['providers[]'], "text"),
            GetSQLValueString($_POST['schedule'], "text"));

 mysql_select_db($database_oe, $oe);
 $Result1 = mysql_query($insertSQL, $oe) or die(mysql_error());
}

mysql_select_db($database_oe, $oe);
$query_hsa = "SELECT * FROM hsa";
$hsa = mysql_query($query_hsa, $oe) or die(mysql_error());
$row_hsa = mysql_fetch_assoc($hsa);
$totalRows_hsa = mysql_num_rows($hsa);

mysql_select_db($database_oe, $oe);
$query_providers = "SELECT * FROM providers ORDER BY provider ASC";
$providers = mysql_query($query_providers, $oe) or die(mysql_error());
$row_providers = mysql_fetch_assoc($providers);
$totalRows_providers = mysql_num_rows($providers);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "external link it won't let me post with">
<html xmlns="external link it won't let me post with">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
 <label></label>
 <br />
<br />
<label>Providers
<select name="providers[]" size="4" multiple="multiple" id="providers">
<option value="Select">Select up to two options</option>
 <option value="1">BlueCross BlueShield</option>
 <option value="2">HealthPartners</option>
 <option value="3">PreferredOne</option>
 <?php $providerArr = $_POST['providers'];
foreach ($providerArr as $provider) {
  echo $provider."<br>\n";
}?>
</select>
</label>


<p>
 <label>Submit
  <input type="submit" name="submit" id="submit" value="Submit" />
  </label>
  <br />
 
<input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>

<?php
mysql_free_result($hsa);

mysql_free_result($providers);
?>
  1. <?php require_once('../../../Connections/oe.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  7.  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  8.  switch ($theType) {
  9.   case "text":
  10.    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  11.    break;  
  12.   case "long":
  13.   case "int":
  14.    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  15.    break;
  16.   case "double":
  17.    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  18.    break;
  19.   case "date":
  20.    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  21.    break;
  22.   case "defined":
  23.    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  24.    break;
  25.  }
  26.  return $theValue;
  27. }
  28. }
  29. $editFormAction = $_SERVER['PHP_SELF'];
  30. if (isset($_SERVER['QUERY_STRING'])) {
  31.  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  32. }
  33. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  34.  $insertSQL = sprintf("INSERT INTO hsa (firstName, lastName, agency, hrContact, providers, schedule) VALUES (%s, %s, %s, %s, %s, %s)",
  35.             GetSQLValueString($_POST['firstName'], "text"),
  36.             GetSQLValueString($_POST['lastName'], "text"),
  37.             GetSQLValueString($_POST['agency'], "text"),
  38.             GetSQLValueString($_POST['hrContact'], "text"),
  39.             GetSQLValueString($_POST['providers[]'], "text"),
  40.             GetSQLValueString($_POST['schedule'], "text"));
  41.  mysql_select_db($database_oe, $oe);
  42.  $Result1 = mysql_query($insertSQL, $oe) or die(mysql_error());
  43. }
  44. mysql_select_db($database_oe, $oe);
  45. $query_hsa = "SELECT * FROM hsa";
  46. $hsa = mysql_query($query_hsa, $oe) or die(mysql_error());
  47. $row_hsa = mysql_fetch_assoc($hsa);
  48. $totalRows_hsa = mysql_num_rows($hsa);
  49. mysql_select_db($database_oe, $oe);
  50. $query_providers = "SELECT * FROM providers ORDER BY provider ASC";
  51. $providers = mysql_query($query_providers, $oe) or die(mysql_error());
  52. $row_providers = mysql_fetch_assoc($providers);
  53. $totalRows_providers = mysql_num_rows($providers);
  54. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "external link it won't let me post with">
  55. <html xmlns="external link it won't let me post with">
  56. <head>
  57. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  58. <title>Untitled Document</title>
  59. </head>
  60. <body>
  61. <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  62.  <label></label>
  63.  <br />
  64. <br />
  65. <label>Providers
  66. <select name="providers[]" size="4" multiple="multiple" id="providers">
  67. <option value="Select">Select up to two options</option>
  68.  <option value="1">BlueCross BlueShield</option>
  69.  <option value="2">HealthPartners</option>
  70.  <option value="3">PreferredOne</option>
  71.  <?php $providerArr = $_POST['providers'];
  72. foreach ($providerArr as $provider) {
  73.   echo $provider."<br>\n";
  74. }?>
  75. </select>
  76. </label>
  77. <p>
  78.  <label>Submit
  79.   <input type="submit" name="submit" id="submit" value="Submit" />
  80.   </label>
  81.   <br />
  82.  
  83. <input type="hidden" name="MM_insert" value="form1" />
  84. </form>
  85. </body>
  86. </html>
  87. <?php
  88. mysql_free_result($hsa);
  89. mysql_free_result($providers);
  90. ?>
  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

If you can get me to figure this out, I'll buy you a beer next time you're in Minnesota.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Are you trying to store the selected options to the database or are you wanting to pull the select options from the database?

You're a bit scrambled up on the code. If you can clarify the purpose, then we can help straighten you out I think. ;)
  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

Trying to pass it TO the database!
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Okie doke. You need to separate your coding then. Your flow should be a form that submits to a php file (the form action), which takes the REQUEST vars, builds an insert query, and then inserts into a table. Try something like this or this (the latter shows serializing arrays in case you want to go that route).
  • 283banfil
  • Newbie
  • Newbie
  • 283banfil
  • Posts: 6

Post 3+ Months Ago

That looks perfect. Thanks a ton!!
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

If you're still having trouble with this, please resume the conversation here rather than starting a duplicate thread. Please post your updated code and give us a new explanation of the issue and we'll have a look. Thanks.

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 39 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.