I'm using joomla and sobi2, I don't like the layout and want to create a way for the site visitor to choose a category from the home page which should look like this. http://www.bidmyneeds.com/index.php?option=com_sobi2&Itemid=28

Then I want them taken to this page. http://www.bidmyneeds.com/index.php?option=com_sobi2&sobi2Task=search&Itemid=28

Where the select catagory would be already populated. I've figured out that a cookie sobi2SearchCookie[cid] when set to 10 is "deck and porches" and if modify the cookie to a different number it changes the selection. But only after the first search is completed.

Name sobi2SearchCookie[cid]
Value 10
Host .www.bidmyneeds.com
Path /
Secure No
Expires At End Of Session

But I'm having problem creating the java script to create a hyperlink, that sets sob2Searchcookie[cid] when it is clicked. (really I don't have a clue)

Also, this may be important, there is a cookie created after the geo search which is
Name 004b3328e3e75e02c13a06e35471f97d
Value e0c8d90a6cda9baaecf61a62a2b49497
Host bidmyneeds.com
Path /
Secure No
Expires At End Of Session

I'd like to add that I've found this code, I'm by no means good with java, but is this code clearing the cookie, the first time it is loaded?

/* searching in categories */
$cid = (int) sobi2Config::request($_REQUEST, "sobiCid", 0);
setcookie("sobi2SearchCookie[cid]", $cid, 0, "/", $cookieDomain);

if(!empty($sIDs) || !$searching) {
if($cid && $cid !=0) {
if(!is_array($sIDs) || empty($sIDs)) {
$itids = null;
}
else {
$ids = implode(" , ", $sIDs);
$itids = "AND itemid IN ({$ids})";
}
$cids = array();
$config->getChildCats($cid, $cids);
$cids = implode(" , ", $cids);
$query = "SELECT itemid FROM #__sobi2_cat_items_relations WHERE catid IN({$cids}) {$itids} ;";
$database->setQuery($query);
$sIDs = $database->loadResultArray();
if( empty( $sIDs ) ) {
$sIDs = array( 0 );
}
}
}
/* getting plugins modifications */
$pluginsOutput = null;
if(!empty($config->S2_plugins)) {
foreach ($config->S2_plugins as $plugin) {
if(method_exists($plugin,"onAjaxSearchResult")) {
$plugin->onAjaxSearchResult($sIDs, $dataForFields, $pluginsOutput);
}
}
}

$total = count($sIDs);

if($total) {
$whereId = implode(" , ", $sIDs);
$now = $config->getTimeAndDate();

$query = "SELECT COUNT(itemid) FROM `#__sobi2_item` " .
"WHERE ( itemid IN ({$whereId}) AND `published` = 1 AND (`publish_down` > '{$now}' OR `publish_down` = '{$config->nullDate}' ) ) ";
$database->setQuery($query);
if($database->loadResult()) {
$total = $database->loadResult();
}
else {
$total = 0;
}
}

$items = array();
$page = 0;
$pages = 0;
if($total) {
/*
* now we have all id's from items contains search strings
*/

Dani AI

Generated

Quick, practical option first: since the Sobi2 code already looks for a request parameter for category, the simplest and most reliable fix is to let the server set the cookie. Add the category id to the search link so the component can set its cookie before rendering the page. A relative link such as /index.php?option=com_sobi2&sobi2Task=search&sobiCid=10&Itemid=28 will trigger the server-side logic and make the category select prepopulate on load.

A client-side approach is possible, but has caveats. Cookies created with JavaScript are only sent on the following HTTP request, and JavaScript cannot modify or read cookies marked HttpOnly. Domain and path must match the component’s cookie settings (subdomain vs naked domain matters). Those timing and scope issues explain ’s observation that changes only took effect after the “first” search, and why suggested this is partly a PHP workflow problem.

If a JavaScript route is preferred (set cookie then navigate), an implementation pattern looks like this:

<script>
function chooseCategory(cid, redirect) {
  var name = 'sobi2SearchCookie[cid]';
  var value = encodeURIComponent(String(cid));
  document.cookie = name + '=' + value + '; path=/;';
  window.location = redirect || '/index.php?option=com_sobi2&sobi2Task=search&Itemid=28';
}
</script>

<a href="#" onclick="chooseCategory(10); return false;">Decks & Porches</a>

Troubleshooting checklist: confirm the cookie name, domain and path in the browser; clear conflicting geo/session cookies; verify whether Sobi2 prefers request param, cookie or session when building the form; and if needed, add the sobiCid parameter to links so PHP sets the cookie server-side before output.

Recommended Answers

All 2 Replies

Is this even a Javascript question given that what you have posted seems like PHP? If you want to manipulate cookies using Javascript, read this. If this really is a PHP question, allow me to move it to the PHP forum.

I believe it has to be a java script question, because I want to create links that set a cookie within php, and from my quick reading on php you have to set cookies before any output?

And the easy way to set cookies is with java inside php.

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.