0

I am working on a form and I've stumbled upon a problem. I have different users in which I want them to see different values for one of my SELECT tags; so far I am using PHP to get the URL and if $field == 'donor' then it takes off some of the options. Here's the code:

function defineUser($field){
    if(isset($_GET['user']) && urldecode($_GET['user'])){
        $field = urldecode($_GET['user']);
        if($field == "donor"){
            return "<p>
                    <label for=designation>Designation</label>
                    <select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
                        <option value=''>Select...</option>
                        <option value=''>Option 1</option>
                        <option value=''>Option 2</option>
                        <option value=''>Option 3</option>
                        <option value='Other'>Option 4</option>
                    </select>
                    </p>";
        } else if ($field != "donor") {
            return "<p>
                    <label for=designation>Designation</label>
                    <select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
                        <option value=''>...</option>
                    </select>
                    </p>";
        } else {
            return "<p>
                    <label for=designation>Designation</label>
                    <select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
                        <option value=''>...</option>
                    </select>
                    </p>";
        }
    }
}

This is my function, now on the page source itself I simply have,

<?php echo defineUser($_GET['user']); ?>

My question simply is how can I get each user to see a standard options set - inside my select tag - based on the URL the user has been sent from?

5
  • It's not clear to me what you're asking in that last question. Can you elaborate? Commented Oct 27, 2016 at 12:57
  • Your function does not make much sense.. You pass it a param ($field) but the first thing you're doing ... you overwrite it at the 3rd line. why? Commented Oct 27, 2016 at 12:59
  • I think by "standard options" he means the last else where $field doesn't match the other values. Commented Oct 27, 2016 at 13:05
  • David, I reworded the question some. I am sorry for my English. Commented Oct 27, 2016 at 13:17
  • Twinfriends on the third line I was trying to get the value from the URL param Commented Oct 27, 2016 at 13:18

1 Answer 1

1

Your code could be simplified:

function defineUser() {
    $user = isset($_GET['user']) ? urldecode($_GET['user']) : null;

    switch ($user) {
        case 'donor':
            return "<p>
                    <label for=designation>Designation</label>
                    <select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
                        <option value=''>Select...</option>
                        <option value=''>Option 1</option>
                        <option value=''>Option 2</option>
                        <option value=''>Option 3</option>
                        <option value='Other'>Option 4</option>
                    </select>
                    </p>";
            break;

        default:
            return "<p>
                    <label for=designation>Designation</label>
                    <select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
                        <option value=''>...</option>
                    </select>
                    </p>";
    }
}

You don't need to pass it $_GET['user'] since that is globally accessible, unless you're worried about dependency. Second, you were essentially repeating the same block of HTML twice in your if else clause, using a switch here makes more sense.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean about $_GET['user'] what could I use instead?
I just mean you don't have to pass it in as a parameter. Instead of defineUser($field) you can simply call defineUser()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.