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/>";

?>
1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@Sourbah do you have a way to save then in an array then print that array?? seems like your way may work
@CodeX, i added the code for storing in array in the edited part.
@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, 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.
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
|
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>

2 Comments

it echos the word array .. @Jenis Patel
just try to check array as like this => print_r($_POST["service"]);
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)
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.