Jump to content

Question Regarding Checkboxes and MySQL


l2hat3

Recommended Posts

Hey everyone. [=

 

I don't have much time to read through all of the forums, because I'm on a time crunch and I'll be putting this certain project behind a couple others while I wait for some answers. This project is confusing me a bit, because I haven't worked much with arrays and the good ol' checkbox, so I need some help getting this to work.

 

This project requires me to set-up a checkbox next to each order that was pulled off a database. You are allowed to select as many checkboxes as you'd like, and then click on the "process receipts" for all of the orders that were selected. I need to be able to have the form send me straight to a new page that pulls a loop of receipts one after another. I have the receipts built, and I can access a receipt one at a time based off the ID of a certain order, and I can easily build a loop that presents the receipts. I'm just having a problem pulling the information off the database based from the checkbox array.

 

If you can give me some ideas on where to start with this, I'd greatly appreciate it.

Note: Example is inside a hidden administrative page. Can't really give you something to play with, sorry! QQ

 

Thanks!

Alex

D.A. Designing

Link to comment
Share on other sites

Start by writing some code and testing it. If it doesn't work, post it here along with a description of the problem, and any errors that are returned.

 

I've built all of my checkboxes accordingly, as well as adding the [] at the end. The reason why I didn't "write some code and test it", is because I'm flat out confused. If I can get some PHPFreak to help give me an example of pulling information from a database on a loop based off the array rendered from the checkbox, I'd greatly appreciate it. I just got hired here, and I'm on a job that was half-way finished and on top of battling the last developer's code, I'm hit with a little bit of an issue with this. I'll go play with the other issues on the bug list, but I'd really love someone to give me a decent example and explain it. I'm not a huge PHP Noob, but if you treat me like one for this issue, that'd be awesome. [=

Link to comment
Share on other sites

just call the checkboxes something like myBox[] (i.e. all with same name) and on the next page loop through that array to find out which ones were selected:

 

C'mon Webstyles, based on your posts I've seen I'm disappointed. I'm sure you are aware that only CHECKED checkboxes are passed in the post data, right? Therefore, the array you receive only contains the checked options. No need to loop over the array.

 

Anyway, I'm not going to write you a solution, but I will give you two pieces of information that should allow you to get started.

 

1. As Webstyles suggested, create your checkboxes as arrays with the ID of the records as the values.

<input type="checkbox" name="selectedReceipts[]" value="<?php echo $receiptID; ?>" >

 

2. On your receiving page you can use the array of checked receipts to generate a query of all the selected receipts using the IN operator.

//Converts ids of selected receipts to comma separated string
$receiptIDs = implode(',', $_POST['selectedReceipts']);
//Create query
$query = "SELECT * FROM table WHERE id IN($receiptIDs)";

 

Note: I left out a lot of validation that would need to be done.

Link to comment
Share on other sites

Thanks, mjdamato. I appreciate your help. I'll test the code today.

 

 

you don't even want to try, you simply want us to do your work for you?

 

Isn't that why I entered this forum? To discuss ways to program it? I've given it shots, believe me. After I spent an hour trying to come up with a solution on my own, I decided that I was unable to do it, and instead of battling myself- I needed to resort to asking other people who know much more than I do. Like I said, I have a huge time crunch and bug list that was given to me the first day I started here to finish, and I'm trying to check off things as fast as possible. Sorry you couldn't help me.

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.