Jump to content

Passing Checkbox Values to Another Page


wolfcry

Recommended Posts

Hey all!

 

Nice to meet you and love the site :)

 

I've been learning PHP for about 6 months and have hit a brick wall and need help please.

 

What I want to do is pass multiple checkbox values from a form on an HTML page to another page (Thank you page) where it can be viewed if it was selected (checked). I know I need to "Get" the information from the quiry string after the information has been submitted but I'm lost on how to exactly set this up so it works properly.

 

EXAMPLE:

<input type="checkbox" name="passon[]" value="Test 1">Test 1

<input type="checkbox" name="passon[]" value="Test 2">Test 2

<input type="checkbox" name="passon[]" value="Test 3">Test 3

<input type="checkbox" name="passon[]" value="Test 4">Test 4

<input type="checkbox" name="passon[]" value="Test 5">Test 5

 

If the user checks test 1, 2, and 4 for example, I want those values to be passed to the thank you page where they can be seen as having been checked such as:

 

Test(s) you're willing to take: Test 1, Test 2, Test 4

 

I have tried everything that I could think of and cannot get this to work so all help will be greatly appreciated.

 

Thanks in advance!

Link to comment
Share on other sites

My suggestion would be to use $_POST instead, and then on the next page (or whatever page handles the form), simply combine all of those values into an array. So you could do:

 

<?php
$array = implode ($_POST['$button1'], $_POST['$button4'], $_POST['$button']);
echo $array;
?>

Link to comment
Share on other sites

thankyou.php

<?php

if (isset($_POST['passon'])) {
     if (is_array($_POST['passon'])) {
             $tests = "You are willing to take ";
             foreach ($_POST['passon'] as $val) {
                   $tests .= $val . ' ';
             }
             $tests .= '!';
     }
}

echo 'Hello Jack!<br />' . $tests;
?>

Link to comment
Share on other sites

wow, wicked response time guys! You both should receive 'fastest customer service award  ;D

 

Frost110, your code worked beautifully....I especially loved the Hello Jack! quip  lol.

 

Thanks a bunch you two..the help was highly appreciated!

 

 

 

 

 

 

Link to comment
Share on other sites

Ok, everything is working as it should so thank you.

 

I have one other question if I may,

If I wanted to customize it so the values appeared in a column format such as the example below, how do I modify this code to do that?

 

EXAMPLE:

Test 1

Test 2

Test 3

etc. etc.

 

I've been experimenting and getting other tweaks and things to work little by little, but I'm hung up on this.

 

Thanks

Link to comment
Share on other sites

<?php

if (isset($_POST['passon'])) {
     if (is_array($_POST['passon'])) {
             $tests = "You are willing to take ";
             foreach ($_POST['passon'] as $val) {
                   $tests .= $val . '<br />';
             }
             $tests .= '!';
     }
}

echo 'Hello Jack!<br />' . $tests;
?>

Link to comment
Share on other sites

I can't think why..

just a silly suggesstion but try changing this line:

$tests = "You are willing to take ";

to this:

$tests = 'You are willing to take ';

might be confusing the php or something.. (probably not though ;))

 

You should even be able to use a table for it like this:

<?php
if (isset($_POST['passon'])) {
     if (is_array($_POST['passon'])) {
             $tests = 'You are willing to take';
             $tests .= '<table align="left" width="100%" border="0" cellpadding="0" cellspacing="0">';
             foreach ($_POST['passon'] as $val) {
                   $tests .= '<tr><td align="left">' . $val . '</td></tr>';
             }
             $tests .= '</table>';
     }
}

echo 'Hello Jack!<br />' . $tests;
?>

Link to comment
Share on other sites

Ok, the table integration didnt cause any problems when testing the form but when viewing the source code after submission, the table that was newly integrated didnt even show up which means it didnt even get created and the output was still in one single string.

 

Also, I adjusted your width="100%" to 200px to see if there was a size reduction and there wasnt. So that means the table actually wasnt created.

 

I tried your suggestion of changing the " to ' but that didnt make a difference either.

 

Not really sure whats going on lol

Link to comment
Share on other sites

Just a shot in the dark, have you viewed the source to see if the script is even trying to display anything? Are you making sure that you are still posting data?

 

My test code is:

<?php
$_POST['passon'] = array("test1", "test3", "test5");
if (isset($_POST['passon'])) {
     if (is_array($_POST['passon'])) {
             $tests = 'You are willing to take';
             $tests .= '<table align="left" width="100%" border="0" cellpadding="0" cellspacing="0">';
             foreach ($_POST['passon'] as $val) {
                   $tests .= '<tr><td align="left">' . $val . '</td></tr>';
             }
             $tests .= '</table>';
     }
}

echo 'Hello Jack!<br />' . $tests;
?>

 

Output was

Hello Jack!<br />You are willing to take
<table align="left" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td align="left">test1</td></tr>
<tr><td align="left">test3</td></tr>
<tr><td align="left">test5</td></tr>
</table>

Link to comment
Share on other sites

Yep, checking everything.

 

Now if I use the exact code above to test the output, it works. It outputs into a table.

 

But if I use a modified version of the code, it doesn't create the table and it outputs into a single string.

 

Here is a modified version. I have two forms that need to use this, the test form with the test information and a form with the following. I am trying to set up this one right now because its higher priority.

<input type="checkbox" name="services[]" value="Marketing and Advertising">Marketing  & Advertising
<input type="checkbox" name="services[]" value="Search Engine Optimization">Search Engine Optimization
<input type="checkbox" name="services[]" value="Web Optimization"> Web optimization
<input type="checkbox" name="services[]" value="Form Hosting">Form Hosting
<input type="checkbox" name="services[]" value="Web Hosting">Web Hosting
<input type="checkbox" name="services[]" value="Web Consultation">Web Consultation
<input type="checkbox" name="services[]" value="All"> All

 

I changed the information on the PHP code to read as:

<?php

if (isset($_POST['services'])) {
     if (is_array($_POST['services'])) {
             $services = "Other services you are interested in";
             foreach ($_POST['services'] as $val) {
                   $services .= $val . '<br /> ';
             }
             $services .= '!';
     }
}

echo 'Other services you are interested in<br />' . $services; 
?>

I had to change the $tests variables to $services because nothing was showing up. Not sure why, considering the variable should be global and not restricted. But I might be forgetting something. Also, I changed the echo 'hello jack" to be what it is, because I didn't want the Hello Jack! there and because if I used:

echo $services; 

to print out "Other services you are interested in" nothing showed up. Again, I'm probably overlooking something small.

 

When I use:

<?php
$_POST['passon'] = array("test1", "test3", "test5");
if (isset($_POST['passon'])) {
     if (is_array($_POST['passon'])) {
             $tests = 'You are willing to take';
             $tests .= '<table align="left" width="100%" border="0" cellpadding="0" cellspacing="0">';
             foreach ($_POST['passon'] as $val) {
                   $tests .= '<tr><td align="left">' . $val . '</td></tr>';
             }
             $tests .= '</table>';
     }
}

echo 'Hello Jack!<br />' . $tests;
?>

 

It prints exactly as it should. Including the "Willing to take" part. But when I make the adjustments (using different variable names such as $services and changing the passon tag to services etc.) and delete the:

$_POST['passon'] = array("test1", "test3", "test5");

 

It again goes back to outputting into a single string. Now if I readd the array but change the tags into other titles such as

$_POST['passon'] = array("Marketing", "SEO", "Web");

It again, outputs into the table but does not show anything that is actually selected. Only the arrays that I am forcing it to print.

 

Again, I'm sure I'm just missing something basic or doing something that is causing the php to conflict, but everything is working as it should with the modified code I have but I just cannot get it to parse in a tabled structure as demonstrated here.

 

Thanks for all your help.

Link to comment
Share on other sites

Ok, I got it to work alittle better but I still cannot get it to parse into a table nor format itself into a column formation.

 

 

Any suggestions are very welcome.

 

Sounds like a problem with your output html can you post the html for the page your are viewing?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.