• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Help with displaying search form when sub-category page appears

chrisj

Legendary Coder
Thanks for all the previous help. The web script that I'm trying to modify successfully allows for the ability to search the site by keyword, and filters results by category and by sub-category. I am trying to display a search form when sub-category page appears (as a first step in creating the ability to search the sub-category page by keyword). The code I was looking over shows sub-category related code: https://pastebin.com/2hsXVK4e on lines 142 - 155 and lines 215 - 225 (I believe the variable to display the Form on a sub-category page may be 'sub_id'?)

HTML:
<form action="content.php" method="GET">
<input id="search" name="keywords" type="text" placeholder="Type here">
<input type="submit" value="Search" />
</form>

Any help/guidance/comment with triggering the Form to appear, upon sub_catageory page being displayed, is appreciated.
 
Last edited:

Ghost

King Coder
I played around with it, but it's a little hard to understand where to put any new code because we can't see the full view that would be displayed on this page.

If I had to guess it might have something to do with:
$pt->show_sub = true;

Maybe something in the page like this?
PHP:
if($pt->show_sub == true){
    // show your form?
}

Again, really hard to understand what's going on here without seeing the end result from the code and being able to manipulate values to see how the page changes.
 

chrisj

Legendary Coder

Many thanks for your reply.
After looking at your reply and suggestion, I see this part on the html page, might be more helpful:

<script type="text/javascript"> $(document).on('change', '#category_id', function(event) {
event.preventDefault();
id = $(this).val();
$('#sub_category_id').html(sub_categories_array["'"+id+"'"]);
});
$(document).on('change','#sub_categories_', function(event) {
window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();
});
</script>[/CODE]

So, based on this, would I need some code to esssentially say when this happens:

JavaScript:
window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();

show the Search Form on the same page?

Any help/guidance/comment is appreciated.
 
Last edited:

Ghost

King Coder
<script type="text/javascript"> $(document).on('change', '#category_id', function(event) {
event.preventDefault();
id = $(this).val();
$('#sub_category_id').html(sub_categories_array["'"+id+"'"]);
});
$(document).on('change','#sub_categories_', function(event) {
window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();
});
</script>[/CODE]

So, based on this, would I need some code to esssentially say when this happens:

JavaScript:
window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();

show the Search Form on the same page?

Any help/guidance/comment is appreciated.


in JavaScript you will want to get the full pathname of the URL:
JavaScript:
var path = window.location.pathname,
      pathparts = path.split("/"),
      id = parthparts[3],
      subcats = parthsparts[4];
// this will come to /videos/category/id/subcategoriesvalue, so ["", "videos","category","id","subcategoriesvalue"]
// notice how 1st value is blank "" that's the empty value before the first "/" in your pathname

$('#sub_category_id').html(sub_categories_array["'"+id+"'"]); // maybe?
// or maybe $('#sub_category_id').html(sub_categories_array[id]); - I am not sure about why you're doing this, but the id in my code is already a string type so it may work without the " ' " + stuff ...

// show search form

I hope this helps you...
You could also detect stuff with PHP and then use if statements to load or not load certain views based on that... by modifying what you have now. You could also use PHP to get values from the URL and then put them into JS doing like <script>var something = "<?php echo $something ?>";</script> but it's obviously up to you and your project :)



Here's something you can do if you use ?blah= GET queries in your URL for custom modifications in future:

https://codeforum.org/threads/help-with-displaying-search-form-when-sub-category-page-appears.2355/?coding=cool&this=fun#post-12715

JavaScript:
var urlgets= window.location.search;

console.log(urlgets);

//  ?coding=cool&this=fun

finalurlgetparts = {}

urlgetparts = (urlgets.replace("?", "*SPLIT*").replace("&", "*SPLIT*")).split("*SPLIT*").forEach(function(urlgetparam){

 if(urlgetparam==''){

 return

 }

 var theseparts = urlgetparam.split("="); console.log(theseparts, theseparts[0],theseparts[1]);

  finalurlgetparts[theseparts[0]] = theseparts[1]

})

We end up with an object like this: {"coding":"cool", "this":"fun"}
 
Last edited:
Top