Posting and Receiving Data

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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Posting and Receiving Data

Post 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");
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

There's a function called array_unique.

http://uk.php.net/manual/en/function.array-unique.php
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.";
}
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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!!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
Post Reply