?
Solved

test checkboxes one after one

Posted on 2012-08-23
15
Medium Priority
?
396 Views
Last Modified: 2012-08-23
Hi
i have a checkboxes that the user can choose 1 or 2 or 3 or 4 or he can select them all
i have the code but it seems not working for all the selection
if (isset($_POST['pulse'])){
     //do here some validation of array elements!
      $pulse1 = array();
      foreach($_POST['pulse'] as $p){
        $pulse1[] = $p;
      }
   }
    $ser= implode(' ', $pulse1);
$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client']; 
        getnonwater($date,$date2,$report,$client,$netw,$auto);
} 
 
      
 
if($ser==1){
    include('water1.php');  
    water1($date,$date2,$report,$auto,$ser); 
  }
if($ser==2){
    include('water2.php');  
    water2($date,$date2,$report,$auto);
  }
if($c==3){
    include('water3.php');  
    water3($date,$date2,$report,$auto,$ser);
  }
if($ser==4){
    include('water4.php');  
    water4($date,$date2,$report,$auto,$ser);
  }

Open in new window

0
Comment
Question by:asaidi
  • 8
  • 5
  • 2
15 Comments
 

Author Comment

by:asaidi
ID: 38324090
sorry
i change $c with $ser on line 27 and still not working
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38324145
Can't see what your question is - user can select 1,2.. all checkboxes - what is not working?

There are no checkboxes in the code you have posted.
0
 

Author Comment

by:asaidi
ID: 38324164
here i can select

 <tr><td> <form action='reportn.php?auto=<?php echo $auto;?>' method='POST'></td></tr>  
<tr>
                        <td align="left">Pulser Channel</td>
                        <td>
                            <input type="checkbox" name ="pulse[]" value="1" checked>1<br>
                            <input type="checkbox" name ="pulse[]" value="2" checked>2<br>
                            <input type="checkbox" name ="pulse[]" value="3" checked>3<br>
                            <input type="checkbox" name ="pulse[]" value="4" checked>4
                        </td>
                           
                    </tr>             

                    
                        </tr><table>
                        <tr><td> <input  type="submit" name="submit" value="Run" ></td> 
                        <td><input type="reset" name="submit" value="Reset" /></td> 
                       <td><INPUT TYPE="button" VALUE="Back" onClick="window.name='autoreload';history.go(-1)"></td>
                         </tr></table>      

Open in new window

then it comes to the reportn.php
$ser=array();
 
if (isset($_POST['pulse'])){
     //do here some validation of array elements!
      $pulse1 = array();
      foreach($_POST['pulse'] as $p){
        $pulse1[] = $p;
      }
   }
    $ser= implode(' ', $pulse1);
$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client']; 
        getnonwater($date,$date2,$report,$client,$netw,$auto);
} 
 
      
 
if($ser==1){
    include('water1.php');  
    water1($date,$date2,$report,$auto,$ser); 
  }
if($ser==2){
    include('water2.php');  
    water2($date,$date2,$report,$auto,$ser);
  }
if($c==3){
    include('water3.php');  
    water3($date,$date2,$report,$auto,$ser);
  }
if($ser==4){
    include('water4.php');  
    water4($date,$date2,$report,$auto,$ser);
  }


 

Open in new window

when i choose 1 it is working but for 2 or 3 or 4 no..
and also when i choose all of them it is working

water1.php
function water1($startDate,$endDate,$report,$auto,$ser){

$sql="SELECT * from count_transactions
          WHERE DATE(tran_date) BETWEEN '$startDate' AND '$endDate' and account_no=$auto and pulse_channel=$ser 
          group by customer_no,unit_serial"; 
          $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
          $num = mysql_num_rows($res);
           
          while($row = mysql_fetch_array($res)) { 

Open in new window

the same code for water2 water3 and water4
why i choose 4 different code bcse inside
i must show column1 or column2 or ..column4
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38324204
Ok when you say it is not working - do you get an error? What do you expect to see and what is actually happening
0
 

Author Comment

by:asaidi
ID: 38324233
like when it is for pulse 2 3 and 4 not working
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38324247
Yes but not working how - you are familiar with your application - we are not. You need to explain what it is you are expecting to happen and what actually happens. Just saying it is not working does not help us understand your problem.
0
 

Author Comment

by:asaidi
ID: 38324287
it is like confusing when i choose 4 means all the boxes checked still he run when the checkbox is 1 ..
how i can test these checkboxes
if i checked 2 it shows only 2 and so
0
 

Author Comment

by:asaidi
ID: 38324325
when i run this
$ser= implode(' ', $pulse1);
$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client'];
        getnonwater($date,$date2,$report,$client,$netw,$auto);
}
it  runs when it is function 1 and function 2 and and 3..not include('nonwater.php');
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38324362
Please read this article and see if it helps.  If not, please post back and help us understand what is not working for you.  It will be helpful if you can isolate a small test case so we can see the issue.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

Best regards, ~Ray
0
 

Author Comment

by:asaidi
ID: 38324472
Hi
thanks for the site help..
everythings is working for when check=1 or 2 or 3 or 4
but when i checked all of them
it is working but after that it runs for 1 and 2 and 3 ..
means for pulse=1 2 3 4 excecute the code nonwater.php and the rest of the test
$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client']; 
        getnonwater($date,$date2,$report,$client,$netw,$auto);
} 
 
      
 
if($ser==1){
    include('water1.php');  
    water1($date,$date2,$report,$auto,$ser); 
  }
if($ser==2){
    include('water2.php');  
    water2($date,$date2,$report,$auto,$ser);
  }
if($c==3){
    include('water3.php');  
    water3($date,$date2,$report,$auto,$ser);
  }
if($ser==4){
    include('water4.php');  
    water4($date,$date2,$report,$auto,$ser);
  }


 
 

Open in new window

as you can see when the $c>=4normally it goes to nonwater.php
but here it runs the nonwater.php and the water1 ...water4
0
 

Author Comment

by:asaidi
ID: 38324478
$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client']; 
        getnonwater($date,$date2,$report,$client,$netw,$auto);
} 
if($ser==1){
    include('water1.php');  
    water1($date,$date2,$report,$auto,$ser); 
  }
if($ser==2){
    include('water2.php');  
    water2($date,$date2,$report,$auto,$ser);
  }
if($ser==3){
    include('water3.php');  
    water3($date,$date2,$report,$auto,$ser);
  }
if($ser==4){
    include('water4.php');  
    water4($date,$date2,$report,$auto,$ser);
  }

Open in new window

0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38324486
Ok here is your problem

You are using an array without index names for your checkbox

pulse[]

When a form is submitted only checkeboxes that are checked are returned.

Here is what you need to do

1. change your checkbox definitions to

<input type="checkbox" name ="pulse[1]" value="1" checked>1<br>
<input type="checkbox" name ="pulse[2]" value="2" checked>2<br>
<input type="checkbox" name ="pulse[3]" value="3" checked>3<br>
<input type="checkbox" name ="pulse[4]" value="4" checked>4

Open in new window


Now you have two options

1. Change your server code to expressly check for the existence of a pulse value so

...
$pulse = $_POST['pulse'];
...
if(isset($pulse[1])){
 ...
}
if(isset($pulse[2])){
 ...
}
if(isset($pulse[3])){
 ...
}
if(isset($pulse[4])){
 ...
}

Open in new window

Second option is a trick

1. Change your pusle definitions as above i.e. pulse[1], pulse[2] etc for the names of your checkboxes

2. Add the following to the start of your form
  <input type="hidden" name="pulse[1]" value="0" />
  <input type="hidden" name="pulse[2]" value="0" />
  <input type="hidden" name="pulse[3]" value="0" />
  <input type="hidden" name="pulse[4]" value="0" />

Open in new window

This should result in your original code working.

Of the two the first solution is better.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 38324531
As usual, var_dump() is your friend.  Computer programming does only one thing: Transform representations of data from one format to another.  So when you use var_dump() to see the data, you can see the transformations your programs are creating.  If you use var_dump() on the intermediate data, such as the inputs from an HTML form, your programming life will instantly get easier!
http://us.php.net/manual/en/function.var-dump.php
<?php // RAY_temp_asaidi.php
error_reporting(E_ALL);


// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
1<input type="checkbox" name ="pulse[1]" value="1" checked>1<br>
2<input type="checkbox" name ="pulse[2]" value="2" checked>2<br>
3<input type="checkbox" name ="pulse[3]" value="3" checked>3<br>
4<input type="checkbox" name ="pulse[4]" value="4" checked>4<br>
<input type="submit" name="Go!" />
</form>
ENDFORM;

// CREATE THE ARRAY OF NAMES TO BE ASSOCIATED WITH THE VALUES FROM THE FORM
$pulse_names[1] = 'Name1';
$pulse_names[2] = 'Name2';
$pulse_names[3] = 'Name3';
$pulse_names[4] = 'Name4';

// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST))
{
    // COPY THE 'pulse' ARRAY FROM THE POST ARRAY
    $pulse = isset($_POST['pulse']) ? $_POST['pulse'] : array();

    // IF THERE IS NO SELECTION IN THE 'pulse' ARRAY
    if (empty($pulse))
    {
        $msg = 'You chose nothing';
    }

    // IF THERE IS ANY SELECTION IN THE 'pulse' ARRAY
    else
    {
        // GET THE NAMES ASSOCIATED WITH THE KEYS
        $new = array_intersect_key($pulse_names, $pulse);
        $msg = 'You chose ' . implode(', ', $new);
    }

    // SHOW THE MESSAGE WITH THE CHOICES
    echo PHP_EOL . $msg;
}

// SEND THE FORM TO THE CLIENT BROWSER
echo $form;

Open in new window

0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 1000 total points
ID: 38324537
if you don't want the if statements to run if all clicked then do this

$c=count($pulse1);
 if($c>=4){
  include('nonwater.php');  
        $netw = empty($_POST['netw'])?null:$_POST['netw'];
        $client = empty($_POST['client'])?null:$_POST['client']; 
        getnonwater($date,$date2,$report,$client,$netw,$auto);
} 
else {  // Put the 1 - 4 in the else block of the count check

if($ser==1){
    include('water1.php');  
    water1($date,$date2,$report,$auto,$ser); 
  }
if($ser==2){
    include('water2.php');  
    water2($date,$date2,$report,$auto,$ser);
  }
if($ser==3){
    include('water3.php');  
    water3($date,$date2,$report,$auto,$ser);
  }
if($ser==4){
    include('water4.php');  
    water4($date,$date2,$report,$auto,$ser);
  }
}                                

Open in new window

0
 

Author Closing Comment

by:asaidi
ID: 38324588
Thanks<br />the only that else change the situation i did not change the code i add the else as julianH suggested but all suggested code was helpful thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question