passing my script to index.php?action=

Started by EmilioGaviria, November 09, 2023, 02:03:13 PM

Previous topic - Next topic

EmilioGaviria

Really having trouble finding info about this

I created a page website.php

I used DB connection, included what's required
<?php
// Include SMF's important files
require_once('SSI.php');

// Get the database functions
global $smcFunc;

// Specify the database name
$db_name 'my_db';

// Select the database
$smcFunc['db_select_db']($db_name$GLOBALS['db_connection']);
// Set the page title
$context['page_title'] = 'MyPage';

// Load the template for your custom page
template_header();

// code


// Close the template
template_footer();

// Exit the script
exit;


but now instead of going directly to website.php, how do I pass it to index.php?action=website

Kindred

you need to create a hook to add it into the action array.... (recommended)

or, hack it directly into the action array (not the recommended choice)

However, doing it that way, I think you should re-code the file to not use SSI

SSI is intended for files OUTSIDE of the SMF system. If you are using action=website, then you are working INSIDE SMF.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

EmilioGaviria

Quote from: Kindred on November 09, 2023, 02:47:28 PMyou need to create a hook to add it into the action array.... (recommended)

or, hack it directly into the action array (not the recommended choice)

However, doing it that way, I think you should re-code the file to not use SSI

SSI is intended for files OUTSIDE of the SMF system. If you are using action=website, then you are working INSIDE SMF.
OMG, thank you so much for your reply
found it
https://wiki.simplemachines.org/smf/Add_a_custom_action

instead of banging my head, I'll just ask
is there a function\array for breadcrumbs as well that I'm too blind to find?




live627


EmilioGaviria

Quote from: live627 on November 09, 2023, 03:26:32 PM$context['linktree']

maybe it would help if I read this

<?php

// First of all, we make sure we are accessing the source file via SMF so that people can not directly access the file. 
if (!defined('SMF'))
die('Hack Attempt...');

function 
YourActionMain()
{

// Second, give ourselves access to all the global variables we will need for this action
global $context$scripturl$txt$smcFunc;

// Third, Load the specialty template for this action.
loadTemplate('YourAction');

//Fourth, Come up with a page title for the main page
$context['page_title'] = $txt['youraction_PageTitle'];
$context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title']));


//Fifth, define the navigational link tree to be shown at the top of the page.
$context['linktree'][] = array(
  
'url' => $scripturl'?action=youraction',
 
'name' => $txt['youraction'],
);

//Sixth, begin doing all the stuff that we want this action to display
//    Store the results of this stuff in the $context array.  
//    This action's template(s) will display the contents of $context.
$context['youraction_Head'] = $txt['youraction'];
$context['youraction_Body'] = 'Hello World';
              
}

?>


thanks again

EmilioGaviria

#5
so I managed to move all of the code except for search, for some reason is broken

echo '<form action="' . $scripturl . '?action=website;sa=search" method="get">';
echo '<input type="text" id="website-search-input" name="search" placeholder="Search " value="' . $searchQuery . '" />';
echo '<select name="category" id="website-search-category">';
echo '<option value="0">All Categories</option>';
foreach ($categories as $categoryId => $categoryName) {
    echo '<option value="' . $categoryId . '"';
    if ($selectedCategory === $categoryId) {
        echo ' selected="selected"';
    }
    echo '>' . $categoryName . '</option>';
}
echo '</select>';
echo '<input type="submit" value="Search" />';
echo '</form>';

after I click search I'm redirected to  index.php?search=test&category=0

but when I manually add
/index.php?action=website;sa=search&search=test&category=0

it works ..

is there more to search as well?

so I asked chatcht and it came up with some js redirect script
echo '<script>
    function submitwebsiteSearchForm() {
        var searchQuery = document.getElementById("website-search-input").value;
        var category = document.getElementById("website-search-category").value;
        window.location.href = "' . $scripturl . '?action=website;sa=search&search=" + encodeURIComponent(searchQuery) + "&category=" + category;
    }
</script>';

and
echo '<input type="button" value="Search" onclick="submitwebsiteSearchForm()" />';

this fixed it, but I feel like I must be missing something

EmilioGaviria

I have too many question, I think I'll just pay for Charter membership and get them answered

marking as solved

SleePy

Typically your charter membership wouldn't cover customizations, but we do try to assist.

I think you would get better luck if you change method="get" to method="post" in your search.  When you do a method="get" the browser may be ignoring everything after the ? as its the querystring, it is then building the querystring from all input boxes inside the form.

Since your inside SMF, you can use $context['session_var'] and $context['session_value']
You would add this to your template:
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">

In your code, at the start of it after your start into the search logic you would add:
checkSession();

This would ensure that only active users on the forum can use the search, preventing abuse by bots/search engines.
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

live627

Quote from: EmilioGaviria on November 09, 2023, 04:34:14 PMafter I click search I'm redirected to  index.php?search=test&category=0

but when I manually add
/index.php?action=website;sa=search&search=test&category=0

it works ..
I ran into this problem recently too. You'll need two hidden inputs, one for action and the other for sa.

EmilioGaviria

Quote from: live627 on November 09, 2023, 09:02:28 PM
Quote from: EmilioGaviria on November 09, 2023, 04:34:14 PMafter I click search I'm redirected to  index.php?search=test&category=0

but when I manually add
/index.php?action=website;sa=search&search=test&category=0

it works ..
I ran into this problem recently too. You'll need two hidden inputs, one for action and the other for sa.
wowww right you are


echo '    <input type="hidden" name="action" value="website" />';
echo '    <input type="hidden" name="sa" value="search" />';

did a trick



EmilioGaviria

Quote from: SleePy on November 09, 2023, 08:25:06 PMTypically your charter membership wouldn't cover customizations, but we do try to assist.

I think you would get better luck if you change method="get" to method="post" in your search.  When you do a method="get" the browser may be ignoring everything after the ? as its the querystring, it is then building the querystring from all input boxes inside the form.

Since your inside SMF, you can use $context['session_var'] and $context['session_value']
You would add this to your template:
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">

In your code, at the start of it after your start into the search logic you would add:
checkSession();

This would ensure that only active users on the forum can use the search, preventing abuse by bots/search engines.

thanks for the info

I agreed to help friend out with some modifications for smf, honestly it's been over 10 years since I worked with PHP, but writing code itself and pulling info from custom database tables wont be a problem

but I want to make sure it's done properly so he's not stuck with same version 
it's weird that there's no youtube channel (or I can't find one) that shows how to do this properly in 2023
everything is 10+ years old

also, I don't mind supporting opensource community and some questions might straight up be one liner for someone familiar with the source

Advertisement: