0

I have these group of checkboxes

upon submit .. i want to echo the chosen checkboxes in another page (pay.php)

for example: if I chose park and wash .. in the pay.php page i want to echo park and wash BUT I only get wash (the last chosen checkbox) so how to make all chosen checkboxes printed??

 <form name="input" action="pay.php" method="post">
Services:
         <br/> 

        <input type="checkbox" name="service" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service" value="wash">Wash <br/>
        <input type="checkbox" name="service" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

IN PAY.php:

<?php


//$servicetext=$_POST["service"];
// echo $servicetext;
     ////THE ARRAY PART////

 echo "<table border='0'>
    <tr>
    <th> //PRINT THE ARRAY HERE </th>
    <th>  </th>
    <th>  </th>
    <th>  </th>
    <th>  </th>
<tr/>";

?>
CodeX
  • 135
  • 2
  • 13
  • possible duplicate of [PHP Multiple Checkbox Array](http://stackoverflow.com/questions/14026361/php-multiple-checkbox-array) – singhakash May 15 '15 at 09:10

3 Answers3

2

since the check boxes are multiple you need to create an array of name for that same name of that input type.

 <form name="input" action="pay.php" method="post">
    Services:
             <br/> 

        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

in PAY.PHP, you can access each check box value in the below formats

<?php
if(!empty($_POST['service'])) {
    foreach($_POST['service'] as $service) {
          echo  $service;
         //rest of your code
    }
}

Edited

in PAY.php

<?php
if(!empty($_POST['service'])) {

$i = 0;
$selArr = array(); //i took an array that will store all these check box values
    foreach($_POST['service'] as $service) {
         $selArr[$i] = $service;
         $i++;
    }
}

Edited2

<?php
if(!empty($_POST['service'])) {

$i = 0;
$selArr = array(); //i took an array that will store all these check box values
?>
<table>
        <?php 
         foreach($_POST['service'] as $key=>$service) {
         ?>

         <tr><td><?php echo $key; ?></td><td><?php echo $service; ?></td></tr>
         <?php
        }
            ?>
</table>

    <?php
}

I hope this helps you.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
  • @Sourbah do you have a way to save then in an array then print that array?? seems like your way may work – CodeX May 15 '15 at 09:24
  • @CodeX, i added the code for storing in array in the edited part. – Sourabh Kumar Sharma May 15 '15 at 09:31
  • @Sourbah it's just that now you have an array but I can not put the function to be outputed in the form .. please check the edit above to understand better .. thanks alot by the way!! – CodeX May 15 '15 at 09:49
  • @CodeX, i did not actually understand what are you trying to implement, if you are talking about passing that array in a function as parameter then after the foreach() loop is completed, you can pass that array as a normal parameter. – Sourabh Kumar Sharma May 15 '15 at 09:51
  • I have a service page and pay page .. In the service: you choose from check boxes and enter other data in textboxes then click submit button to go to pay.php ... In the pay: you get a table of the data you entered (like a receipt ) then you click pay .... so now we got the checkboxes as an array .. I want to print them in their place of the table@Sourabh – CodeX May 15 '15 at 09:54
  • @CodeX, please check the edited2 in the answer i hope that helps, it might not be exact result table that you want, but you can modify it as i have created the structure. – Sourabh Kumar Sharma May 15 '15 at 10:02
  • @Sourbah i have a question in php again , if you have the time plz check it.. thanks – CodeX May 17 '15 at 20:22
0

Add the checkbox values into array and then access all the posted values via this array:

HTML:

<form name="input" action="pay.php" method="post">
Services:
         <br/> 

        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

PHP:

Now all posted values are stored in $service[] array.

<?php
$servicetext=$_POST["service"];
var_dump($servicetext); // show all the posted values (content of posted array)
?>
pes502
  • 1,597
  • 3
  • 17
  • 32
0

You need to make the checkbox name as an array as like this service[]

Modify your form as like below :

<form name="input" action="pay.php" method="post">
Services:
         <br/> 
        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>
Jenis Patel
  • 1,617
  • 1
  • 13
  • 20