Current location: Hot Scripts Forums » Programming Languages » PHP » Help with PHP multiple select field

Help with PHP multiple select field

Reply
  #1 (permalink)  
Old 10-23-09, 12:46 AM
paligron paligron is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Help with PHP multiple select field

Hi,
I have a script that updates a multiple select field. I would like it to do the following:
1. check to see if the submitted form has an empty multiple select field and return an error
2. On loading the update page, the previously selected items should be selected in the multiple select field.

Here is what I have so far but I keep on getting the Undefined Index error:

functions.php
PHP Code:
//----------Function for getting item accessories----------
function getAccessories()
{
    
$sql "SELECT accessories FROM tablename ORDER BY accessories"
    
$res mysql_query($sql);

    echo 
"<select name='accessories[]' size='4' multiple='multiple' class='input'><option value='-1'>------------------------</option>";
        while(
$row mysql_fetch_row($res)) { 
            echo  
"<option value='".$row[0]."'>".$row[0]."</option>";
            }
    echo 
"</select>";
}


//----------Function for updating item on mysql db----------
function updateItem($accessories,$id)
{
    if (empty(
$accessories)) {
        return 
1;
          } else {                
        
$sql "UPDATE theitem SET item_accessories = '" $accessories "' WHERE item_id = " $id "";
        
$res mysql_query($sql); // or die(mysql_error());
        
return 99;
        }

update.php
PHP Code:
require_once ('library/database.php');
include (
'library/functions.php');
include (
'library/english.php');
if (isset(
$_POST['update_item'])){
    
$_POST cleanCode($_POST);
    
$serializedaccessories serialize($_POST['accessories']);
        
$res updateItem($serializedaccessories,$_POST['item_id']);
        
//if successful
        
if($res == 99){
            
$message ITEM_EDITED;
            }
        
//if errors occured
        
if($res == 1){
            
$error EMPTY_FIELD;
        }

HTML Code:
<HTML>
<HEAD></HEAD>
<BODY>
    <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" name="fmpost">
		<input name="item_id" type="hidden" value="<?= $id; ?>" /> //$id got from a function
                <span class="error"><?php if(isset($error)){ echo $error;}?></span>
		<span class="message"><?php if(isset($message)){ echo $message;} ?></span>	
                <div><label for="accessories" class="label">Accessories:</label><br /><span class="smallText">Hold down CTRL key and click to select multiple.</span></div>
		<div><? getAccessories(); ?> </div>
                <div><input type="submit" name="update_item" value="Update" class="postbttn"/></div>
</BODY>
</HTML>
Please help

Last edited by wirehopper; 10-23-09 at 08:45 AM. Reason: PHP & HTML tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-23-09, 07:14 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,030
Thanks: 14
Thanked 34 Times in 33 Posts
Quote:
Originally Posted by paligron View Post
Here is what I have so far but I keep on getting the Undefined Index error
The Undefined Index error is telling you that some variable (an array) isn't being declared before use. You can resolve this by declaring all your variables, including arrays, before running any of the code.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-23-09, 08:46 AM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 2,050
Thanks: 9
Thanked 65 Times in 63 Posts
Add var_dump($_REQUEST); as the first line of your code. That will show you what is being sent from the client.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-23-09, 05:04 PM
paligron paligron is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
the undefined variable is the 'accessories', which is the name of the input field. How do you define it as an array? I have tried to define it normally as an array but the submitted result is always empty.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-23-09, 07:19 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,030
Thanks: 14
Thanked 34 Times in 33 Posts
Quote:
Originally Posted by paligron View Post
the undefined variable is the 'accessories', which is the name of the input field. How do you define it as an array? I have tried to define it normally as an array but the submitted result is always empty.
"Undefined index" means the variable you are trying to use doesn't exist (when your variable is not properly set). One way to handle this issue is to check if $_POST['action'] is set before using it. For example:

PHP Code:
if (!isset($_POST['action'])){

//If not isset -> set with dummy value
$_POST['action'] = "undefine";


__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to End User For This Useful Post:
paligron (10-25-09)
  #6 (permalink)  
Old 10-24-09, 12:07 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,702
Thanks: 0
Thanked 29 Times in 29 Posts
I changed your getAccessories() and updateItem() functions.
functions.php
PHP Code:
//----------Function for getting item accessories----------
function getAccessories()
{
 
$sql "SELECT accessories FROM tablename ORDER BY accessories";
 
$res mysql_query($sql);
 echo 
"<select name='accessories[]' size='4' multiple='multiple' class='input'><option value='-1'>------------------------</option>";
 while(
$row mysql_fetch_row($res))
 {
  if(!empty(
$_POST['accessories']))
  {
   if(
in_array($row[0],$_POST['accessories'])){echo  "<option value='".$row[0]."' selected>".$row[0]."</option>";}
   else{echo  
"<option value='".$row[0]."'>".$row[0]."</option>";}
   }
  else{echo  
"<option value='".$row[0]."'>".$row[0]."</option>";}
  }
 echo 
"</select>";
 }

//----------Function for updating item on mysql db----------
function updateItem($accessories,$id)
{
 
$sql "UPDATE theitem SET item_accessories = '" $accessories "' WHERE item_id = " $id "";
 
$res mysql_query($sql); // or die(mysql_error());
 

And I modified your update.php code.
PHP Code:
<?php
require_once ('library/database.php');
include (
'library/functions.php');
include (
'library/english.php');
$message "";
if(!empty(
$_POST['update_item']))
{
 if(empty(
$_POST['accessories']) || ($_POST['accessories'][0] == -&& empty($_POST['accessories'][1])))
 {
  
$message "EMPTY_FIELD";
  
$class "error";
  }
 else
 {
  if(
$_POST['accessories'][0] == -1){array_shift($_POST['accessories']);}
  
$_POST cleanCode($_POST);
  
$serializedaccessories serialize($_POST['accessories']);
  
updateItem($serializedaccessories,$_POST['item_id']);
  
$message "ITEM_EDITED";
  
$class "message";
  }
 }
?>
<HTML>
<HEAD></HEAD>
<BODY>
    <form action="#" method="post" name="fmpost">
     <input name="item_id" type="hidden" value="<?php echo $id//$id got from a function ?>" />
     <?php if($message){echo "<span class='$class'>$message</span>";} ?>    
     <div><label for="accessories" class="label">Accessories:</label><br /><span class="smallText">Hold down CTRL key and click to select multiple.</span></div>
     <div><?php getAccessories(); ?> </div>
     <div><input type="submit" name="update_item" value="Update" class="postbttn"/></div>
    </form>
</BODY>
</HTML>
__________________
Jerry Broughton

Last edited by job0107; 10-24-09 at 12:47 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-25-09, 02:00 AM
paligron paligron is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
thanks job0107

Yup. Thank you guys. Your help is highly appreciated. Job, wrapping ones head around all those if statements, especially for a php newbie like me, is mind tiring. But I understand the concept now. You have also fully answered my original question. I am now on the right track. Thanks a milli.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 10-25-09, 11:30 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,702
Thanks: 0
Thanked 29 Times in 29 Posts
Quote:
Originally Posted by paligron View Post
Yup. Thank you guys. Your help is highly appreciated. Job, wrapping ones head around all those if statements, especially for a php newbie like me, is mind tiring. But I understand the concept now. You have also fully answered my original question. I am now on the right track. Thanks a milli.
Understanding the logic flow is 3/4th the battle in creating any program.
Learning the syntax of the language you wish to program in, is the other 1/4.

So, when you have a problem that seems too difficult to figure out, stop and create a flow chart that describes the program flow.
Once you understand what you have to do to make the program work, figuring out the right commands to use becomes much simpler.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-26-09, 12:18 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,702
Thanks: 0
Thanked 29 Times in 29 Posts
I found an error in your updateItem() function and corrected it.
The error was in the quoting of the $id varable.

PHP Code:
<?php
//----------Function for updating item on mysql db----------
function updateItem($accessories,$id)
{
 
mysql_query("UPDATE theitem SET item_accessories = '".$accessories."' WHERE item_id = '".$id."'");
 }
?>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
paligron (10-26-09)
  #10 (permalink)  
Old 10-26-09, 01:46 AM
paligron paligron is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Thumbs up Thanks

It is now working perfectly. I also noticed that the key to the message/error displays lay with the 'in_array' function. Thanks again. This thread can now be closed. Unfortunately, I must claim ignorance as I don't know how to close this thread.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sports Pick Em racingboy20 Script Requests 2 11-27-09 07:52 PM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 04:27 PM
PHP Redirect Script based on User Text Field skipcollege Script Requests 7 09-18-05 12:03 PM
Use MySql field as source for form select field CutAndPaste PHP 3 03-14-05 10:35 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 11:15 AM


All times are GMT -5. The time now is 12:56 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.