943,659 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 654
  • PHP RSS
Jul 3rd, 2009
0

Field List Input Problem

Expand Post »
I am working of a musicians volunteer DB. I have a ADD Volunteer page:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. require_once('auth.php');
  3. ?>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  9. <title>Village Of Hope Worship</title>
  10. <style type="text/css">
  11. <!--
  12. .style1 {
  13. font-size: 24px;
  14. font-weight: bold;
  15. }
  16. .style3 {
  17. font-size: 12px;
  18. font-weight: bold;
  19. font-style: italic;
  20. }
  21. .style5 {
  22. font-size: 10px;
  23. font-family: Verdana, Arial, Helvetica, sans-serif;
  24. color: #0000FF;
  25. }
  26. -->
  27. </style>
  28. </head>
  29.  
  30. <body>
  31. <table width="505" border="0" align="center" cellpadding="2" cellspacing="2">
  32. <tr align="center" valign="middle">
  33. <td height="42" colspan="2"><div align="center" class="style1">New Volunteer Entry
  34. </div></td>
  35. </tr>
  36. <tr>
  37. <td width="348"><span class="style3"><a href="volunteers_list.php">Back to LIST</a> </span></td>
  38. <td width="143" align="center" valign="middle"><div align="center">
  39. <form id="form1" name="form1" method="post" action="volunteers_list_add.php">
  40. <input type="reset" name="Reset" value="Reset" />
  41. <input name="Submit" type="submit" id="Submit" value="SAVE" />
  42.  
  43. </div></td>
  44. </tr>
  45. </table>
  46. <br />
  47. <table width="504" border="0" align="center" cellpadding="2" cellspacing="2">
  48. <tr>
  49. <td width="91"><div align="right">
  50.  
  51. <label>First Name: </label>
  52.  
  53. </div></td>
  54. <td colspan="2"><input name="First_Name" type="text" id="First_Name" maxlength="25" /></td>
  55. </tr>
  56. <tr>
  57. <td><div align="right">
  58. <label>Last Name:</label>
  59. </div></td>
  60. <td colspan="2"><input name="Last_Name" type="text" id="Last_Name" size="42" maxlength="35" /></td>
  61. </tr>
  62. <tr>
  63. <td><div align="right">
  64. <label>Email:</label>
  65.  
  66. </div></td>
  67. <td colspan="2"><input name="Email" type="text" id="Email" size="42" maxlength="60" /></td>
  68. </tr>
  69. <tr>
  70. <td><div align="right">
  71.  
  72. <label>Contact Phone:</label>
  73.  
  74. </div></td>
  75. <td colspan="2"><input name="Contact_Phone" type="text" id="Contact_Phone" maxlength="20" /></td>
  76. </tr>
  77. <tr>
  78. <td><div align="right">
  79.  
  80. <label>Address:</label>
  81.  
  82. </div></td>
  83. <td colspan="2"><input name="Address" type="text" id="Address" size="42" maxlength="60" /></td>
  84. </tr>
  85. <tr>
  86. <td><div align="right">
  87.  
  88. <label>City/St/Zip:</label>
  89.  
  90. </div></td>
  91. <td colspan="2"><input name="City" type="text" id="City" maxlength="30" />
  92. &nbsp;
  93. <input name="State" type="text" id="State" size="3" maxlength="2" />
  94.  
  95. &nbsp;
  96. <input name="Zip" type="text" id="Zip" size="12" maxlength="10" /></td>
  97. </tr>
  98. <tr>
  99. <td> <div align="right">
  100. <label>Username:</label>
  101. </div></td>
  102. <td colspan="2"><input name="Username" type="text" id="Username" maxlength="20" /></td>
  103. </tr>
  104. <tr>
  105. <td><div align="right">Password:</div></td>
  106. <td colspan="2"><input name="Password" type="text" id="Password" maxlength="20" /></td>
  107. </tr>
  108. <tr>
  109. <td><div align="right">ADMIN:</div></td>
  110. <td colspan="2"><input name="ADMIN" type="text" id="ADMIN" maxlength="2" /></td>
  111. </tr>
  112. <tr>
  113. <td><div align="right">Instruments:</div></td>
  114. <td width="116" align="center" valign="middle">
  115. <div align="left">
  116. <select name="Instrument" size="5" multiple="multiple" id="Instrument">
  117. <option>Select from the list below...</option>
  118. <option value="Leader">Leader</option>
  119. <option value="Singer">Singer</option>
  120. <option value="Piano">Piano</option>
  121. <option value="Synth">Synth</option>
  122. <option value="Acoustic Guitar">Acoustic Guitar</option>
  123. <option value="Electric Guitar">Electric Guitar</option>
  124. <option value="Bass Guitar">Bass Guitar</option>
  125. <option value="Drums">Drums</option>
  126. <option value="Percussion">Percussion</option>
  127. <option value="Sax/Horn">Sax/Horn</option>
  128. <option value="Flute">Flute</option>
  129. </select>
  130. </div></td>
  131. <td width="277" align="center" valign="middle"><div align="left"><span class="style5">To select multiple, hold down your <br />
  132. Command Key (MAC) or ALT (PC)</span></div></td>
  133. </tr>
  134. <tr>
  135. <td><div align="right">Special Notes: </div></td>
  136. <td colspan="2"><textarea name="Notes" cols="40" rows="4" id="Notes"></textarea></td>
  137. </tr>
  138. </table>
  139. </form>
  140. </body>
  141. </html>

Then I target a page that does the ADD:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // -- Add a record --
  3. ServerConnect();
  4. DB_Connect();
  5.  
  6. // Get values from form.
  7. $First_Name=$_POST['First_Name'];
  8. $Last_Name=$_POST['Last_Name'];
  9. $Email=$_POST['Email'];
  10. $Contact_Phone=$_POST['Contact_Phone'];
  11. $Address=$_POST['Address'];
  12. $City=$_POST['City'];
  13. $State=$_POST['State'];
  14. $Zip=$_POST['Zip'];
  15. $Username=$_POST['Username'];
  16. $Password=$_POST['Password'];
  17. $ADMIN=$_POST['ADMIN'];
  18. $Instrument=$_POST['Instrument'];
  19. $Notes=$_POST['Notes'];
  20.  
  21. $sql="INSERT INTO Volunteers (First_Name, Last_Name, Email, Contact_Phone, Address, City, State, Zip, Username, Password, ADMIN, Instrument, Notes) VALUES ('$First_Name','$Last_Name','$Email','$Contact_Phone','$Address','$City','$State','$Zip','$Username','$Password','$ADMIN','$Instrument','$Notes')";
  22. $result = mysql_query($sql) or die(mysql_error());
  23. echo "<span class=\"style2\">Volunteer Added...</span>";
  24. ?>

I, also, have a EDIT Volunteer page:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. require_once('auth.php');
  3. ?>
  4. <?
  5. include("inc/interface.php");
  6. include("inc/funct.php");
  7. ?>
  8.  
  9. <?php
  10. // -- Add a record --
  11. ServerConnect();
  12. DB_Connect();
  13.  
  14. $id=$_GET['id'];
  15.  
  16. $result=mysql_query("select * from Volunteers where id='$id'");
  17.  
  18. $row=mysql_fetch_assoc($result);
  19.  
  20. ?>
  21.  
  22. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  23. <html xmlns="http://www.w3.org/1999/xhtml">
  24. <head>
  25. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  26. <title>Village Of Hope Worship</title>
  27. <style type="text/css">
  28. <!--
  29. .style1 {
  30. font-size: 24px;
  31. font-weight: bold;
  32. }
  33. .style3 {
  34. font-size: 12px;
  35. font-weight: bold;
  36. font-style: italic;
  37. }
  38. .style5 {
  39. font-size: 10px;
  40. font-family: Verdana, Arial, Helvetica, sans-serif;
  41. color: #0000FF;
  42. }
  43. -->
  44. </style>
  45. </head>
  46.  
  47. <body>
  48. <table width="505" border="0" align="center" cellpadding="2" cellspacing="2">
  49. <tr align="center" valign="middle">
  50. <td height="42" colspan="2"><div align="center" class="style1">Edit Volunteer Entry
  51. </div></td>
  52. </tr>
  53. <tr>
  54. <td width="348"><span class="style3"><a href="volunteers_list.php">Back to LIST</a> </span></td>
  55. <td width="143" align="center" valign="middle"><div align="center">
  56. <form id="form1" name="form1" method="post" action="volunteers_list_update.php">
  57. <input type="reset" name="Reset" value="Reset" />
  58. <input name="Submit" type="submit" id="Submit" value="SAVE" />
  59.  
  60. </div></td>
  61. </tr>
  62. <input type="hidden" name="id" value="<? echo $row['id']; ?>">
  63. </table>
  64. <br />
  65. <table width="504" border="0" align="center" cellpadding="2" cellspacing="2">
  66. <tr>
  67. <td width="91"><div align="right">
  68.  
  69. <label>First Name: </label>
  70.  
  71. </div></td>
  72. <td colspan="2"><input name="First_Name" type="text" id="First_Name" value="<? echo $row['First_Name']; ?>" maxlength="25" /></td>
  73. </tr>
  74. <tr>
  75. <td><div align="right">
  76. <label>Last Name:</label>
  77. </div></td>
  78. <td colspan="2"><input name="Last_Name" type="text" id="Last_Name" size="42" value="<? echo $row['Last_Name']; ?>" maxlength="35" /></td>
  79. </tr>
  80. <tr>
  81. <td><div align="right">
  82. <label>Email:</label>
  83.  
  84. </div></td>
  85. <td colspan="2"><input name="Email" type="text" id="Email" size="42" value="<? echo $row['Email']; ?>" maxlength="60" /></td>
  86. </tr>
  87. <tr>
  88. <td><div align="right">
  89.  
  90. <label>Contact Phone:</label>
  91.  
  92. </div></td>
  93. <td colspan="2"><input name="Contact_Phone" type="text" id="Contact_Phone"value="<? echo $row['Contact_Phone']; ?>" maxlength="20" /></td>
  94. </tr>
  95. <tr>
  96. <td><div align="right">
  97.  
  98. <label>Address:</label>
  99.  
  100. </div></td>
  101. <td colspan="2"><input name="Address" type="text" id="Address" value="<? echo $row['Address']; ?>" size="42" maxlength="60" /></td>
  102. </tr>
  103. <tr>
  104. <td><div align="right">
  105.  
  106. <label>City/St/Zip:</label>
  107.  
  108. </div></td>
  109. <td colspan="2"><input name="City" type="text" id="City" value="<? echo $row['City']; ?>" maxlength="30" />
  110. &nbsp;
  111. <input name="State" type="text" id="State" size="3" value="<? echo $row['State']; ?>" maxlength="2" />
  112.  
  113. &nbsp;
  114. <input name="Zip" type="text" id="Zip" size="12" value="<? echo $row['Zip']; ?>" maxlength="10" /></td>
  115. </tr>
  116. <tr>
  117. <td> <div align="right">
  118. <label>Username:</label>
  119. </div></td>
  120. <td colspan="2"><input name="Username" type="text" id="Username" value="<? echo $row['Username']; ?>" maxlength="20" /></td>
  121. </tr>
  122. <tr>
  123. <td><div align="right">Password:</div></td>
  124. <td colspan="2"><input name="Password" type="text" id="Password" value="<? echo $row['Password']; ?>" maxlength="20" /></td>
  125. </tr>
  126. <tr>
  127. <td><div align="right">ADMIN:</div></td>
  128. <td colspan="2"><input name="ADMIN" type="text" id="ADMIN" value="<? echo $row['ADMIN']; ?>" maxlength="2" /></td>
  129. </tr>
  130. <tr>
  131. <td><div align="right">Instruments:</div></td>
  132. <td width="116" align="center" valign="middle">
  133. <div align="left">
  134. <select name="Instrument" size="5" multiple="multiple" id="Instrument">
  135. <option>Select from the list below...</option>
  136. <option value="Leader">Leader</option>
  137. <option value="Singer">Singer</option>
  138. <option value="Piano">Piano</option>
  139. <option value="Synth">Synth</option>
  140. <option value="Acoustic Guitar">Acoustic Guitar</option>
  141. <option value="Electric Guitar">Electric Guitar</option>
  142. <option value="Bass Guitar">Bass Guitar</option>
  143. <option value="Drums">Drums</option>
  144. <option value="Percussion">Percussion</option>
  145. <option value="Sax/Horn">Sax/Horn</option>
  146. <option value="Flute">Flute</option>
  147. </select>
  148. </div></td>
  149. <td width="277" align="center" valign="middle"><div align="left"><span class="style5">To select multiple, hold down your <br />
  150. Command Key (MAC) or ALT (PC)</span></div></td>
  151. </tr>
  152. <tr>
  153. <td><div align="right">Special Notes: </div></td>
  154. <td colspan="2"><textarea name="Notes" cols="40" rows="4" id="Notes" value="<? echo $row['Notes']; ?>" ></textarea></td>
  155. </tr>
  156. </table>
  157. </form>
  158. </body>
  159. </html>

Then I target a page that does the UPDATE:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // -- Update a record --
  3. ServerConnect();
  4. DB_Connect();
  5.  
  6. // Get values from form.
  7. $id=$_POST['id'];
  8. $First_Name=$_POST['First_Name'];
  9. $Last_Name=$_POST['Last_Name'];
  10. $Email=$_POST['Email'];
  11. $Contact_Phone=$_POST['Contact_Phone'];
  12. $Address=$_POST['Address'];
  13. $City=$_POST['City'];
  14. $State=$_POST['State'];
  15. $Zip=$_POST['Zip'];
  16. $Username=$_POST['Username'];
  17. $Password=$_POST['Password'];
  18. $ADMIN=$_POST['ADMIN'];
  19. $Instrument=$_POST['Instrument'];
  20. $Notes=$_POST['Notes'];
  21.  
  22.  
  23. $sql="UPDATE Volunteers SET First_Name='$First_Name', Last_Name='$Last_Name', Email='$Email', Contact_Phone='$Contact_Phone', Address='$Address', City='$City', State='$State', Zip='$Zip', Username='$Username', Password='$Password', ADMIN='$ADMIN', Instrument='$Instrument', Notes='$Notes' WHERE id='$id'";
  24. //echo "$sql";
  25. $result = mysql_query($sql) or die(mysql_error());
  26. echo "<span class=\"style2\">Volunteer Updated...</span>";
  27. ?>

Three issues I need help to resolve:

1) When I select more then one choice from my Instrument field, then select the ADD button, the multiple selections do not carry over to my mySQL table record. Only one value does. The field is a varchar type with plenty of characters allowed.

2) Once I get the data into my DB properly, I need to then be able to have it display properly when I retrieve it on my Update Volunteer Entry page.

3) Eventually, I will establish a Performance Date table in which I will assign Volunteers to performances. When I add a Volunteer:

IF they have multiple instruments they play, I would to provide the user a menu of choices for the field, based of the field Volunteer::Instrument
ELSE
Display it this way: Instrument - Piano
END IF

On a personal note, this is a online DB I am working on for the local Rescue Mission (Homeless Shelter Program). This is my first PHP/mySQL project, so I got much to learn. But once I see how things are done, I catch on pretty quickly.

If there is anyone who would like to volunteer their services to assist me in finishing this project for them, I could make sure you get a Letter of Donated Services from the the Non-Profit organization(tax right off). Just give me a PRIVATE message, if you are willing to help.

Thanks and I appreciate any help that can be offered.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dwdata is offline Offline
44 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Field List Input Problem

You need to change the name of the instrument select element to "Instrument[]". Then when adding to the database run implode(',',$Instrument); because it will return an array of selected elements. Implode will put them into a comma separated string so it can be put into the database.

On the update page you will need to put the instruments into an array and loop through them checking if they are selected (You should also do this for the add page as it will help keep the data uniform and prevent errors).
ex.
PHP Syntax (Toggle Plain Text)
  1. $selected = explode(',',$Instruments); //instrument data from database
  2. $instruments = array('Leader','Singer','Piano');
  3. $options = '';
  4. foreach( $instruments as $inst ) {
  5. $sel = '';
  6. if ( in_array( $inst,$selected ) ) {
  7. $sel = 'selected="selected" ';
  8. }
  9. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
  10. }

and then echo $options where the options for the select should be in the update form.

Make sure you sanitize your input by adding field validation and using mysql_real_escape_string to prevent sql injection.
Last edited by kkeith29; Jul 3rd, 2009 at 3:06 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 3rd, 2009
0

Re: Field List Input Problem

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
You need to change the name of the instrument select element to "Instrument[]". Then when adding to the database run implode(',',$Instrument); because it will return an array of selected elements. Implode will put them into a comma separated string so it can be put into the database.

On the update page you will need to put the instruments into an array and loop through them checking if they are selected (You should also do this for the add page as it will help keep the data uniform and prevent errors).
ex.
PHP Syntax (Toggle Plain Text)
  1. $selected = explode(',',$Instruments); //instrument data from database
  2. $instruments = array('Leader','Singer','Piano');
  3. $options = '';
  4. foreach( $instruments as $inst ) {
  5. $sel = '';
  6. if ( in_array( $inst,$selected ) ) {
  7. $sel = 'selected="selected" ';
  8. }
  9. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
  10. }

and then echo $options where the options for the select should be in the update form.

Make sure you sanitize your input by adding field validation and using mysql_real_escape_string to prevent sql injection.
Sorry. I am struggling with this. Base of the code I provided in the original post. Will you please modify it where the changes are needed. I can't seem to get the order right. I am sure once I see it, it will all make sense and I'll learn from it.

I really appreciate it.
Reputation Points: 10
Solved Threads: 0
Light Poster
dwdata is offline Offline
44 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Field List Input Problem

This is the add page. It submits to itself now since there is no reason to have another page process it.

Put this code on one page:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. include 'auth.php';
  4.  
  5. ServerConnect();
  6. DB_Connect();
  7.  
  8. $result = '';
  9. $error = array();
  10.  
  11. if ( isset( $_POST['submit'] ) ) {
  12. foreach( $_POST as $key => $value ) {
  13. ${$key} = ( is_array( $value ) ? array_map( 'mysql_real_escape_string',$value ) : mysql_real_escape_string( $value ) );
  14. }
  15. if ( empty( $First_Name ) ) {
  16. $error[] = "First Name is empty";
  17. }
  18. //ect. need to add more validation for each field. search google and you will find many resources to help with form validation
  19. if ( count( $error ) == 0 ) {
  20. $Instrument = implode( ',',$Instrument );
  21. mysql_query("INSERT INTO Volunteers (First_Name, Last_Name, Email, Contact_Phone, Address, City, State, Zip, Username, Password, ADMIN, Instrument, Notes) VALUES ('{$First_Name}','{$Last_Name}','{$Email}','{$Contact_Phone}','{$Address}','{$City}','{$State}','{$Zip}','{$Username}','{$Password}','{$ADMIN}','{$Instrument}','{$Notes}')") or die('Error: ' . mysql_error());
  22. $result = "<span class=\"style2\">Volunteer Added...</span>";
  23. }
  24. }
  25.  
  26. $errstr = '';
  27. if ( count( $error ) > 0 ) {
  28. $errstr = "<div id=\"error\">\n\t<ul>\n";
  29. foreach( $error as $err ) {
  30. $errstr .= "\t\t<li>{$err}</li>\n";
  31. }
  32. $errstr .= "\t</ul>\n</div>";
  33. }
  34.  
  35. echo <<<HTML
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  40. <title>Village Of Hope Worship</title>
  41. <style type="text/css">
  42. <!--
  43. .style1 {
  44. font-size: 24px;
  45. font-weight: bold;
  46. }
  47. .style3 {
  48. font-size: 12px;
  49. font-weight: bold;
  50. font-style: italic;
  51. }
  52. .style5 {
  53. font-size: 10px;
  54. font-family: Verdana, Arial, Helvetica, sans-serif;
  55. color: #0000FF;
  56. }
  57. -->
  58. </style>
  59. </head>
  60. <body>
  61. <table width="505" border="0" align="center" cellpadding="2" cellspacing="2">
  62. <tr align="center" valign="middle">
  63. <td height="42" colspan="2"><div align="center" class="style1">New Volunteer Entry
  64. </div></td>
  65. </tr>
  66. <tr>
  67. <td width="348"><span class="style3"><a href="volunteers_list.php">Back to LIST</a> </span></td>
  68. <td width="143" align="center" valign="middle"><div align="center">
  69. <form id="form1" name="form1" method="post" action="{$_SERVER['PHP_SELF']}">
  70. <input type="reset" name="Reset" value="Reset" />
  71. <input name="submit" type="submit" id="Submit" value="SAVE" />
  72.  
  73. </div></td>
  74. </tr>
  75. </table>
  76. <br />
  77. {$errstr}
  78. <table width="504" border="0" align="center" cellpadding="2" cellspacing="2">
  79. <tr>
  80. <td width="91"><div align="right">
  81.  
  82. <label>First Name: </label>
  83.  
  84. </div></td>
  85. <td colspan="2"><input name="First_Name" type="text" id="First_Name" maxlength="25" /></td>
  86. </tr>
  87. <tr>
  88. <td><div align="right">
  89. <label>Last Name:</label>
  90. </div></td>
  91. <td colspan="2"><input name="Last_Name" type="text" id="Last_Name" size="42" maxlength="35" /></td>
  92. </tr>
  93. <tr>
  94. <td><div align="right">
  95. <label>Email:</label>
  96.  
  97. </div></td>
  98. <td colspan="2"><input name="Email" type="text" id="Email" size="42" maxlength="60" /></td>
  99. </tr>
  100. <tr>
  101. <td><div align="right">
  102.  
  103. <label>Contact Phone:</label>
  104.  
  105. </div></td>
  106. <td colspan="2"><input name="Contact_Phone" type="text" id="Contact_Phone" maxlength="20" /></td>
  107. </tr>
  108. <tr>
  109. <td><div align="right">
  110.  
  111. <label>Address:</label>
  112.  
  113. </div></td>
  114. <td colspan="2"><input name="Address" type="text" id="Address" size="42" maxlength="60" /></td>
  115. </tr>
  116. <tr>
  117. <td><div align="right">
  118.  
  119. <label>City/St/Zip:</label>
  120.  
  121. </div></td>
  122. <td colspan="2"><input name="City" type="text" id="City" maxlength="30" />
  123. &nbsp;
  124. <input name="State" type="text" id="State" size="3" maxlength="2" />
  125.  
  126. &nbsp;
  127. <input name="Zip" type="text" id="Zip" size="12" maxlength="10" /></td>
  128. </tr>
  129. <tr>
  130. <td> <div align="right">
  131. <label>Username:</label>
  132. </div></td>
  133. <td colspan="2"><input name="Username" type="text" id="Username" maxlength="20" /></td>
  134. </tr>
  135. <tr>
  136. <td><div align="right">Password:</div></td>
  137. <td colspan="2"><input name="Password" type="text" id="Password" maxlength="20" /></td>
  138. </tr>
  139. <tr>
  140. <td><div align="right">ADMIN:</div></td>
  141. <td colspan="2"><input name="ADMIN" type="text" id="ADMIN" maxlength="2" /></td>
  142. </tr>
  143. <tr>
  144. <td><div align="right">Instruments:</div></td>
  145. <td width="116" align="center" valign="middle">
  146. <div align="left">
  147. <select name="Instrument[]" size="5" multiple="multiple" id="Instrument">
  148. <option>Select from the list below...</option>
  149. <option value="Leader">Leader</option>
  150. <option value="Singer">Singer</option>
  151. <option value="Piano">Piano</option>
  152. <option value="Synth">Synth</option>
  153. <option value="Acoustic Guitar">Acoustic Guitar</option>
  154. <option value="Electric Guitar">Electric Guitar</option>
  155. <option value="Bass Guitar">Bass Guitar</option>
  156. <option value="Drums">Drums</option>
  157. <option value="Percussion">Percussion</option>
  158. <option value="Sax/Horn">Sax/Horn</option>
  159. <option value="Flute">Flute</option>
  160. </select>
  161. </div></td>
  162. <td width="277" align="center" valign="middle"><div align="left"><span class="style5">To select multiple, hold down your <br />Command Key (MAC) or ALT (PC)</span></div></td>
  163. </tr>
  164. <tr>
  165. <td><div align="right">Special Notes: </div></td>
  166. <td colspan="2"><textarea name="Notes" cols="40" rows="4" id="Notes"></textarea></td>
  167. </tr>
  168. </table>
  169. </form>
  170. {$result}
  171. </body>
  172. </html>
  173. HTML;
  174.  
  175. ?>

You can change where the errors and result go. You need to add validation to the forms.

I am working on the edit page.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 3rd, 2009
0

Re: Field List Input Problem

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
You need to change the name of the instrument select element to "Instrument[]". Then when adding to the database run implode(',',$Instrument); because it will return an array of selected elements. Implode will put them into a comma separated string so it can be put into the database.

On the update page you will need to put the instruments into an array and loop through them checking if they are selected (You should also do this for the add page as it will help keep the data uniform and prevent errors).
ex.
PHP Syntax (Toggle Plain Text)
  1. $selected = explode(',',$Instruments); //instrument data from database
  2. $instruments = array('Leader','Singer','Piano');
  3. $options = '';
  4. foreach( $instruments as $inst ) {
  5. $sel = '';
  6. if ( in_array( $inst,$selected ) ) {
  7. $sel = 'selected="selected" ';
  8. }
  9. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
  10. }

and then echo $options where the options for the select should be in the update form.

Make sure you sanitize your input by adding field validation and using mysql_real_escape_string to prevent sql injection.
OK I am doing this and now it does not display ANYTHING in my Instrument List:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // -- Edit record --
  3. ServerConnect();
  4. DB_Connect();
  5.  
  6. $id=$_GET['id'];
  7.  
  8. $result=mysql_query("select * from Volunteers where id='$id'");
  9.  
  10. $row=mysql_fetch_assoc($result);
  11.  
  12. $Instruments1 = $row['Instrument'];
  13.  
  14. $selected = explode(',',$Instruments1); //instrument data from database
  15. $instruments = array('Select from the list below...','Leader','Singer','Piano','Synth','Acoustic Guitar','Electric Guitar','Bass Guitar','Drums','Percussion','Sax/Flute','Flute');
  16. $options = '';
  17. foreach( $instruments as $inst ) {
  18. $sel = '';
  19. if ( in_array( $inst,$selected ) ) {
  20. $sel = 'selected="selected" ';
  21. }
  22. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
  23. }
  24. ?>

And my HTML is formatted like this:

PHP Syntax (Toggle Plain Text)
  1. <tr>
  2. <td><div align="right">Instruments:</div></td>
  3. <td width="116" align="center" valign="middle">
  4. <div align="left">
  5. <select name="Instrument[]" size="5" multiple="multiple" id="Instrument">
  6.  
  7. <? echo $options;
  8. //<option>Select from the list below...</option>
  9. //<option value="Leader">Leader</option>
  10. //<option value="Singer">Singer</option>
  11. //<option value="Piano">Piano</option>
  12. //<option value="Synth">Synth</option>
  13. //<option value="Acoustic Guitar">Acoustic Guitar</option>
  14. //<option value="Electric Guitar">Electric Guitar</option>
  15. //<option value="Bass Guitar">Bass Guitar</option>
  16. //<option value="Drums">Drums</option>
  17. //<option value="Percussion">Percussion</option>
  18. //<option value="Sax/Horn">Sax/Horn</option>
  19. //<option value="Flute">Flute</option>
  20. ?>
  21. </select>
  22. </div></td>
  23. <td width="277" align="center" valign="middle"><div align="left"><span class="style5">To select multiple, hold down your <br />
  24. Command Key (MAC) or ALT (PC)</span></div></td>
  25. </tr>
  26. <tr>
  27. <td><div align="right">Special Notes: </div></td>
  28. <td colspan="2"><textarea name="Notes" cols="40" rows="4" id="Notes" value="<? echo $row['Notes']; ?>" ></textarea></td>
  29. </tr>
  30. </table>
  31. </form>
  32. </body>
  33. </html>

Hopefully it something simple in the HTML or PHP.

It's weird though... It shows a List (displayed very narrow) with the correct rows highlighted but NO VALUES ???

Thanks!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
dwdata is offline Offline
44 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Field List Input Problem

It is my fault. I get to typing and miss simple stuff.
Change:
PHP Syntax (Toggle Plain Text)
  1. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
to
PHP Syntax (Toggle Plain Text)
  1. $options .= "<option {$sel}value=\"{$inst}\">{$inst}</option>\n";
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 3rd, 2009
0

Re: Field List Input Problem

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
It is my fault. I get to typing and miss simple stuff.
Change:
PHP Syntax (Toggle Plain Text)
  1. $options .= "<option {$sel}value=\"{$inst}\"></option>\n";
to
PHP Syntax (Toggle Plain Text)
  1. $options .= "<option {$sel}value=\"{$inst}\">{$inst}</option>\n";
GREAT! It's all working now.
Last edited by dwdata; Jul 3rd, 2009 at 10:04 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
dwdata is offline Offline
44 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Regex help
Next Thread in PHP Forum Timeline: emailto option in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC