I have a session_id which I am using to identify the user in a shopping basket type application.
The session_id is stored in the database as the session id.
When I update the cart, the session_id, product_id and qty are stored in the Basket table.
I am trying to add an item to the Basket, then show the updated basket on the screen without page refresh using jquery.
Do I store the session id in a hidden html field or is there a better way of getting jQuery to know what the session_id is?
UPDATE: Ok, so now based on answers and comments below, I don't really want to be using the session id in a hidden field. I certainly don't want to show the session id within the html page.
I may need to backtrack a little...
When ajax is calling my update basket function, I am faced with the following error...
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ..... /ajaxData.php on line 105
The code I am using to start the session is...
if (!session_id()){
session_start();
}
I don't really understand what this warning means, so this is why I was thinking of sending the session id via the hidden html field.
I guess the solution is to fix what the warning was and try to do things properly...
Any suggestions on my warning message?
IMO all session data must be managed in the server, if you really need it in the front-end get it asynchronously. For example:
1.- jquery stuff
$.get(
window.location.href,
{
get_session_id : 1
},
function(data) {
var session_id = data.session_id
if ("" == session_id){
// alert user
// give a friendly way to proceed without loss user's time
// return;
}
// use session_id
}, "json");
UPDATE To avoid Warning: session_start() [function.session-start]...
error put this snippet early as possible in your script. BEFORE any html output.
2.- server stuff
// none output before this snippet
if ($_GET['get_session_id'])
{
// get $session_id
$output['session_id'] = $session_id ? $session_id : '';
echo json_encode($output);
exit;
}
UPDATE 2:
The reason I propose to you this solution is that I think it is better to get the session ID dynamically when needed, instead of having it stored in a static way, is that this identifier may be modified, deleted, etc., while the user takes his time to fill a form.
Unfortunately this defect is very common and annoying. Submit a form and instead of being processed, we are redirected to a login page, losing all data sent.
Use this JavaScript code (combined with PHP):
<script type="text/javascript">
var sessionid = "<?php echo session_id(); ?>";
</script>
Then use the sessionid variable in your jQuery (AJAX?) call.
You could use a data-
attribute:
<form id="cart-form" action="" data-sessionid="<?php echo session_id(); ?>">
<!-- rest of form code -->
</form>
Then in your jQuery, just use $(this).data('sessionid')
to retrieve it:
$('#cart-form').submit(function() {
var session_id = $(this).data('sessionid');
// ... rest of your code here ...
});