Daily Lucky Numbers:
8
11
19
29
40
42

submit box

Started by Fuji, May 03, 2023, 09:45:03 PM

Previous topic - Next topic

Fuji

want to be able to have the ppl to fill out a form

example:

game name: goes here in a box
steam id:  goes here in a box
email:     goes here in a box

submit

Chen Zhen

Hello Fuji,

Some simple PHP + HTML can accomplish this task.
What is the database table & columns to save the data or do you want to access it from $modSettings?

Chen Zhen


Here is a basic PHP block with your request:

global $context, $txt, $settings;
$steam_text = array(
'name' => 'Game Name',
'id' => 'Steam ID',
'email' =>'Email',
'submit' => 'Submit'
);
$check = isset($_POST['steam_gamename']) || isset($_POST['steam_userid']) || isset($_POST['steam_email']) ? true : false;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $check) {
// filter the inputs
foreach($_POST as $k => $v)
{
$v = trim($v);
$v = preg_replace('/\s+/', '', $v);
$v = filter_var($v, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
$_POST[$k] = $v;
}

$_SESSION['steam_gamename'] = isset($_POST['steam_gamename']) ? $_POST['steam_gamename'] : (!empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '');
$_SESSION['steam_userid'] = isset($_POST['steam_userid']) ? $_POST['steam_userid'] : (!empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '');
$_SESSION['steam_email'] = isset($_POST['steam_email']) ? filter_var($_POST['steam_email'], FILTER_SANITIZE_EMAIL) : (!empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '');

// add code to save your variables to the database...
}

echo '
<script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/sha1.js"></script>
<div style="display: flex; justify-content: flex-start;">
<form action="', htmlspecialchars($_SERVER['PHP_SELF']), '" name="steam_postpage" method="post" accept-charset="', $context['character_set'], '">
<div class="ehPortal_table">
<div class="ehPortal_row">
<div class="ehPortal_cell sp_left">
<label for="steam_gamename">', $steam_text['name'], ':</label>
<div style="display: inline;">
<input type="text" id="steam_gamename" name="steam_gamename" size="6" style="width:100%;" value="', !empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '', '" />
</div>
</div>
</div>
<div class="ehPortal_row">
<div class="ehPortal_cell sp_left"><label for="steam_userid">', $steam_text['id'], ':</label>
<div style="display: inline;">
<input type="text" name="steam_userid" id="steam_userid" size="6" style="width:100%;" value="', !empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '', '" />
</div>
</div>
</div>
<div class="ehPortal_row">
<div class="ehPortal_cell sp_left"><label for="steam_email">', $steam_text['email'], ':</label>
<div style="display: inline;">
<input type="text" name="steam_email" id="steam_email" size="6" style="width:100%;" value="', !empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '', '" />
</div>
</div>
</div>
<div class="ehPortal_row">
<div class="ehPortal_cell" style="padding-top: 0.2rem;">
<input type="submit" value="', $steam_text['submit'], '" class="button_submit" />
</div>
</div>
</div>
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
</form>
</div>';



You can add your own database query to save the data.

Fuji

I did your code i get this error

Parse error: syntax error, unexpected identifier " ", expecting ")" in /usr/www/ckdod/public/Sources/Subs-Portal.php(1276) : eval()'d code on line 5

Chen Zhen

You're likely not copying all the code because that block works fine.
Use the "expand" link and then the "select" link at the top of the code box.
After that right click & copy the highlighted code into the clipboard then paste it in a PHP block on your forum.

Fuji

that was it , but its only in a block format i get error when i try to add in to a page

Chen Zhen


It's working fine for me on a PHP page.
Remember to set the drop-down to PHP for it.

You'll need to adjust its style for a wide page view for when its viewed on a desktop/laptop.

Chen Zhen

Actually the issue you're having with errors is due to a bug in EhPortal.
With the most recent update I neglected to test preview for blocks & pages which is causing the error.
The code is good but don't use preview to test it until I release a fixed version of EhPortal this weekend.

Try this code without using "preview":


global $context, $txt, $settings;
$sideBlock = false;

if (!empty($sideBlock))
    list($justify, $inputSize) = array('flex-start', '6');
else
    list($justify, $inputSize) = array('space-around', '20');

$steam_text = array(
    'name' => 'Game Name',
    'id' => 'Steam ID',
    'email' =>'Email',
    'submit' => 'Submit'
);
$check = isset($_POST['steam_gamename']) || isset($_POST['steam_userid']) || isset($_POST['steam_email']) ? true : false;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $check) {
    // filter the inputs
    foreach($_POST as $k => $v)
    {
        $v = trim($v);
        $v = preg_replace('/\s+/', '', $v);
        $v = filter_var($v, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
        $_POST[$k] = $v;
    }

    $_SESSION['steam_gamename'] = isset($_POST['steam_gamename']) ? $_POST['steam_gamename'] : (!empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '');
    $_SESSION['steam_userid'] = isset($_POST['steam_userid']) ? $_POST['steam_userid'] : (!empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '');
    $_SESSION['steam_email'] = isset($_POST['steam_email']) ? filter_var($_POST['steam_email'], FILTER_SANITIZE_EMAIL) : (!empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '');

    // add code to save your variables to the database...
}

echo '
                                    <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/sha1.js"></script>
                                        <div style="display: flex; justify-content: ' . $justify . ';">
                                        <form action="', htmlspecialchars($_SERVER['PHP_SELF']), '" name="steam_postpage" method="post" accept-charset="', $context['character_set'], '">
                                            <div class="ehPortal_table">
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_gamename">', $steam_text['name'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" id="steam_gamename" name="steam_gamename" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_userid">', $steam_text['id'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" name="steam_userid" id="steam_userid" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_email">', $steam_text['email'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" name="steam_email" id="steam_email" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div style="display: flex; justify-content: ' . $justify . ';" class="ehPortal_cell" style="padding-top: 0.2rem;">
                                                        <input type="submit" value="', $steam_text['submit'], '" class="button_submit" />
                                                    </div>
                                                </div>
                                            </div>
                                            <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
                                        </form>
                                    </div>';




You can change the $sideBlock boolean to true or false depending on what block or page this is being used for.


Chen Zhen

Here is a fix for that PHP code concerning the form URL so it will work in blocks and pages...


global $context, $txt, $settings;
$sideBlock = false;

if (!empty($sideBlock))
    list($justify, $inputSize) = array('flex-start', '6');
else
    list($justify, $inputSize) = array('space-around', '20');

$steam_text = array(
    'name' => 'Game Name',
    'id' => 'Steam ID',
    'email' =>'Email',
    'submit' => 'Submit'
);
$check = isset($_POST['steam_gamename']) || isset($_POST['steam_userid']) || isset($_POST['steam_email']) ? true : false;
$filters = array('steam_gamename', 'steam_userid', 'steam_email');
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $check) {
    // filter the inputs
    foreach($_POST as $k => $v)
    {
        if (in_array($k, $filters, true)) {
            $v = trim($v);
            $v = preg_replace('/\s+/', '', $v);
            $v = filter_var($v, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
            $_POST[$k] = $v;
        }
    }

    $_SESSION['steam_gamename'] = isset($_POST['steam_gamename']) ? $_POST['steam_gamename'] : (!empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '');
    $_SESSION['steam_userid'] = isset($_POST['steam_userid']) ? $_POST['steam_userid'] : (!empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '');
    $_SESSION['steam_email'] = isset($_POST['steam_email']) ? filter_var($_POST['steam_email'], FILTER_SANITIZE_EMAIL) : (!empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '');

    // add code to save your variables to the database...
}

echo '
                                    <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/sha1.js"></script>
                                        <div style="display: flex; justify-content: ' . $justify . ';">
                                        <form action="', htmlspecialchars($url), '" name="steam_postpage" method="post" accept-charset="', $context['character_set'], '">
                                            <div class="ehPortal_table">
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_gamename">', $steam_text['name'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" id="steam_gamename" name="steam_gamename" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_gamename']) ? $_SESSION['steam_gamename'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_userid">', $steam_text['id'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" name="steam_userid" id="steam_userid" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_userid']) ? $_SESSION['steam_userid'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div class="ehPortal_cell sp_left">
                                                        <label style="display: flex; justify-content: ' . $justify . ';" for="steam_email">', $steam_text['email'], ':</label>
                                                        <div style="display: inline;">
                                                            <input type="text" name="steam_email" id="steam_email" size="' . $inputSize . '" style="width:100%;" value="', !empty($_SESSION['steam_email']) ? $_SESSION['steam_email'] : '', '" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="ehPortal_row">
                                                    <div style="display: flex; justify-content: ' . $justify . ';" class="ehPortal_cell" style="padding-top: 0.2rem;">
                                                        <input type="submit" value="', $steam_text['submit'], '" class="button_submit" />
                                                    </div>
                                                </div>
                                            </div>
                                            <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
                                        </form>
                                    </div>';




I'm still working on why this particular code crashes a portal page "preview".
It seems to work ok for a PHP block preview so it's a bug somewhere in EhPortal that I have to find.
Thanks for asking your question as it made me realize there is an issue with portal page previews.

Chen Zhen


The preview for pages should be fixed now with the new release which is available from the download section.

Fuji

thats working now but it doesn't submit to anywhere

Chen Zhen


// add code to save your variables to the database...

You didn't request anything specific other than a submit box in a portal page.
I don't know what you want to do with the data.. I just gave you a means to collect it.

Fuji

be able to i guess save it or send it to a place like a file

Chen Zhen

It can easily be set up to save the data into the settings database table.
I can have it run a check if the username exists or not & require the proper password for an existing login.
The password can be hashed using PHP's password_hash() and the view hidden for the input.
I'll rig it up this weekend.

What are you going to do with it once it's saved?
Having it login to an external server will take more if that's your intent.

Fuji

save to the website somewhere or just a file its for scrims we do for old game day of defeat