Page 1 of 1

php self processing forms and passing arrays

Posted: Tue Sep 28, 2004 7:52 am
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

Posted: Tue Sep 28, 2004 9:35 am
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.

Posted: Tue Sep 28, 2004 10:28 am
by feyd
post the code you are using.. I'd imagine you aren't printing the current_cont array currently..

Posted: Tue Sep 28, 2004 4:07 pm
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]

Posted: Tue Sep 28, 2004 4:17 pm
by feyd
use a loop to output each value in the array.

line 29 is an error, btw.

Posted: Wed Sep 29, 2004 2:38 am
by davidh
Thanks for the help - the loop worked a treat :-)