Hi guys, sorry if i'm posting this and it is already answered in another discussion.

I am creating a website for a sales process using phpstorm, I'm new with all this so that is why I'm so confused.

Actually i have this form:

<form name="frm" id="frm" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">

                                                <div id="salesopt"><select id="soptns" name="soptns">
                                                <option value="">-----</option>
                                                <option value="NS">Normal Sale</option>
                                                <option value="PT">Platinum Sale</option>
                                                <option value="SP">Seniors Sale</option>
                                                <option value="UP">Upgrade</option>
                                                                </select></div><br>

                </form>

I have different *.php files to each of the options. If I want to load the content of each of them into the same page after the sellection of any one. which is the best way to do it?

Thanks in advance for your help!

Dani AI

Generated

Short summary: and point toward server-side inclusion when the form posts to itself. Both approaches are valid; choose server-side includes when you need SEO or server processing, or client-side loading (AJAX/fetch) when you want to swap content without a full page reload. The notes below expand on those answers with security, structure, and a lightweight client-side option.

Server-side guidance (builds on /)

  • Treat the selected value as untrusted input. Use a small whitelist/mapping of allowed keys -> partial filenames and never interpolate raw POST values into paths.
  • Keep include files as fragments (partials) that expect to be injected into the existing page (no duplicated <head>/<html>).
  • Place partials in a dedicated directory, restrict extensions, and verify file existence before including. Prefer throwing a controlled error or showing a default fragment rather than failing loudly.
  • For forms that change application state, include CSRF protection and validate on the server.

Client-side (AJAX) option — avoids reload, with progressive enhancement

  • Keep the original form action as the non-JS fallback.
  • Use JavaScript to fetch a trusted endpoint that returns the fragment HTML for the chosen option and inject it into a target container:
    document.getElementById('soptns').addEventListener('change', function(e){
    const val = e.target.value;
    if (!val) return;
    fetch('sale-partial.php?s=' + encodeURIComponent(val))
      .then(r => r.ok ? r.text() : Promise.reject(r.status))
      .then(html => document.getElementById('saleTarget').innerHTML = html)
      .catch(err => console.error('Load failed', err));
    });

    The server-side endpoint must validate s against a whitelist and return only the fragment.

Troubleshooting and accessibility

  • Use browser DevTools to check network requests and response HTML.
  • Verify file permissions and server error logs if includes fail.
  • For accessibility, ensure keyboard users can submit or that injected content is announced (ARIA live) and focus is managed appropriately.

This guidance complements the server-side examples already posted while emphasizing secure, maintainable structure and a no-reload alternative.

Recommended Answers

All 3 Replies

Okay, if I am understanding this correctly: you wish to include a file depending on which sales option has been chosen? Here's a simple demonstration:

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['soptns'])) {
    $salesFiles = array (
        'ns': 'ns.inc.php',
        'pt': 'pt.inc.php',
        'sp': 'sp.inc.php',
        'up': 'up.inc.php'
    );

    $chosen = $_POST['soptns'];

    if (array_key_exists($chosen, $salesFiles)) {
        require_once($salesFiles[$chosen]);
    }
}

We first check to see if we're responding to an HTTP POST request (the form has been submitted). If so, has the sales option been passed through?

Assuming it has, we declare an associative array containing the legal sales option values. The values in this array are the files to be included. The purpose of this array is to define what file belongs to each possible option. It's a whitelist, if you will. And we can grab the chosen value by simply referencing $_POST['soptns'].

If the array contains a key equal to the given option then it's legal. So we check for that. And ultimately we then include the file by grabbing it from the array and passing it to the require_once() function.

From what I can see you would need to test $_POST['soptns'] value to output the correct file. Something like this on the same file, since the form action is itself:

switch($_POST['soptns']){
    case 'NS':
        include_once(normalSale.php);
        break;
    case 'PT':
        include_once(platinumSale.php);
        break;
    case 'SP':
        include_once(seniorsSale.php);
        break;
    case 'UP':
        include_once(upgrade.php);
        break;
    default:
        include_once(default.php);
        break;
}

I think this would sort your problem. Let us know if anything goes wrong^^

I was thinking, so I'll throw in another solution for you to choose from:

if(isset($_POST['soptns'])){
    $filename = 'sale'.$_POST['soptns'].'.php';
    //filename would be saleNS.php
    include_once($filename);
}

If you need to include a file for each type of sale, but having the full version for the selected and maintaining the same order every time:

$types = array('NS', 'PT', 'SP', 'UP');
foreach($types as $type){
    if(isset($_POST['soptns']) && $_POST['soptns'] == $type){
        $filename = 'sale'.$type.'full.php';
        //filename would be saleNSfull.php
        include_once($filename);
    } else {
        $filename = 'sale'.$type.'mini.php';
        //filename would be saleNSmini.php
        include_once($filename);
    }
}
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.