[SOLVED] php self processing forms and passing arrays

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
davidh
Forum Newbie
Posts: 3
Joined: Tue Sep 28, 2004 7:51 am

php self processing forms and passing arrays

Post by davidh »

Hi,

I am hoping that someone can come up with a solution to a problem I am having.

I have a self processing form with two select lists. basically I want the left list populated with some default values and when an item is selected and the submit button pressed the value is copied over to the select box on the right. Values can be added and removed at will.

I have attempted to do this by creating a hidden field with the right hand select box's contents in:
<input type="hidden" name="current_cont[]" value="<?=$cont?>">

when I load the page I check for current_cont. If it is populated I create the new right hand list by merging current_cont and the results from any select operation on the left hand list.

When I try to use this I am getting valid values on the first select, but subsequently all that is displayed is a list containing array and the last value I selected.

I hope that this makes sense and can be followed.

Many thanks
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

what about setting a cookie with all the items the user has selected?

You could seperate items using a comma and then use explode() to get each value out of the cookie.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post the code you are using.. I'd imagine you aren't printing the current_cont array currently..
davidh
Forum Newbie
Posts: 3
Joined: Tue Sep 28, 2004 7:51 am

Post by davidh »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Sorry was playing with the code.Correct version is as below.
Thanks for any help

Code: Select all

<?php

    $add	 			= (isset($_POST['add']))			? $_POST['add']				: "";	//add tasks button was pressed
    $remove	 			= (isset($_POST['remove']))			? $_POST['remove']			: "";	//remove task button was pressed
    
    $objectslist=("one","two","three","four");
    
    if ($add)
    {
     	$select		= (isset($_POST['select']))		? $_POST['select']	:	"";
    	if(isset($_POST['list_contents']))
    	{
       		$included_tasks = array_merge($_POST['list_contents'], $select);
       	}
       	else
       	{
       		$included_tasks = $select;
       	}
       	
    }
	if ($remove)
	{
		//implement remove code	
	}
	//ouput html header
    page_header();
  ?>  

<form action="<?=PHP_SELF?>" method="POST">
<?php if (isset($included_tasks)){?>

	<INPUT type="hidden" name="list_contents[]" value="<?=$included_tasks?>">
<?php }?>	
	<table cellpadding="5" cellspacing="1" border="0" class="selectform">
		<th class="list" colspan="3">
			select from
		</th>
		<tr valign="top">
			<td colspan="3">
				 write to
			</td>
		</tr>
		<tr valign="top">
			<td colspan="3">
				&nbsp
			</td>
		</tr>
		<tr valign="top" >
			<td class="selectform">
				Task list
			</td>
			<td>
				&nbsp
			</td>
			<td class="selectform">
				<label>Included tasks</label>
			</td>
		</tr>
		<tr valign="top">
			<td class="selectform" rowspan="2">
				<select name="select[]" size="30" multiple class="fixedWidth">
				<?php foreach ($objectslist as $object) 
						{?>
							<option><?=$object['name']." - " .$object['description']?></option>
					<?php } ?>
			</td>
			<td valign="center" align="center">
				<input type="submit" name="add" value=">>" >				
			</td>
			<td class="selectform" rowspan="2">
				<select name="addto[]" size="30" multiple class="fixedWidth">
				<?php foreach ($included_tasks as $included_task) 
						{?>
							<option><?=$included_task?></option>
					<?php } ?>
			</td>
		</tr>
		<tr valign="top">
			<td>
				<input type="submit" name="remove" value="<<" >
			</td>
		</tr>
	</table>
</form>
    <?php page_footer(); ?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use a loop to output each value in the array.

line 29 is an error, btw.
davidh
Forum Newbie
Posts: 3
Joined: Tue Sep 28, 2004 7:51 am

Post by davidh »

Thanks for the help - the loop worked a treat :-)
Post Reply