Jump to content

Recommended Posts

Hey everyone. I have a form that lists necklaces. The point is for me to receive via email a list of the different styles the visitors like so I want them to be able to select as many as they like using checkboxes. It's not being stored in a database. I just want the text emailed to me. Everything works fine for single selections. Now I'm having trouble getting it to work for multiple selections.

 

In the sample HTML form I put...

 

<input type="checkbox" name="Necklaces" value="One"> One
<input type="checkbox" name="Necklaces" value="Two"> Two

 

On the net I learned to add brackets to the names so the form will know it's an array, so then I put this...

 

<input type="checkbox" name="Necklaces[]" value="One"> One
<input type="checkbox" name="Necklaces[]" value="Two"> Two

 

In the PHP form I put this...

 

$Necklaces = $_POST['Necklaces']; 

 

... and selected both options to test it out. When I received the info, it just said the word "array". I searched the net again and found a suggestion to use the "serialize" function to resolve that issue, so then I wrote this...

 

$Necklaces = serialize($_POST['Necklaces']; )

 

When I received the info, it had the right information in this format...

 

 (a:2:{i:0;s:3:"One";i:1;s:3:"Two";}) 

 

...which is what the suggestion said would happen. It then said to unserialize it in order to turn that garbled information back into a usable, readable array. This is where I got stuck because I didn't understand what they were doing since they were sending their information to a database and added a bunch of other stuff like while loops, etc.

 

I understand everything up to this point, so here's my question. How do I use the unserialize function to return the data back into a manageable text format and is there another way to accomplish this same goal without using the serialize function? I keep seeing the foreach function come up in the answers I've found. I understand what foreach does but I don't understand the syntax in order to use it.

 

My site has dozens of styles on it at any given time, so you can see why I want to learn how to use the array instead of writing out code for each individual style.

 

Any help would be greatly appreciated!!!

Ok, I'm not a PHP person so I just went to look up what the implode function is so I'd understand what it does and the syntax. From what I gather, it lists the elements of the array and you can also use it to specify what to put in between those elements. I wanted a comma separating them so I put this...

 

$Necklaces =  implode(",",$_POST['Necklaces']);

 

... and then called to the $Necklaces variable as I originally had it and it worked, it sent me the options separated by commas, yay! I'll have to practice using the foreach but the implode function works great.

 

Thank you so much. I searced the net today and all day yesterday and every answer was so complicated with so many lines of code. I kept saying there has to be an easier way to do this and you showed it to me. You know, they always say to search the net first before asking questions, but sometimes, if you just ask first, it'll save you so much time lol Thanks again!!!

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.