Codeigniter select box not populating session value - php

I am facing a session problem with Codeigniter.
I have a form containing a select box to perform a search. When the form gets submitted, the search criteria is stored in session, which is used to populate the form fields once user gets back to the form. In my local machine, it works fine. But in server, it doesn't for select boxes. Upon clearing the browser cache only, it works.
I have just echoed the session variable. outside the form. And it holds the correct data.In Firebug, it shows the correct value also - <option selected="selected">foobar</option>. But I can't see the selected value in the select box.
FYI - I am using the following CI form helper to print select box.
<?php echo form_dropdown('product', $search_options['product_list'],
stripslashes($this->session->userdata('qes_product')),
'id="qes_product" tabindex="1"');
?>
Any idea ?

1) Make sure you're using DB storage for your session and not a cookie. Cookies have a limited size, and if you're doing something wrong, meaning not replacing the values but constantly adding them to the session, then it can get full. This is something else you should look into (what you store in your session)
2) Don't use $this->session in your view. Store everything in a array (like $formdata) in your session, then retrieve it in your controller and pass it to your view.
You can access it in your form using $formdata['ques_product']
If above doesn't help, try changing the value of sess_expire_on_close in your session config. There seems to be weird a bug in CI.
Good luck!

Related

PHP $_SESSION - some values retained, others lost during page submit

$_SESSION is successfully maintaining some fields across a page submit process, but ONLY those that are specifically conserved in my own, self-created $_SESSION initialisation function.
This would not be odd if my $_SESSION initialisation function was actually being called .. obviously!! Its whole purpose is to retain some key values, and wipe all the others from the $_SESSION array
However, this happens even when that function is NOT called by my code.
I have a View page including a form that is submitted, identifying a Control action file to trigger on submit.
I verify $_SESSION contents as part of the View page display. It displays the full set of $_SESSION array fields
I then do a print_r of $_SESSION contents immediately following session_start() in the identified Control file
Prior to the user hitting submit, the full set of $_SESSION values are there
Immediately after user hitting submit, most of them are gone, but anything I choose to retain on $_SESSION initialisation is retained.
NONE of my own code can possibly be running in between: there is pure PHP only there.
I had 2 theories on this.
1. PHP does something in the background and happens to have an internal function name that I used by mistake, which was then calling my function in preference to the system function of the same name, thus resulting in my $_SESSION getting initialised.
So I changed the function name (to clean_session()) -> same problem
Something about running the function when I initialise the $_SESSION upfront at the start of the application is giving those fields within the $_SESSION array a different set of properties from other fields which are added later. As a result, only those fields are surviving a generic loss across the page submit.
Option 2 seems like the only even potentially non-insane answer.
Does anyone know if that is even remotely possible?
Or alternatively what else could be happening?

How to check multiple form submission with PHP?

I have my own MVC applicaiton where I have 3 main area: model, view, controller.
In the view, I've created a form, to insert some data to the DB. The action is set to the same page and the type is: POST.
In the model, I have some basic checking if the form has been submitted or not. There is an 1 second sleep after that.
In the controller, I'm connecting the view and the model in a very simple way, so they can commuicate between each other.
However a problem is arising, whenever the visitor clicks multiple times on the submit button. Because there is 1 second waiting (it is intentional - so it looke like the system is processing the data), they can click multiple times to the button.
There is an issue with that, because this way, the data gets inserted to the database based on the amount of clicks. If I output a simple word, like "Foo" it will get displayed only 1 time (I guess this is because the page is reloading every time), however the data gets inserted multiple times to the DB.
Is there any way around this issue? My intention was to create a session and check if this is the first submit, else do nothing with the DB. However this didn't work, because the session always got the basic value upon the page load.
Can anyone help me with this issue?
Two options:
Put some javascript on the front-end to disable the button upon being clicked and then submit the form - this is probably the easiest way but you may want to try and leave the front-end as uncluttered as possible.
Set a session variable before you load the page, then when you do the insert delete this session variable immediately after. Before doing the insert you should check for the session variable - if it is set then the insert is allowed, if it is not then it isn't. You may also want to add in code into a master controller (if all your controllers are derived from one master controller) that checks what controller has been called and then if the session variable that is set should be for a different controller then it should be deleted (so it no longer exists if the user goes to a different page without submitting the form).

How to update only modified values in php

I'm using Codeigniter for building a website. The website has an option called 'update record' which allows the user to update 50 different values. The update page has the old value along with an empty text box for the user to enter the new value. Updating the whole record even though the user has not actually updated anything makes no sense (website uses MySQL database). I've tried using hidden form field with the original value and tried to update only modified fields. Though it is working fine, the process looks so lengthy every time comparing the old value with the new one and updating only if there is a change.
Is there a better way to do this (with or without using codeigniter)? Just curious to know.
Thanks in advance.
I would parse the post array and remove any entries that don't have a value for updated (which would mean the input was submitted empty).
Then you could only update the modified records
I don't know how code igniter passes the post array into the controller after form submission but you could do something like
<?php
foreach ($postData as $key => $value) {
if (empty($value)) {
unset($postData[$key]);
}
}
The only place this would fall down is if you legitimately want to store an empty record as the updated value.
I use javascript to do filter only updated values
1 - I use hashes.js to calculate a sha1 when page loaded. store it in hidden or just a variable, or you can do sha in the server.
2 - I also do a snapshot of the form in array when page loaded. I put all my input in one class and use jQuery('forminputs').each to put them in array.
3 - when user click submit, first I do another snapshot of the form as No. 2 and compare the hash. if the hash diff, I use php.js get the updated value php.array_diff_assoc(newsnapshot, oldsnapshot). and post this to server.
Though these seems like a lot of computation, but in fact it is not slow at all in firefox or chrome(never try IE).

PHP:How to save checkbox values temporary before submission

I'm a bit rusty in PHP,
I'm curently doing an PHP assignment where a user can select and save listed images/data/values into their own collection.
I have inputted all the data and printed it out in a repeat region with recordset paging.
I'm confused about how I am supposed to save a checked checkbox temporary before submission as there a more then 1 page as I'm using recordset paging to output the options.(Meaning: i have selected 2 values in the first page then i click next page to select balance values and finally submit my selection)
TIA
I have read an article on storing in session , that is the solution I guess, but I wonder how I'm supposed to send the value to the session when chaging the page (recordset paging generated by Dreamweaver)
To clarify the previous answers, you will most likely want to create a new $_SESSION variable for each check-box and associate a boolean with it.
You can store the result of a form post in PHP's $_SESSION variable.
Read this post for more information: Storing Form Data as a Session Variable
Also, there has to a tutorial or something in Google Land.
If you need to save the form results without submitting the form, try a JavaScript/AJAX approach. The idea is that you actually do submit the form, but in a behind-the-scenes kind of way (that is, the user never notices it). Essentially, you're going to want to build a "autosave" functionality. Look at these two posts:
Autosaving Form Input Using Prototype and PHP
AJAX Autosave functionality
They probably won't fit your needs exactly, but they should give you a good idea of what you should do.
Note: both of these posts use a timer to trigger their autosave functionality. I would suggest tweaking the trigger to detect any changes in your form instead.
Store them in _SESSION and process when needed.

Problem displaying $_POST data with session

I have a form which collects user info including name, location, url, email address etc etc AND company logo AND appropriate catagories they wish to be associated with (using a checkbox array in $_POST).
I then display a preview of the page (using $_SESSION and hidden form fields to hold the $_POST data) which their data will appear on BEFORE storing it in MySQL with an 'approve' button which links to the PHP page which has the MySQL INSERT query.
The problem i'm having is the preview page in between entry and submission to the database is having trouble with the logo input and the checkbox array input:
$_SESSION['clogo'] = $_POST['clogo'];
$_SESSION['cat[]'] = $_POST['cat[]'];
I get an 'undefined index' error when i try to preview these, however all other standard input is fine, and is then passed along to the insert mysql query on user approval. In addition, if i skip the 'preview page' and go straight to the insert PHP file from the original form, there is no problem, and all data is entered into the database correctly.
Can anyone help
You don't want this:
$_SESSION['cat[]'] = $_POST['cat[]'];
But this:
$_SESSION['cat'] = $_POST['cat'];
I.e., 'cat' is the index of the _POST array, and the element that it refers to is another array.
I'm guessing $_POST['cat'] is an array in itself. (I'm guessing this because of the [] following cat in your example.) If this is the case, then in your example, you're setting $_SESSION['cat[]'] equal to a variable name that doesn't exist. Try setting $_SESSION['cat'] equal to $_POST['cat'].
Remember to start the session using the start_session function. Otherwise nothing will be saved in the session.
Also worth noting is that when using some third party application they might remove all session variables. Wordpress is a good example.
$_POST[] array must be re-initialize, as you are posting to preview page and then php insert script page...
When you go directly from form to php script page ,POST have variables in it, so it must be working fine.
'cat[]' would not be the index. The index would be 'cat' which should be an array.

Categories