• You are not logged in. | Login

Post a reply

February 19, 2007 12:51 pm

cplusplus
Member
Ranks

Both checkboxes are selected

The task is to have written ‘You’ve selected value 1’ when you select the first value; ‘You’ve selected value 2’ when you select the second one; and finally I’d like to have ‘You’ve selected value 3’ when I select the third value. But when you select 2 or 3 values at once it’s undesirable to have something like ‘You’ve selected value 1’, ‘You’ve selected value 2’ and ‘You’ve selected value 3’. It’s better to have something like ‘You’ve selected value 1, value 2 and value 3’ without duplicating of the common part ‘You’ve selected’. Help, tell me how to do this? I cannot manage it on my own!

Here is my code

<form action="<?php echo $_SERVER['REQUEST_URI']?>" method="post">
<input type=checkbox name="chek[]" value="chek1"> value 1
<input type=checkbox name="chek[]" value="chek2"> value 2
<input type=checkbox name="chek[]" value="chek3"> value 3

<input type="submit" name="submit" value="You’ve selected">
<? if($submit)
{$sel_chek=$_POST['chek'];
for($i=0;$i<count($sel_chek);$i++)
{
if ($sel_chek[$i]=='chek1'){echo "You’ve selected value 1";} 
else if ($sel_chek[$i]=='chek2'){echo " You’ve selected value 2"; } 
else if ($sel_chek[$i]=='chek3'){echo " You’ve selected value 3";} 
}
}?>

</form>

 

 

February 19, 2007 12:53 pm

phppat
Member
Ranks

Re: Both checkboxes are selected

Frankly speaking, checkbox is a rather special thing. You’d better look up in the HTML-manual before usage.


PHP monster

 

 

February 19, 2007 1:09 pm

cplusplus
Member
Ranks

Re: Both checkboxes are selected

I’ve fixed everything but it doesn’t help.


 

 

February 19, 2007 1:16 pm

Keeper
Member
Ranks

Re: Both checkboxes are selected

var_dump($_POST);

Look what it contains and what these contents are equal to.

You shouldn’t duplicate

{echo "You’ve selected value 1";}

And you do duplicate it.


 

 

February 19, 2007 1:19 pm

cplusplus
Member
Ranks

Re: Both checkboxes are selected

Generally speaking, it’s just a little example. Maybe it’s not good enough but it’s quite suitable to understand the matter of the case: how to evade such situation when by choosing last two checkboxes you have the common part written twice.


 

 

February 19, 2007 1:28 pm

ABK
Member
Ranks

Re: Both checkboxes are selected

Imagine such situation when an alert like this should be shown not to the script but to you personally. Try to write carefully and clearly step-by-step this phrase in a way which seems to you to be the right one. What would you keep in the mind? On what reasons would your choice depend?


 

 

February 19, 2007 1:30 pm

cplusplus
Member
Ranks

Re: Both checkboxes are selected

Well. The code should display following meanings
Selecting one of the checkboxes the alert should correspond to the items selected
‘You’ve selected value 1(2)(3)’
Selecting the fist and the second (or the third) ones
‘You’ve selected value 1’
‘You’ve selected value 2 (3)’
Selecting the second and the third value
‘You’ve selected value 2 and value 3’
Selecting all three values
‘You’ve selected value 1’
‘You’ve selected value 2 and value 3’
Something like that.
It means that alert for the first checkbox remains unchangeable.


 

 

February 19, 2007 1:33 pm

bobbee
Member
Ranks

Re: Both checkboxes are selected

1) the code should display ‘You’ve selected’ if you have really selected something i.e. massive $_POST['chek'] contains elements

if ( isset($_POST['check']) && sizeof($_POST['check']) ) { 
    echo "You’ve selected "; 
} 
$sel_chek=$_POST['check'];

2) as far as more than one value is possible you need a separator. The initial meaning of the separator is blank “”. When at least one value selected is found the separator value is changed to “,”

$sep = ""; 
$pos = strlen("check"); 
foreach($sel_check as $k => $v) { 
    echo $sep."value ".substr($v, $pos); 
    $sep = ", "; 
}

 

 

February 19, 2007 1:36 pm

cplusplus
Member
Ranks

Re: Both checkboxes are selected

And when the alert for the first checkbox doesn’t begin with ‘You’ve selected’ and you need it only for the second and the third ones. What should you do then?


 

 

February 19, 2007 1:40 pm

Keeper
Member
Ranks

Re: Both checkboxes are selected

Maybe you should add

!isset($_POST['check'][1])

to the condition.


 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services