Javascript array to PHP using cookies problem
Hi,
I am having a little nightmare with a small application that I am writing in PHP. One part of the site allows the user to click on check boxes to select users from a list. This includes a 'select all' option to check all the check boxes. Now this list can be any size in length, so I do not know the values of each box. When the user submits the selection a javascript function is called that adds all the selected check boxes into an array and should be stored in a cookie. The next page should read this serialized array from the cookie to allow updates to a MySQL database. The problem that I am having at the moment is that I am getting the following error. Quote:
Code:
var mailchk = null; Code:
/*get javascript array from cookie.*/ Thanks... |
Quote:
What I do is use an array to set the "name" attribute of the HTML checkbox element and then when the form is submitted, you just need to retrieve these. For example: let's store each checkbox as "id1", "id2" etc. etc. using an array. All you need to do to populate the checkbox is run this through an array to get 1,2,3... etc. |
Sadly not as the values of the check boxes are IDs in a database table, and depending on the search results might not be in a linear format.
That is why I am looking at storing the checkbox values in an array, which is in essence the user id's in the database. I am interested in a better way of doing this, than using cookies as it does not seem to be the most reliable way of passing the data around the application. Thanks again in advance. |
Just retrieve the Ids from the database, use that in an array to set the "name" attribute of the checkboxes in the form field and when you retrieve form, you can access the elements using that ID.
If you set the id to each corresponding check item, it will work. No need for it to be in any order (asc or desc). |
You're right; cookies aren't that reliable -- users can easily turn them off or delete them. Users can also look at the values of their cookies, and they're transmitted as plain text, so they're not particularly secure, either.
Personally, I'd use PHP sessions rather than keeping everything in cookies. PHP Sessions also use cookies internally when they're available, but can work without them if needed. The great thing is it's all transparent; all you have to to is call session_start() at the beginning of your code on every page, and you get an array called $_SESSION that is persistent for a given user and reasonably secure. Even better, you don't need to bother with that messy array serialising; just dump all your array values into $_SESSION[], and you've got them to hand whenever you need them. Hope that helps :) |
General advice: even if you use sessions, you should try to encrypt the data. Sessions are usually stored in cookies ;)...
|
Quote:
The only real potential security issue comes from someone trying to take over another user's session by using their session ID, but PHP uses a few other tricks internally (eg IP address) to make sure the session ID being used is legitimate, so it's not easy to hack. |
Quote:
Besides, I still think that the original question can be solved without discussing either of these issues. The solution I mentioned in my previous post should help. |
All working now, following your guidance... plus a few minor changes to the code.
Many thanks for your help it is very much appreciated. |
Quote:
|
I used the method described in post number 4.
Quote:
Thanks again... |
All times are GMT -5. The time now is 08:34 PM. |