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!

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.