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
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 2:02 pm
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() » Sun Mar 04, 2007 2:27 pm
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.
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 2:31 pm
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() » Sun Mar 04, 2007 2:33 pm
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.
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 2:44 pm
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() » Sun Mar 04, 2007 3:17 pm
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 3:44 pm
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.";
}
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 4:48 pm
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!!!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Mar 04, 2007 5:45 pm
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>
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Mar 04, 2007 7:45 pm
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 :