Page 1 of 1

Posting and Receiving Data

Posted: Sun Mar 04, 2007 2:02 pm
by tecktalkcm0391
I have a page with some inputs like this:

Code: Select all

<input name="remove0" type="checkbox" value="1" checked />
<input name="save0" type="checkbox" value="1"/>
<input name="remove1" type="checkbox" value="1" />
<input name="save1" type="checkbox" value="1" checked />
<input name="remove2" type="checkbox" value="1" />
<input name="save2" type="checkbox" value="1" checked />
How can I get the them from a post, like collect all of the removes that are checked, and all of the saves that are checked? And then just get the number after it. So from the above would return something like:

Code: Select all

$removed = array("0");
$save = array("1","2");

Posted: Sun Mar 04, 2007 2:27 pm
by impulse()

Code: Select all

$remove = array();
$save = array();

if ($_POST["remove0"] == "1") {
  array_push($remove, 1);
}
if ($_POST["save0"] == "1") {
  array_push($save, 1);
}
Something along those lines sound work.

Posted: Sun Mar 04, 2007 2:31 pm
by tecktalkcm0391
impulse() wrote:

Code: Select all

$remove = array();
$save = array();

if ($_POST["remove0"] == "1") {
  array_push($remove, 1);
}
if ($_POST["save0"] == "1") {
  array_push($save, 1);
}
Something along those lines sound work.
Thanks, but the inputs are created dynamically, and they can be alot of inputs with the removeX and saveX format.

Posted: Sun Mar 04, 2007 2:33 pm
by impulse()
Try and figure a way to run a foreach loop on $_POST. May be worth setting the checkbox values to something very random that wont ever be used elsewhere.

Posted: Sun Mar 04, 2007 2:44 pm
by tecktalkcm0391
Thanks, I go it.

Now how can I check to make sure that no number in the $save array and the $remove array are the same:

Code: Select all

Save Array ( [0] => 1, [1] => 2, [2] => 3 ) 
Remove Array ( [0] => 1, [1] => 4, [2] => 5 )
I want something that has above, to return that there is a error with the value 1.

Posted: Sun Mar 04, 2007 3:17 pm
by impulse()
There's a function called array_unique.

http://uk.php.net/manual/en/function.array-unique.php

Posted: Sun Mar 04, 2007 3:44 pm
by tecktalkcm0391
impulse() wrote:There's a function called array_unique.

http://uk.php.net/manual/en/function.array-unique.php
Thanks! Ended up not using that but doing this:

Code: Select all

$diff = array_intersect($save, $remove); 
if($diff){
	echo "Error: Please confirm that each item has only one option checked.";
}

Posted: Sun Mar 04, 2007 4:48 pm
by tecktalkcm0391
I am having another problem....

I can get the remove stuff in an array, but I can't remove it from the $cart array.

Code: Select all

$cart = '1,2,3,4,5';
$remove = array("1","2");

if(!empty($remove)){
		foreach($remove as $remove){
			if ($cart) {
				$items = explode(',',$cart);
				$newcart = '';
				foreach ($items as $item) {
					if ($remove != $item) {
						if ($newcart != '') {
							$newcart.= ','.$item;
						} else {
							$newcart = $item;
						}
					}
				}
			}
		}
		$cart = $newcart;
	}
That just ouputs: 1,3,4,5 NOT 3,4,5....help!!!

Posted: Sun Mar 04, 2007 5:45 pm
by volka
tecktalkcm0391 wrote:Now how can I check to make sure that no number in the $save array and the $remove array are the same:
Then why do you use input/checkbox instead of input/radio or select?

Code: Select all

<html>
	<head><title>...</title></head>
	<body>
		<form action="#" method="post">
			<div>
<?php for($i=1; $i<11; $i++) { ?>
				<fieldset><legend>item #<?php echo $i; ?></legend>
					<select name="item[<?php echo $i; ?>]">
						<option>nothing</option>
						<option>remove</option>
						<option>save</option>
					</select>
				</fieldset>
<?php } ?>				
				<input type="submit" />
			</div>
		</form>
<?php
if ( isset($_POST['item']) && is_array($_POST['item']) ) {
	foreach($_POST['item'] as $key=>$value) {
		echo '<div>item:', $key, ', action:', $value, "</div>\n";
	}
}
?>		
	</body>
</html>

Posted: Sun Mar 04, 2007 7:45 pm
by tecktalkcm0391
volka wrote:
tecktalkcm0391 wrote:Now how can I check to make sure that no number in the $save array and the $remove array are the same:
Then why do you use input/checkbox instead of input/radio or select?

Code: Select all

<html>
	<head><title>...</title></head>
	<body>
		<form action="#" method="post">
			<div>
<?php for($i=1; $i<11; $i++) { ?>
				<fieldset><legend>item #<?php echo $i; ?></legend>
					<select name="item[<?php echo $i; ?>]">
						<option>nothing</option>
						<option>remove</option>
						<option>save</option>
					</select>
				</fieldset>
<?php } ?>				
				<input type="submit" />
			</div>
		</form>
<?php
if ( isset($_POST['item']) && is_array($_POST['item']) ) {
	foreach($_POST['item'] as $key=>$value) {
		echo '<div>item:', $key, ', action:', $value, "</div>\n";
	}
}
?>		
	</body>
</html>
Good point , but still does anyone know how to do what I asked in my last post?

Edit: I got it nevermind. Thanks! though : :D