hello everyone i am facing a little problem
I have a 3 dependent selectboxes and i want to save the selected values in the session so user can remember what category i selected before also it prints the last visited link

here is my code pelase tell me what code i use in it to save the last selected values

<script type="text/javascript"><!--

$(document).ready(function(){
$("#cPath").change(modelList);
$("#select_model").change(yearList);
//$("#select_year").focus();
$("#select_year").change(function()
{
var baseDirectory=document.getElementById("basemodulepath").value;
var cPath1=$("#cPath").val();
var cPath2=$("#select_model").val();
var cPath3=$("#select_year").val();
window.location=(baseDirectory+"index.php?main_page=index&cPath="+cPath1+"_"+cPath2+"_"+cPath3);
}
);
});
var modelList=function model_list(){
var baseDirectory=document.getElementById("basemodulepath").value;
var cPath=document.getElementById("cPath").value;
$.ajax({
type: 'POST',
url:baseDirectory+"modellist.php",
data:"cPath="+cPath+"&my=1",
success: function(data){
// alert(data);
if(data!=""){
arrayVar=new Array();
arrayVar=data.split("|");
arrayVarId=new Array();
arrayVarId=arrayVar[0].split("^");
arrayVarName=new Array();
arrayVarName=arrayVar[1].split("^");
$('#select_model').html("");
$('#select_model').append("<option value=''><?php echo TEXT_PLEASE_SELECT; ?><\/option>");
for(i=1;i<arrayVarId.length-1;i++)
{
$('#select_model').append($("<option><\/option>").attr("value",arrayVarId).text(arrayVarName));
}
}
}
});
};
var yearList=function year_list(){
var baseDirectory=document.getElementById("basemodulepath").value;
var cPath=document.getElementById("select_model").value;
$.ajax({
type: 'POST',
url:baseDirectory+"modellist.php",
data:"cPath="+cPath+"&my=2",
success: function(data){
if(data!=""){
arrayVar=new Array();
arrayVar=data.split("|");
arrayVarId=new Array();
arrayVarId=arrayVar[0].split("^");
arrayVarName=new Array();
arrayVarName=arrayVar[1].split("^");
$('#select_year').html("");
$('#select_year').append("<option value=''><?php echo TEXT_PLEASE_SELECT; ?><\/option>");

for(i=1;i<arrayVarId.length-1;i++)
{
$('#select_year').append($("<option><\/option>").attr("value",arrayVarId).text(arrayVarName));
}
}
}
});
};
//--></script>

Recommended Answers

All 11 Replies

You pass the selected values to modellist.php by ajax for some processing, using $_POST array, if I got the code correctly. So the modellist.php script would be good candidate to save values to the session. IN modellist.php just assign the values to the $_SESSION array using their names for associative keys.

<?php
session_start();

$_SESSION['cPath'] = $_POST[cPath];
$_SESSION['select_model'] = $_POST[select_model];
$_SESSION['select_year'] = $_POST[select_year];

It is even better if you check for existence of $_POST elements first:

$_SESSION['cPath'] = isset($_POST[cPath]) ? $_POST[cPath] : '';
$_SESSION['select_model'] = isset($_POST[select_model]) ? $_POST[select_model] : '';
$_SESSION['select_year'] = isset($_POST[select_year]) ? $_POST[select_year] : '';

I hope I got the code right :-)

here is the code of modelist.php if you require other code please let me know thanks

<?php

       require('includes/application_top.php');
       $manufacturers_id=$_REQUEST['cPath'];
      if($_REQUEST['cPath']!=""){
       $categories = "select c.categories_id, cd.categories_name, c.parent_id
                                from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                where c.categories_id = cd.categories_id
                                and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and c.parent_id = '".$manufacturers_id."' 
                                order by c.sort_order, cd.categories_name";
    $product_to_categories_query="select distinct categories_id from ".TABLE_PRODUCTS_TO_CATEGORIES;
    $product_c_result=$db->Execute($product_to_categories_query);
    $categories_array=array();
    $inarray="";
    $count=0;
     while(!$product_c_result->EOF){
                $categories_array[]= $product_c_result->fields['categories_id'];
                if($count==0){
                $inarray.=$product_c_result->fields['categories_id'];
                }
                else{
                $inarray.=",".$product_c_result->fields['categories_id'];
                }
                $product_c_result->MoveNext();
                $count=1;
                }
    $inarray=" IN (".$inarray.")";
    $product_to_categories_query="select c.categories_id, cd.categories_name, c.parent_id
                                from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                where c.categories_id = cd.categories_id
                                and cd.language_id = '" . (int)$_SESSION['languages_id'] . "' and c.categories_id ".$inarray." 
                                order by c.sort_order, cd.categories_name";
    $product_c_result=$db->Execute($product_to_categories_query);
    $categories_array=array();
    $inarray="";
    $count=0;
     while(!$product_c_result->EOF){
                 $categories_array[]= $product_c_result->fields['categories_id'];
                 $categories_array1[]= $product_c_result->fields['categories_name'];
                 $categories_array2[]= $product_c_result->fields['parent_id'];
                 $product_c_result->MoveNext();
              }         
                $expected = $db->Execute($categories);
                       $ids="";
                       $name="";
                       $count=0;
                       while (!$expected->EOF) {
                       if(in_array($expected->fields['categories_id'],$categories_array2) ||in_array($expected->fields['categories_id'],$categories_array)){
                           if($count==0){
                            $ids="^";
                            $name="^";
                            }
                            $ids.=$expected->fields['categories_id']."^";
                            $name.=$expected->fields['categories_name']."^";
                            $count=1;
                              }
                            $expected->MoveNext();
                            } 
                        if($count==1)
                        echo $ids."|".$name; 
}
                   ?>

Can you specify which data exactly would you like to store in session, please.

values of dropdown boxes to maintain the last selected items

OK, so you have three select elements (drop down boxes): cPath, select_model and select_year. When user selects the data, you send over only cPath. Is that correct?

Now, if you want to store other two values into session, you have to send them over, too. Which means you have to add them to the query string:

$.ajax({
    type: 'POST',
    url:baseDirectory+"modellist.php",
    data:"cPath="+cPath+"&my=1&select_model=" + select_model + "&select_year=" + select_year,
    success: function(data){
    ...

In modelist.php script you first store these values into session and then do de rest of the code:

<?php
session_start();
$_SESSION['cPath'] = isset($_POST[cPath]) ? $_POST[cPath] : '';
$_SESSION['select_model'] = isset($_POST[select_model]) ? $_POST[select_model] : '';
$_SESSION['select_year'] = isset($_POST[select_year]) ? $_POST[select_year] : '';


require('includes/application_top.php');
$manufacturers_id=$_REQUEST['cPath'];
...

If my assumptions about your code are wrong, please correct me. And it is recommended to avoid using $_REQUEST. It is safer to use $_POST.

thanks bro but this is a zencart's plugin i dont know about about it i gave you the download link of this please check this out
I tried your code but its not working

it will be easy if there is a little tooltip show with the last searched url i.e. Your Last Search( Make:"Company" Model: "Model Name" Year:"Year Name")

What is cPath?

I dont know about that you please check the plugin by downloading it

Studying this plugin code might take some time. I am not sure if I can do it right now. Not promissing anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.