Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Pass variables between two PHP pages

Page1.php {FORM NAME ="form1" METHOD ="POST" ACTION ="page2.php"} <?php $file1 = 424241; ?> <?php $file2 = 424242; ?> <?php $file3 = 424243; ?> <?php $file4 = 424244; ?> <?php $file5 = 424245; ?> <?php $file6 = 424246; ?> <?php $file7 = 424247; ?> <?php $file8 = 424248; ?> <?php $file9 = 424249; ?> <?php $file10 = 4242410; ?>

<?php $i=1; ?>

<?php for ($i=1; $i <= 10; $i++) { ?>

{Input type = 'Checkbox' Name ='checkbox<?php echo $i; ?>' value = '<?php echo ${'file'.$i}; ?>' uncheked } UserID

{INPUT TYPE = "Submit" Name = "Submit1" VALUE = "What ID"} {/FORM}

<?php for ($i=1; $i <= 10; $i++) { ?> <?php if (isset($_POST['Submit1'])) { if (isset($_POST['checkbox'.$i])) { echo ${'chi'.$i} = $_POST['checkbox'.$i] ; } } ?>


Page2.php <?php

    echo $_POST['checkbox1'] ;
            echo "<br/>";
            echo $_POST['checkbox2'] ;
            echo "<br/>";
            echo  $_POST['checkbox3'] ;
            echo "<br/>";

?> I have replace the <> with {} as when i put < for HTML code > it will not display correctly.

Here the question: Right now page1.php is looping 10 times. Let said page1.php looping times is a variable, it might be change to 15 times or even more. therefore I do not want to type manually on page2.php to echo out the value of checkbox.

if page1.php suddenly looping 15 times then page2.php also looping 15 times how can i make sure the two pages of php is looping same times? appreciate if you can give me some idea

Best Regard

2 Answers

Hi!

You could try adding a final hidden input field that has a 'value' of your final i. You could then retrieve it on your next page with $_POST['final_i'];

Thanks Logan, that a great idea. i will try it out