Problem displaying $_POST data with session - php

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.

Related

can i use include instead of session variables?

I am trying to pass the values of variables from one php page to another php page. Is it possible to include the previous php page in the next php page using include statement and use those variables and values rather than using $_SESSION['variable']="value".
If not, is there any other way to pass the values? Thanks in advance!
No. You'll run the code in the included file again, from scratch. Any data produced based on user input will have been lost.
If not, is there any other way to pass the values?
Passing the entire value to the browser and having it send the entire value back to the new script. (via form data, query strings in links, cookies, etc).
I am trying to create a script where a user can post something they want to sell and then I am using while loop in php to display the information from the database. Other users can click on the contact button and send an email to them. I was told that if I use 'session', the same email id will be stored for every listing. So I am wondering if I can use the hidden field and if so, how do I assign that php variable value to the html hidden field?
You can't use hidden fields for this. You need to store persistent data on the server. Store the email address with the rest of the information in the database. (You'll probably want a separate user table and associate the For Sale item with a user using a foreign key).

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).

Codeigniter select box not populating session value

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!

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.

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Categories